-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.ts
73 lines (67 loc) · 1.88 KB
/
slice.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createSlice } from "@reduxjs/toolkit";
import { PayloadAction } from "@reduxjs/toolkit/dist/createAction";
import { DefaultSortOrder, SortOrderTypes } from "components/Pagination";
import { POSTS_PER_PAGE } from "config";
import { PostSortOptions } from "./types";
export type PostsStateProps = {
pageNumber: number;
perPage: number;
filterTag: string;
sortOption: string;
sortOrder: SortOrderTypes;
};
const initState: PostsStateProps = {
pageNumber: 1,
perPage: POSTS_PER_PAGE,
filterTag: "-",
sortOption: PostSortOptions.defaultOption,
sortOrder: DefaultSortOrder,
};
const postsSlice = createSlice({
name: "posts",
initialState: initState,
reducers: {
setPageNumber: (state, action: PayloadAction<number>) => {
state.pageNumber = action.payload;
},
resetPageNumber: (state) => {
state.pageNumber = initState.pageNumber;
},
setPerPage: (state, action: PayloadAction<number>) => {
state.perPage = action.payload;
},
setFilterTag: (state, action: PayloadAction<string>) => {
state.filterTag = action.payload;
},
setSortOption: (state, action: PayloadAction<string>) => {
state.sortOption = action.payload;
},
setSortOrder: (state, action: PayloadAction<SortOrderTypes>) => {
state.sortOrder = action.payload;
},
toggleSortOrder: (state) => {
state.sortOrder =
state.sortOrder === SortOrderTypes.ascending
? SortOrderTypes.descending
: SortOrderTypes.ascending;
},
resetSortOrder: (state) => {
state.sortOrder = initState.sortOrder;
},
resetState: (state) => {
return initState;
},
},
});
export const {
setPageNumber,
resetPageNumber,
setPerPage,
setFilterTag,
setSortOption,
setSortOrder,
toggleSortOrder,
resetSortOrder,
resetState,
} = postsSlice.actions;
export const postsReducer = postsSlice.reducer;