-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsorting.js
35 lines (31 loc) · 1.01 KB
/
sorting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { createSlice } from '@reduxjs/toolkit';
import { DIRECTION } from '../actions/sorting';
const initialState = {
field: 'createdAt',
direction: DIRECTION.DESC
};
const sortingSlice = createSlice({
name: 'sorting',
initialState,
reducers: {
toggleDirectionForField: (state, action) => {
const { field } = action.payload;
if (field && field !== state.field) {
const direction = field === 'name' ? DIRECTION.ASC : DIRECTION.DESC;
return { ...state, field, direction };
}
const direction =
state.direction === DIRECTION.ASC ? DIRECTION.DESC : DIRECTION.ASC;
return { ...state, direction };
},
setSorting: {
reducer: (state, action) => {
const { field, direction } = action.payload;
return { ...state, field, direction };
},
prepare: (field, direction) => ({ payload: { field, direction } })
}
}
});
export const { toggleDirectionForField, setSorting } = sortingSlice.actions;
export default sortingSlice.reducer;