Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate redux reducer about write-reviews #70

Merged
merged 8 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/actions/write-reviews/rankedReviews.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import Review from '@/shapes/model/review/Review';
import Semester from '@/shapes/model/subject/Semester';
import type Review from '@/shapes/model/review/Review';
import type { SemesterType } from '@/shapes/enum';

const BASE_STRING = 'WR_RR_';

export const RESET = `${BASE_STRING}RESET` as const;
export const ADD_SEMESTER_REVIEWS = `${BASE_STRING}ADD_SEMESTER_REVIEWS` as const;
export const SET_SEMESTER_REVIEW_COUNT = `${BASE_STRING}SET_SEMESTER_REVIEW_COUNT` as const;

type SemesterKey = `${number}-${SemesterType}` | 'ALL';

export function reset() {
return {
type: RESET,
};
}

export function addSemesterReviews(semester: Semester, reviews: Review[]) {
export function addSemesterReviews(semester: SemesterKey, reviews: Review[]) {

Check warning on line 18 in src/actions/write-reviews/rankedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/actions/write-reviews/rankedReviews.ts#L18

Added line #L18 was not covered by tests
return {
type: ADD_SEMESTER_REVIEWS,
semester: semester,
reviews: reviews,
};
}

export function setSemesterReviewCount(semester: Semester, count: number) {
export function setSemesterReviewCount(semester: SemesterKey, count: number) {

Check warning on line 26 in src/actions/write-reviews/rankedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/actions/write-reviews/rankedReviews.ts#L26

Added line #L26 was not covered by tests
return {
type: SET_SEMESTER_REVIEW_COUNT,
semester: semester,
Expand Down
2 changes: 1 addition & 1 deletion src/components/blocks/SemesterBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
const SemesterBlock: React.FC<Props> = ({ semester, isRaised, onClick }) => {
const { t } = useTranslation();

const handleClick = onClick?.(semester);
const handleClick = () => onClick?.(semester);

const title =
semester === 'ALL'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const CombinedReducer = combineReducers({
rankedReviews: rankedReviews,
});

export type WriteReviewsState = ReturnType<typeof CombinedReducer>;
export default CombinedReducer;
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { RESET, ADD_REVIEWS, UPDATE_REVIEW } from '../../actions/write-reviews/latestReviews';
import {

Check warning on line 1 in src/reducers/write-reviews/latestReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/latestReviews.ts#L1

Added line #L1 was not covered by tests
RESET,
ADD_REVIEWS,
UPDATE_REVIEW,
LatestReviewsAction,
} from '@/actions/write-reviews/latestReviews';
import Review from '@/shapes/model/review/Review';

const initialState = {
interface LatestReviewsState {
reviews: Review[] | null;
}

const initialState: LatestReviewsState = {

Check warning on line 13 in src/reducers/write-reviews/latestReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/latestReviews.ts#L13

Added line #L13 was not covered by tests
reviews: null,
};

const latestReviews = (state = initialState, action) => {
const latestReviews = (state = initialState, action: LatestReviewsAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case ADD_REVIEWS: {
const newReviews = [...(state.reviews !== null ? state.reviews : []), ...action.reviews];
return Object.assign({}, state, {
return {

Check warning on line 24 in src/reducers/write-reviews/latestReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/latestReviews.ts#L24

Added line #L24 was not covered by tests
...state,
reviews: newReviews,
});
};
}
case UPDATE_REVIEW: {
if (!state.reviews) {
return state;

Check warning on line 31 in src/reducers/write-reviews/latestReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/latestReviews.ts#L31

Added line #L31 was not covered by tests
}
const originalReviews = state.reviews;
const { review, isNew } = action;
const foundIndex = originalReviews.findIndex((r) => r.id === review.id);
Expand All @@ -29,9 +43,10 @@
: isNew
? [review, ...originalReviews.slice()]
: [...originalReviews.slice()];
return Object.assign({}, state, {
return {

Check warning on line 46 in src/reducers/write-reviews/latestReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/latestReviews.ts#L46

Added line #L46 was not covered by tests
...state,
reviews: newReviews,
});
};
}
default: {
return state;
Expand Down
23 changes: 0 additions & 23 deletions src/reducers/write-reviews/likedReviews.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/reducers/write-reviews/likedReviews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { LikedReviewsAction, RESET, SET_REVIEWS } from '@/actions/write-reviews/likedReviews';

Check warning on line 1 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L1

Added line #L1 was not covered by tests
import Review from '@/shapes/model/review/Review';
interface LikedReviewsState {
reviews: Review[] | null;
}

const initialState: LikedReviewsState = {

Check warning on line 7 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L7

Added line #L7 was not covered by tests
reviews: null,
};

const likedReviews = (state = initialState, action: LikedReviewsAction) => {
switch (action.type) {
case RESET: {
return initialState;

Check warning on line 14 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L13-L14

Added lines #L13 - L14 were not covered by tests
}
case SET_REVIEWS: {
return { ...state, reviews: action.reviews };

Check warning on line 17 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
}
default: {
return state;

Check warning on line 20 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
}
}
};

export default likedReviews;

Check warning on line 25 in src/reducers/write-reviews/likedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/likedReviews.ts#L25

Added line #L25 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@
RESET,
ADD_SEMESTER_REVIEWS,
SET_SEMESTER_REVIEW_COUNT,
} from '../../actions/write-reviews/rankedReviews';
RankedReviewsAction,
} from '@/actions/write-reviews/rankedReviews';
import Review from '@/shapes/model/review/Review';

const initialState = {
interface RankedReviewState {
reviewsBySemester: {
[semester: string]: Review[];
};
reviewCountBySemester: {
[semester: string]: number;
};
}

const initialState: RankedReviewState = {

Check warning on line 18 in src/reducers/write-reviews/rankedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/rankedReviews.ts#L18

Added line #L18 was not covered by tests
reviewsBySemester: {},
reviewCountBySemester: {},
};

const latestReviews = (state = initialState, action) => {
const latestReviews = (state = initialState, action: RankedReviewsAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case ADD_SEMESTER_REVIEWS: {
const prevReviewsOfSemester = state.reviewsBySemester[action.semester] || [];
return Object.assign({}, state, {
return {

Check warning on line 30 in src/reducers/write-reviews/rankedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/rankedReviews.ts#L30

Added line #L30 was not covered by tests
...state,
reviewsBySemester: {
...state.reviewsBySemester,
[action.semester]: prevReviewsOfSemester.concat(action.reviews),
},
});
};
}
case SET_SEMESTER_REVIEW_COUNT: {
return Object.assign({}, state, {
return {

Check warning on line 39 in src/reducers/write-reviews/rankedReviews.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/rankedReviews.ts#L39

Added line #L39 was not covered by tests
...state,
reviewCountBySemester: {
...state.reviewCountBySemester,
[action.semester]: action.count,
},
});
};
}
default: {
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,51 @@
SET_REVIEWS_FOCUS,
CLEAR_REVIEWS_FOCUS,
SET_REVIEWS,
} from '../../actions/write-reviews/reviewsFocus';
ReviewsFocusAction,
} from '@/actions/write-reviews/reviewsFocus';

import { ReviewsFocusFrom } from '@/shapes/enum';
import Review from '@/shapes/model/review/Review';
import Lecture from '@/shapes/model/subject/Lecture';

const initialState = {
interface ReviewsFocusState {
from: ReviewsFocusFrom;
lecture: Lecture | null;
reviews: Review[] | null;
}

const initialState: ReviewsFocusState = {

Check warning on line 19 in src/reducers/write-reviews/reviewsFocus.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/reviewsFocus.ts#L19

Added line #L19 was not covered by tests
from: ReviewsFocusFrom.NONE,
lecture: null,
reviews: null,
};

const reviewsFocus = (state = initialState, action) => {
const reviewsFocus = (state = initialState, action: ReviewsFocusAction) => {
switch (action.type) {
case RESET: {
return initialState;
}
case SET_REVIEWS_FOCUS: {
return Object.assign({}, state, {
return {

Check warning on line 31 in src/reducers/write-reviews/reviewsFocus.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/reviewsFocus.ts#L31

Added line #L31 was not covered by tests
...state,
from: action.from,
lecture: action.lecture,
reviews: null,
});
};
}
case CLEAR_REVIEWS_FOCUS: {
return Object.assign({}, state, {
return {

Check warning on line 39 in src/reducers/write-reviews/reviewsFocus.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/reviewsFocus.ts#L39

Added line #L39 was not covered by tests
...state,
from: ReviewsFocusFrom.NONE,
lecture: null,
reviews: null,
});
};
}
case SET_REVIEWS: {
return Object.assign({}, state, {
return {

Check warning on line 47 in src/reducers/write-reviews/reviewsFocus.ts

View check run for this annotation

Codecov / codecov/patch

src/reducers/write-reviews/reviewsFocus.ts#L47

Added line #L47 was not covered by tests
...state,
reviews: action.reviews,
});
};
}
default: {
return state;
Expand Down
Loading