-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port ReviewEntries to use redux-toolkit (#2800)
- Loading branch information
1 parent
3b56ee7
commit 26a0414
Showing
13 changed files
with
232 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,38 @@ | ||
import { | ||
defaultState, | ||
ReviewEntriesAction, | ||
ReviewEntriesActionTypes, | ||
ReviewEntriesState, | ||
} from "goals/ReviewEntries/Redux/ReviewEntriesReduxTypes"; | ||
import { StoreAction, StoreActionTypes } from "rootActions"; | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
export const reviewEntriesReducer = ( | ||
state: ReviewEntriesState = defaultState, //createStore() calls each reducer with undefined state | ||
action: ReviewEntriesAction | StoreAction | ||
): ReviewEntriesState => { | ||
switch (action.type) { | ||
case ReviewEntriesActionTypes.SortBy: | ||
// Change which column is being sorted by | ||
return { ...state, sortBy: action.sortBy }; | ||
import { defaultState } from "goals/ReviewEntries/Redux/ReviewEntriesReduxTypes"; | ||
import { StoreActionTypes } from "rootActions"; | ||
|
||
case ReviewEntriesActionTypes.UpdateAllWords: | ||
// Update the local words | ||
return { ...state, words: action.words }; | ||
const reviewEntriesSlice = createSlice({ | ||
name: "reviewEntriesState", | ||
initialState: defaultState, | ||
reducers: { | ||
deleteWordAction: (state, action) => { | ||
state.words = state.words.filter((w) => w.id !== action.payload); | ||
}, | ||
resetReviewEntriesAction: () => defaultState, | ||
setAllWordsAction: (state, action) => { | ||
state.words = action.payload; | ||
}, | ||
setSortByAction: (state, action) => { | ||
state.sortBy = action.payload; | ||
}, | ||
updateWordAction: (state, action) => { | ||
state.words = state.words.map((w) => | ||
w.id === action.payload.oldId ? action.payload.updatedWord : w | ||
); | ||
}, | ||
}, | ||
extraReducers: (builder) => | ||
builder.addCase(StoreActionTypes.RESET, () => defaultState), | ||
}); | ||
|
||
case ReviewEntriesActionTypes.UpdateWord: | ||
// Update the word of specified id | ||
return { | ||
...state, | ||
words: state.words.map((w) => | ||
w.id === action.oldId ? { ...action.updatedWord } : w | ||
), | ||
}; | ||
export const { | ||
deleteWordAction, | ||
resetReviewEntriesAction, | ||
setAllWordsAction, | ||
setSortByAction, | ||
updateWordAction, | ||
} = reviewEntriesSlice.actions; | ||
|
||
case ReviewEntriesActionTypes.ClearReviewEntriesState: | ||
return defaultState; | ||
|
||
case StoreActionTypes.RESET: | ||
return defaultState; | ||
|
||
default: | ||
return state; | ||
} | ||
}; | ||
export default reviewEntriesSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,12 @@ | ||
import { | ||
ColumnId, | ||
ReviewEntriesWord, | ||
} from "goals/ReviewEntries/ReviewEntriesTypes"; | ||
|
||
export enum ReviewEntriesActionTypes { | ||
SortBy = "SORT_BY", | ||
UpdateAllWords = "UPDATE_ALL_WORDS", | ||
UpdateWord = "UPDATE_WORD", | ||
ClearReviewEntriesState = "CLEAR_REVIEW_ENTRIES_STATE", | ||
} | ||
|
||
export interface ReviewSortBy { | ||
type: ReviewEntriesActionTypes.SortBy; | ||
sortBy?: ColumnId; | ||
} | ||
|
||
export interface ReviewUpdateWords { | ||
type: ReviewEntriesActionTypes.UpdateAllWords; | ||
words: ReviewEntriesWord[]; | ||
} | ||
|
||
export interface ReviewUpdateWord { | ||
type: ReviewEntriesActionTypes.UpdateWord; | ||
oldId: string; | ||
updatedWord: ReviewEntriesWord; | ||
} | ||
|
||
export interface ReviewClearReviewEntriesState { | ||
type: ReviewEntriesActionTypes.ClearReviewEntriesState; | ||
} | ||
|
||
export type ReviewEntriesAction = | ||
| ReviewSortBy | ||
| ReviewUpdateWords | ||
| ReviewUpdateWord | ||
| ReviewClearReviewEntriesState; | ||
import { Word } from "api/models"; | ||
import { ColumnId } from "goals/ReviewEntries/ReviewEntriesTypes"; | ||
|
||
export interface ReviewEntriesState { | ||
words: ReviewEntriesWord[]; | ||
isRecording: boolean; | ||
words: Word[]; | ||
sortBy?: ColumnId; | ||
wordBeingRecorded?: string; | ||
} | ||
|
||
export const defaultState: ReviewEntriesState = { | ||
words: [], | ||
isRecording: false, | ||
sortBy: undefined, | ||
wordBeingRecorded: undefined, | ||
}; |
Oops, something went wrong.