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 common #67

Merged
merged 4 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
4 changes: 2 additions & 2 deletions src/actions/common/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import GeneralTrack from '@/shapes/model/graduation/GeneralTrack';
import MajorTrack from '@/shapes/model/graduation/MajorTrack';
import AdditionalTrack from '@/shapes/model/graduation/AdditionalTrack';

interface TracksProps {
export interface Tracks {
general: GeneralTrack[];
major: MajorTrack[];
additional: AdditionalTrack[];
}

export function setTracks(tracks: TracksProps) {
export function setTracks(tracks: Tracks) {
return {
type: SET_TRACKS,
tracks: tracks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const CombinedReducer = combineReducers({
media: media,
});

export type CommonState = ReturnType<typeof CombinedReducer>;
export default CombinedReducer;
18 changes: 0 additions & 18 deletions src/reducers/common/media.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/reducers/common/media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MediaAction, SET_IS_PORTRAIT } from '../../actions/common/media';

interface MediaState {
isPortrait: boolean;
}

const initialState: MediaState = {
isPortrait: false,
};

const media = (state = initialState, action: MediaAction): MediaState => {
switch (action.type) {
case SET_IS_PORTRAIT:
return { ...state, isPortrait: action.isPortrait };
default:
return state;
}
};

export default media;
18 changes: 0 additions & 18 deletions src/reducers/common/semester.js

This file was deleted.

21 changes: 21 additions & 0 deletions src/reducers/common/semester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SET_SEMESTERS, SemesterAction } from '../../actions/common/semester';
import Semester from '@/shapes/model/subject/Semester';

interface SemesterState {
semesters: Semester[] | null;
}

const initialState: SemesterState = {
semesters: null,
};

const semester = (state = initialState, action: SemesterAction): SemesterState => {
switch (action.type) {
case SET_SEMESTERS:
return { ...state, semesters: action.semesters };
default:
return state;
}
};

export default semester;
18 changes: 0 additions & 18 deletions src/reducers/common/track.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/reducers/common/track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SET_TRACKS, TrackAction, Tracks } from '../../actions/common/track';

interface TrackState {
tracks: Tracks | null;
}

const initialState: TrackState = {
tracks: null,
};

const track = (state = initialState, action: TrackAction): TrackState => {
switch (action.type) {
case SET_TRACKS:
return { ...state, tracks: action.tracks };
default:
return state;
}
};

export default track;
29 changes: 15 additions & 14 deletions src/reducers/common/user.js → src/reducers/common/user.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { SET_USER, UPDATE_USER_REVIEW } from '../../actions/common/user';
import User from '@/shapes/model/session/User';
import { SET_USER, UPDATE_USER_REVIEW, UserAction } from '../../actions/common/user';

const initialState = {
user: undefined,
interface UserState {
user: User | null;
}

const initialState: UserState = {
user: null,
};

export const reducer = (state = initialState, action) => {
const user = (state = initialState, action: UserAction): UserState => {
switch (action.type) {
case SET_USER:
return Object.assign({}, state, {
user: action.user,
});
return { ...state, user: action.user };
case UPDATE_USER_REVIEW: {
if (!state.user) {
return state;
}
const originalReviews = state.user.reviews;
const { review } = action;
const foundIndex = originalReviews.findIndex((r) => r.id === review.id);
Expand All @@ -22,16 +28,11 @@ export const reducer = (state = initialState, action) => {
...originalReviews.slice(foundIndex + 1, originalReviews.length),
]
: [...originalReviews.slice(), review];
return Object.assign({}, state, {
user: {
...state.user,
reviews: newReviews,
},
});
return { ...state, user: { ...state.user, reviews: newReviews } };
}
default:
return state;
}
};

export default reducer;
export default user;
Loading