aboutsummaryrefslogtreecommitdiff
path: root/src/features/todo/reducer.js
blob: 210fb053bbdc78cc61d17be7db488d75d8d6158d (plain) (blame)
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
148
149
150
151
152
import { createSlice } from "@reduxjs/toolkit";

export const todo_slice = createSlice({
    name: "todo_list",
    initialState: {
        last_id: 0,
        todos: [],

        current_sorting: "Id/ASC",

        filtered_todos: [],
        current_filters: {
            name: "",
            priority: "All",
            state: "All",
        },
    },

    reducers: {
        set_last_id: (state, action) => {
            state.last_id = action.payload.id;
        },

        add_todo: (state, action) => {
            state.todos = [
                ...state.todos,
                {
                    id: ++state.last_id,
                    text: action.payload.text,
                    due_date: action.payload.due_date,
                    done: false,
                    priority: action.payload.priority,
                    creation_date: action.payload.creation_date,
                },
            ];
        },

        set_todo: (state, action) => {
            let selected_todo = state.todos.findIndex(
                (x) => x.id == action.payload.id
            );
            if (selected_todo == -1) {
                state.todos = [
                    ...state.todos,
                    {
                        id: action.payload.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,
                    },
                ];
                state.last_id++;
            } else {
                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;
            }
        },

        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].priority = action.payload.priority;
        },

        set_sort_todo: (state, action) => {
            switch (action.payload.where_clicked) {
                case "priority":
                    switch (state.current_sorting) {
                        case "Priority/DESC":
                            state.current_sorting = "Priority/ASC";
                            break;

                        case "Priority/ASC":
                            state.current_sorting = "Id/ASC";
                            break;

                        default:
                            state.current_sorting = "Priority/DESC";
                            break;
                    }
                    break;

                case "due_date":
                    switch (state.current_sorting) {
                        case "DueDate/DESC":
                            state.current_sorting = "DueDate/ASC";
                            break;

                        case "DueDate/ASC":
                            state.current_sorting = "Id/ASC";
                            break;

                        default:
                            state.current_sorting = "DueDate/DESC";
                            break;
                    }
            }
        },

        set_filters: (state, action) => {
            state.current_filters = {
                name: action.payload.name,
                priority: action.payload.priority,
                state: action.payload.state,
            };
        },
    },
});

export const {
    set_last_id,
    add_todo,
    set_todo,
    change_done,
    remove_todo,
    edit_todo,
    set_sort_todo,
    set_filters,
} = 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 const select_current_filters = (state) =>
    state.todo_list.current_filters;

export default todo_slice.reducer;