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-projects-reducers-actions-rtk #3123

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ export const API_KEY_CREATED = 'API_KEY_CREATED';
export const API_KEY_REMOVED = 'API_KEY_REMOVED';

export const SET_PROJECT_NAME = 'SET_PROJECT_NAME';
export const RENAME_PROJECT = 'RENAME_PROJECT';

export const PROJECT_SAVE_SUCCESS = 'PROJECT_SAVE_SUCCESS';
export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL';
export const NEW_PROJECT = 'NEW_PROJECT';
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';
Expand All @@ -42,8 +40,6 @@ 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';

export const SET_SELECTED_FILE = 'SET_SELECTED_FILE';
export const SHOW_MODAL = 'SHOW_MODAL';
export const HIDE_MODAL = 'HIDE_MODAL';
Expand Down
7 changes: 3 additions & 4 deletions client/modules/IDE/actions/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import apiClient from '../../../utils/apiClient';
import * as ActionTypes from '../../../constants';
import { startLoader, stopLoader } from '../reducers/loading';

import { setProjects } from '../reducers/projects';

// eslint-disable-next-line
export function getProjects(username) {
return (dispatch) => {
Expand All @@ -15,10 +17,7 @@ export function getProjects(username) {
return apiClient
.get(url)
.then((response) => {
dispatch({
type: ActionTypes.SET_PROJECTS,
projects: response.data
});
dispatch(setProjects(response.data));
dispatch(stopLoader());
})
.catch((error) => {
Expand Down
42 changes: 24 additions & 18 deletions client/modules/IDE/reducers/projects.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import * as ActionTypes from '../../../constants';
import { createSlice } from '@reduxjs/toolkit';

const sketches = (state = [], action) => {
switch (action.type) {
case ActionTypes.SET_PROJECTS:
return action.projects;
case ActionTypes.DELETE_PROJECT:
return state.filter((sketch) => sketch.id !== action.id);
case ActionTypes.RENAME_PROJECT: {
return state.map((sketch) => {
if (sketch.id === action.payload.id) {
return { ...sketch, name: action.payload.name };
}
return { ...sketch };
});
const sketchesSlice = createSlice({
name: 'sketches',
initialState: [],
reducers: {
setProjects(state, action) {
return action.payload;
},
deleteProject(state, action) {
return state.filter((sketch) => sketch.id !== action.payload.id);
},
renameProject(state, action) {
const { id, name } = action.payload;
return state.map((sketch) =>
sketch.id === id ? { ...sketch, name } : { ...sketch }
);
}
default:
return state;
}
};
});

export default sketches;
export const {
setProjects,
deleteProject,
renameProject
} = sketchesSlice.actions;

export default sketchesSlice.reducer;
Loading