Skip to content

Commit

Permalink
refactor collections reducers and actions using redux toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
PiyushChandra17 committed May 16, 2024
1 parent ae8f517 commit 2c887c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 52 deletions.
6 changes: 0 additions & 6 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ export const RESET_PROJECT = 'RESET_PROJECT';
export const SET_PROJECT = 'SET_PROJECT';
export const SET_PROJECTS = 'SET_PROJECTS';

export const SET_COLLECTIONS = 'SET_COLLECTIONS';
export const CREATE_COLLECTION = 'CREATED_COLLECTION';
export const DELETE_COLLECTION = 'DELETE_COLLECTION';

export const ADD_TO_COLLECTION = 'ADD_TO_COLLECTION';
export const REMOVE_FROM_COLLECTION = 'REMOVE_FROM_COLLECTION';
export const EDIT_COLLECTION = 'EDIT_COLLECTION';

export const DELETE_PROJECT = 'DELETE_PROJECT';

Expand Down
32 changes: 11 additions & 21 deletions client/modules/IDE/actions/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import * as ActionTypes from '../../../constants';
import { startLoader, stopLoader } from '../reducers/loading';
import { setToastText, showToast } from './toast';

import {
setCollections,
delCollection,

Check warning on line 9 in client/modules/IDE/actions/collections.js

View workflow job for this annotation

GitHub Actions / Test and lint code base

'delCollection' is defined but never used
updateCollection
} from '../reducers/collections';

const TOAST_DISPLAY_TIME_MS = 1500;

export function getCollections(username) {
Expand All @@ -18,10 +24,7 @@ export function getCollections(username) {
return apiClient
.get(url)
.then((response) => {
dispatch({
type: ActionTypes.SET_COLLECTIONS,
collections: response.data
});
dispatch(setCollections(response.data));
dispatch(stopLoader());
})
.catch((error) => {
Expand Down Expand Up @@ -72,10 +75,7 @@ export function addToCollection(collectionId, projectId) {
return apiClient
.post(url)
.then((response) => {
dispatch({
type: ActionTypes.ADD_TO_COLLECTION,
payload: response.data
});
dispatch(updateCollection(response.data));
dispatch(stopLoader());

const collectionName = response.data.name;
Expand All @@ -102,10 +102,7 @@ export function removeFromCollection(collectionId, projectId) {
return apiClient
.delete(url)
.then((response) => {
dispatch({
type: ActionTypes.REMOVE_FROM_COLLECTION,
payload: response.data
});
dispatch(updateCollection(response.data));
dispatch(stopLoader());

const collectionName = response.data.name;
Expand All @@ -131,10 +128,7 @@ export function editCollection(collectionId, { name, description }) {
return apiClient
.patch(url, { name, description })
.then((response) => {
dispatch({
type: ActionTypes.EDIT_COLLECTION,
payload: response.data
});
dispatch(updateCollection(response.data));
return response.data;
})
.catch((error) => {
Expand All @@ -152,11 +146,7 @@ export function deleteCollection(collectionId) {
return apiClient
.delete(url)
.then((response) => {
dispatch({
type: ActionTypes.DELETE_COLLECTION,
payload: response.data,
collectionId
});
dispatch(deleteCollection(response.data, collectionId));
return response.data;
})
.catch((error) => {
Expand Down
49 changes: 24 additions & 25 deletions client/modules/IDE/reducers/collections.js
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;

0 comments on commit 2c887c5

Please sign in to comment.