-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor collections reducers and actions using redux toolkit
- Loading branch information
1 parent
ae8f517
commit 2c887c5
Showing
3 changed files
with
35 additions
and
52 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,28 +1,27 @@ | ||
import * as ActionTypes from '../../../constants'; | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const sketches = (state = [], action) => { | ||
switch (action.type) { | ||
case ActionTypes.SET_COLLECTIONS: | ||
return action.collections; | ||
|
||
case ActionTypes.DELETE_COLLECTION: | ||
return state.filter(({ id }) => action.collectionId !== id); | ||
|
||
// The API returns the complete new edited collection | ||
// with any items added or removed | ||
case ActionTypes.EDIT_COLLECTION: | ||
case ActionTypes.ADD_TO_COLLECTION: | ||
case ActionTypes.REMOVE_FROM_COLLECTION: | ||
return state.map((collection) => { | ||
if (collection.id === action.payload.id) { | ||
return action.payload; | ||
} | ||
|
||
return collection; | ||
}); | ||
default: | ||
return state; | ||
const sketchesSlice = createSlice({ | ||
name: 'sketches', | ||
initialState: [], | ||
reducers: { | ||
setCollections: (state, action) => action.payload, | ||
delCollection: (state, action) => { | ||
const { collectionId } = action.payload; | ||
return state.filter((collection) => collection.id !== collectionId); | ||
}, | ||
updateCollection: (state, action) => { | ||
const updatedCollection = action.payload; | ||
return state.map((collection) => | ||
collection.id === updatedCollection.id ? updatedCollection : collection | ||
); | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
export const { | ||
setCollections, | ||
delCollection, | ||
updateCollection | ||
} = sketchesSlice.actions; | ||
|
||
export default sketches; | ||
export default sketchesSlice.reducer; |