Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor search && sorting reducers && actions using redux toolkit
  • Loading branch information
PiyushChandra17 committed May 16, 2024
commit 331889e71924ccb2f29e3173fb4d3afceabbaf8a
3 changes: 0 additions & 3 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ export const CLEAR_PERSISTED_STATE = 'CLEAR_PERSISTED_STATE';
export const HIDE_RUNTIME_ERROR_WARNING = 'HIDE_RUNTIME_ERROR_WARNING';
export const SHOW_RUNTIME_ERROR_WARNING = 'SHOW_RUNTIME_ERROR_WARNING';

export const TOGGLE_DIRECTION = 'TOGGLE_DIRECTION';
export const SET_SORTING = 'SET_SORTING';
export const SET_SORT_PARAMS = 'SET_SORT_PARAMS';
export const SET_SEARCH_TERM = 'SET_SEARCH_TERM';
export const CLOSE_SKETCHLIST_MODAL = 'CLOSE_SKETCHLIST_MODAL';

export const START_SAVING_PROJECT = 'START_SAVING_PROJECT';
Expand Down
20 changes: 3 additions & 17 deletions client/modules/IDE/actions/sorting.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import * as ActionTypes from '../../../constants';
import { sortingActions } from '../reducers/sorting';

export const { toggleDirection, setSorting } = sortingActions;

export const DIRECTION = {
ASC: 'ASCENDING',
DESC: 'DESCENDING'
};

export function setSorting(field, direction) {
return {
type: ActionTypes.SET_SORTING,
payload: {
field,
direction
}
};
}

export function resetSorting() {
return setSorting('createdAt', DIRECTION.DESC);
}

export function toggleDirectionForField(field) {
return {
type: ActionTypes.TOGGLE_DIRECTION,
field
};
}

export function setSearchTerm(scope, searchTerm) {
return {
type: ActionTypes.SET_SEARCH_TERM,
Expand Down
25 changes: 17 additions & 8 deletions client/modules/IDE/reducers/search.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import * as ActionTypes from '../../../constants';
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
collectionSearchTerm: '',
sketchSearchTerm: ''
};

export default (state = initialState, action) => {
switch (action.type) {
case ActionTypes.SET_SEARCH_TERM:
return { ...state, [`${action.scope}SearchTerm`]: action.query };
default:
return state;
const searchSlice = createSlice({
name: 'search',
initialState,
reducers: {
setSearchTerm: (state, action) => {
const { scope, query } = action.payload;
return {
...state,
[`${scope}SearchTerm`]: query
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to do ...state stuff when using Redux Toolkit. The reducer uses a proxy of the actual state so it is okay to mutate it. You can return a new state to replace the entire state, but typically you just modify the state and don't return anything. It can be written as:

setSearchTerm: (state, action) => {
  const { scope, query } = action.payload;
  state[`${scope}SearchTerm`] = query;
}

}
}
};
});

export const { setSearchTerm } = searchSlice.actions;

export default searchSlice.reducer;
46 changes: 23 additions & 23 deletions client/modules/IDE/reducers/sorting.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import * as ActionTypes from '../../../constants';
import { createSlice } from '@reduxjs/toolkit';
import { DIRECTION } from '../actions/sorting';
import * as ActionTypes from '../../../constants';

const initialState = {
field: 'createdAt',
direction: DIRECTION.DESC
};

const sorting = (state = initialState, action) => {
switch (action.type) {
case ActionTypes.TOGGLE_DIRECTION:
if (action.field && action.field !== state.field) {
if (action.field === 'name') {
return { ...state, field: action.field, direction: DIRECTION.ASC };
}
return { ...state, field: action.field, direction: DIRECTION.DESC };
}
if (state.direction === DIRECTION.ASC) {
return { ...state, direction: DIRECTION.DESC };
const sortingSlice = createSlice({
name: 'sorting',
initialState,
reducers: {
toggleDirection: (state, action) => {
const { field } = action.payload;
if (field && field !== state.field) {
const direction = field === 'name' ? DIRECTION.ASC : DIRECTION.DESC;
return { ...state, field, direction };
}
return { ...state, direction: DIRECTION.ASC };
case ActionTypes.SET_SORTING:
return {
...state,
field: action.payload.field,
direction: action.payload.direction
};
default:
return state;
const direction =
state.direction === DIRECTION.ASC ? DIRECTION.DESC : DIRECTION.ASC;
return { ...state, direction };
},
setSorting: (state, action) => {
const { field, direction } = action.payload;
return { ...state, field, direction };
}
}
};
});

export const sortingActions = sortingSlice.actions;

export default sorting;
export default sortingSlice.reducer;