/
store.js
50 lines (45 loc) · 1.42 KB
/
store.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* A simple redux store/actions/reducer implementation.
* A true app would be more complex and separated into different files.
*/
import { configureStore, createSlice } from "@reduxjs/toolkit";
/*
* The initial state of our store when the app loads.
* Usually, you would fetch this from a server.
*/
const defaultTasks = [
{ id: "1", title: "Something", state: "TASK_INBOX" },
{ id: "2", title: "Something more", state: "TASK_INBOX" },
{ id: "3", title: "Something else", state: "TASK_INBOX" },
{ id: "4", title: "Something again", state: "TASK_INBOX" },
];
/*
* The store is created here.
* You can read more about Redux Toolkit's slices in the docs:
* https://redux-toolkit.js.org/api/createSlice
*/
const TasksSlice = createSlice({
name: "tasks",
initialState: defaultTasks,
reducers: {
updateTaskState: (state, action) => {
const { id, newTaskState } = action.payload;
const task = state.findIndex((task) => task.id === id);
if (task >= 0) {
state[task].state = newTaskState;
}
},
},
});
// The actions contained in the slice are exported for usage in our components
export const { updateTaskState } = TasksSlice.actions;
/*
* Our app's store configuration goes here.
* Read more about Redux's configureStore in the docs:
* https://redux-toolkit.js.org/api/configureStore
*/
const store = configureStore({
reducer: {
tasks: TasksSlice.reducer,
},
});
export default store;