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

refactor collections reducers and actions using redux toolkit #3125

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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,
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(delCollection(response.data, collectionId));
PiyushChandra17 marked this conversation as resolved.
Show resolved Hide resolved
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;
Loading