Skip to content

Commit 2c887c5

Browse files
refactor collections reducers and actions using redux toolkit
1 parent ae8f517 commit 2c887c5

File tree

3 files changed

+35
-52
lines changed

3 files changed

+35
-52
lines changed

client/constants.js

-6
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ export const RESET_PROJECT = 'RESET_PROJECT';
3434
export const SET_PROJECT = 'SET_PROJECT';
3535
export const SET_PROJECTS = 'SET_PROJECTS';
3636

37-
export const SET_COLLECTIONS = 'SET_COLLECTIONS';
3837
export const CREATE_COLLECTION = 'CREATED_COLLECTION';
39-
export const DELETE_COLLECTION = 'DELETE_COLLECTION';
40-
41-
export const ADD_TO_COLLECTION = 'ADD_TO_COLLECTION';
42-
export const REMOVE_FROM_COLLECTION = 'REMOVE_FROM_COLLECTION';
43-
export const EDIT_COLLECTION = 'EDIT_COLLECTION';
4438

4539
export const DELETE_PROJECT = 'DELETE_PROJECT';
4640

client/modules/IDE/actions/collections.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import * as ActionTypes from '../../../constants';
44
import { startLoader, stopLoader } from '../reducers/loading';
55
import { setToastText, showToast } from './toast';
66

7+
import {
8+
setCollections,
9+
delCollection,
10+
updateCollection
11+
} from '../reducers/collections';
12+
713
const TOAST_DISPLAY_TIME_MS = 1500;
814

915
export function getCollections(username) {
@@ -18,10 +24,7 @@ export function getCollections(username) {
1824
return apiClient
1925
.get(url)
2026
.then((response) => {
21-
dispatch({
22-
type: ActionTypes.SET_COLLECTIONS,
23-
collections: response.data
24-
});
27+
dispatch(setCollections(response.data));
2528
dispatch(stopLoader());
2629
})
2730
.catch((error) => {
@@ -72,10 +75,7 @@ export function addToCollection(collectionId, projectId) {
7275
return apiClient
7376
.post(url)
7477
.then((response) => {
75-
dispatch({
76-
type: ActionTypes.ADD_TO_COLLECTION,
77-
payload: response.data
78-
});
78+
dispatch(updateCollection(response.data));
7979
dispatch(stopLoader());
8080

8181
const collectionName = response.data.name;
@@ -102,10 +102,7 @@ export function removeFromCollection(collectionId, projectId) {
102102
return apiClient
103103
.delete(url)
104104
.then((response) => {
105-
dispatch({
106-
type: ActionTypes.REMOVE_FROM_COLLECTION,
107-
payload: response.data
108-
});
105+
dispatch(updateCollection(response.data));
109106
dispatch(stopLoader());
110107

111108
const collectionName = response.data.name;
@@ -131,10 +128,7 @@ export function editCollection(collectionId, { name, description }) {
131128
return apiClient
132129
.patch(url, { name, description })
133130
.then((response) => {
134-
dispatch({
135-
type: ActionTypes.EDIT_COLLECTION,
136-
payload: response.data
137-
});
131+
dispatch(updateCollection(response.data));
138132
return response.data;
139133
})
140134
.catch((error) => {
@@ -152,11 +146,7 @@ export function deleteCollection(collectionId) {
152146
return apiClient
153147
.delete(url)
154148
.then((response) => {
155-
dispatch({
156-
type: ActionTypes.DELETE_COLLECTION,
157-
payload: response.data,
158-
collectionId
159-
});
149+
dispatch(deleteCollection(response.data, collectionId));
160150
return response.data;
161151
})
162152
.catch((error) => {
+24-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
import * as ActionTypes from '../../../constants';
1+
import { createSlice } from '@reduxjs/toolkit';
22

3-
const sketches = (state = [], action) => {
4-
switch (action.type) {
5-
case ActionTypes.SET_COLLECTIONS:
6-
return action.collections;
7-
8-
case ActionTypes.DELETE_COLLECTION:
9-
return state.filter(({ id }) => action.collectionId !== id);
10-
11-
// The API returns the complete new edited collection
12-
// with any items added or removed
13-
case ActionTypes.EDIT_COLLECTION:
14-
case ActionTypes.ADD_TO_COLLECTION:
15-
case ActionTypes.REMOVE_FROM_COLLECTION:
16-
return state.map((collection) => {
17-
if (collection.id === action.payload.id) {
18-
return action.payload;
19-
}
20-
21-
return collection;
22-
});
23-
default:
24-
return state;
3+
const sketchesSlice = createSlice({
4+
name: 'sketches',
5+
initialState: [],
6+
reducers: {
7+
setCollections: (state, action) => action.payload,
8+
delCollection: (state, action) => {
9+
const { collectionId } = action.payload;
10+
return state.filter((collection) => collection.id !== collectionId);
11+
},
12+
updateCollection: (state, action) => {
13+
const updatedCollection = action.payload;
14+
return state.map((collection) =>
15+
collection.id === updatedCollection.id ? updatedCollection : collection
16+
);
17+
}
2518
}
26-
};
19+
});
20+
21+
export const {
22+
setCollections,
23+
delCollection,
24+
updateCollection
25+
} = sketchesSlice.actions;
2726

28-
export default sketches;
27+
export default sketchesSlice.reducer;

0 commit comments

Comments
 (0)