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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import { createSlice } from "@reduxjs/toolkit";
export const todo_slice = createSlice({
name: "todo_list",
initialState: {
todos: [],
last_id: 0,
current_sorting: "created_time",
},
reducers: {
add_todo: (state, action) => {
state.todos = [
...state.todos,
{
id: ++state.last_id,
text: action.payload.text,
due_date: action.payload.due_date,
done: action.payload.done,
priority: action.payload.priority,
creation_date: action.payload.creation_date,
},
];
},
change_done: (state, action) => {
let selected_todo = state.todos.findIndex(
(x) => x.id == action.payload.id
);
if (selected_todo == -1) return;
state.todos[selected_todo].done = action.payload.done;
},
remove_todo: (state, action) => {
state.todos = state.todos.filter(
(todo) => todo.id != action.payload
);
},
edit_todo: (state, action) => {
let selected_todo = state.todos.findIndex(
(x) => x.id == action.payload.id
);
if (selected_todo == -1) return;
state.todos[selected_todo].text = action.payload.text;
state.todos[selected_todo].due_date = action.payload.due_date;
state.todos[selected_todo].done = action.payload.done;
state.todos[selected_todo].priority = action.payload.priority;
},
set_sort_todo: (state, action) => {
switch (action.payload.where_clicked) {
case "priority":
switch (state.current_sorting) {
case "priority-^":
state.current_sorting = "priority-v";
break;
case "priority-v":
state.current_sorting = "created_time";
break;
default:
state.current_sorting = "priority-^";
break;
}
break;
case "due_date":
switch (state.current_sorting) {
case "due-date-^":
state.current_sorting = "due-date-v";
break;
case "due-date-v":
state.current_sorting = "created_time";
break;
default:
state.current_sorting = "due-date-^";
break;
}
}
},
sort_todo: (state) => {
const priority_order = {
Low: 1,
Medium: 2,
High: 3,
};
switch (state.current_sorting) {
case "priority-^":
state.todos.sort(
(a, b) =>
priority_order[b.priority] -
priority_order[a.priority]
);
break;
case "priority-v":
state.todos.sort(
(a, b) =>
priority_order[a.priority] -
priority_order[b.priority]
);
break;
case "due-date-^":
state.todos.sort((a, b) =>
b.due_date.localeCompare(a.due_date)
);
break;
case "due-date-v":
state.todos.sort((a, b) =>
a.due_date.localeCompare(b.due_date)
);
break;
default:
state.todos.sort((a, b) =>
a.creation_date.localeCompare(b.creation_date)
);
break;
}
},
},
});
export const {
add_todo,
change_done,
remove_todo,
edit_todo,
set_sort_todo,
sort_todo,
} = todo_slice.actions;
export const select_todos = (state) => state.todo_list.todos;
export const select_last_index = (state) => state.todo_list.last_id;
export const select_current_sorting = (state) =>
state.todo_list.current_sorting;
export default todo_slice.reducer;
|