reducer.js (6082B) - raw


      1 import { createSlice } from "@reduxjs/toolkit";
      2 
      3 export const todo_slice = createSlice({
      4     name: "todo_list",
      5     initialState: {
      6         last_id: 0,
      7         todos: [],
      8 
      9         current_sorting: "created_time",
     10 
     11         filtered_todos: [],
     12         current_filters: {
     13             name: "",
     14             priority: "All",
     15             state: "All",
     16         },
     17     },
     18 
     19     reducers: {
     20         add_todo: (state, action) => {
     21             state.todos = [
     22                 ...state.todos,
     23                 {
     24                     id: ++state.last_id,
     25                     text: action.payload.text,
     26                     due_date: action.payload.due_date,
     27                     done: action.payload.done,
     28                     priority: action.payload.priority,
     29                     creation_date: action.payload.creation_date,
     30                 },
     31             ];
     32         },
     33 
     34         change_done: (state, action) => {
     35             let selected_todo = state.todos.findIndex(
     36                 (x) => x.id == action.payload.id
     37             );
     38             if (selected_todo == -1) return;
     39 
     40             state.todos[selected_todo].done = action.payload.done;
     41         },
     42 
     43         remove_todo: (state, action) => {
     44             state.todos = state.todos.filter(
     45                 (todo) => todo.id != action.payload
     46             );
     47         },
     48 
     49         edit_todo: (state, action) => {
     50             let selected_todo = state.todos.findIndex(
     51                 (x) => x.id == action.payload.id
     52             );
     53             if (selected_todo == -1) return;
     54 
     55             state.todos[selected_todo].text = action.payload.text;
     56             state.todos[selected_todo].due_date = action.payload.due_date;
     57             state.todos[selected_todo].done = action.payload.done;
     58             state.todos[selected_todo].priority = action.payload.priority;
     59         },
     60 
     61         set_sort_todo: (state, action) => {
     62             switch (action.payload.where_clicked) {
     63                 case "priority":
     64                     switch (state.current_sorting) {
     65                         case "priority-^":
     66                             state.current_sorting = "priority-v";
     67                             break;
     68 
     69                         case "priority-v":
     70                             state.current_sorting = "created_time";
     71                             break;
     72 
     73                         default:
     74                             state.current_sorting = "priority-^";
     75                             break;
     76                     }
     77                     break;
     78 
     79                 case "due_date":
     80                     switch (state.current_sorting) {
     81                         case "due-date-^":
     82                             state.current_sorting = "due-date-v";
     83                             break;
     84 
     85                         case "due-date-v":
     86                             state.current_sorting = "created_time";
     87                             break;
     88 
     89                         default:
     90                             state.current_sorting = "due-date-^";
     91                             break;
     92                     }
     93             }
     94         },
     95 
     96         sort_todo: (state) => {
     97             const priority_order = {
     98                 Low: 1,
     99                 Medium: 2,
    100                 High: 3,
    101             };
    102             switch (state.current_sorting) {
    103                 case "priority-^":
    104                     state.todos.sort(
    105                         (a, b) =>
    106                             priority_order[b.priority] -
    107                             priority_order[a.priority]
    108                     );
    109                     break;
    110 
    111                 case "priority-v":
    112                     state.todos.sort(
    113                         (a, b) =>
    114                             priority_order[a.priority] -
    115                             priority_order[b.priority]
    116                     );
    117                     break;
    118 
    119                 case "due-date-^":
    120                     state.todos.sort((a, b) =>
    121                         b.due_date.localeCompare(a.due_date)
    122                     );
    123                     break;
    124 
    125                 case "due-date-v":
    126                     state.todos.sort((a, b) =>
    127                         a.due_date.localeCompare(b.due_date)
    128                     );
    129                     break;
    130 
    131                 default:
    132                     state.todos.sort((a, b) =>
    133                         a.creation_date.localeCompare(b.creation_date)
    134                     );
    135                     break;
    136             }
    137         },
    138 
    139         set_filters: (state, action) => {
    140             state.current_filters = {
    141                 name: action.payload.name,
    142                 priority: action.payload.priority,
    143                 state: action.payload.state, // True is "1" and False is "0".
    144             };
    145         },
    146 
    147         refresh_filtered_todos: (state) => {
    148             state.filtered_todos = [...state.todos];
    149 
    150             // If name, filter by names.
    151             if (state.current_filters.name.length != 0) {
    152                 state.filtered_todos = state.filtered_todos.filter((todo) =>
    153                     todo.text.includes(state.current_filters.name)
    154                 );
    155             }
    156 
    157             // If priority != all, filter by priorities.
    158             if (state.current_filters.priority != "All") {
    159                 state.filtered_todos = state.filtered_todos.filter(
    160                     (todo) => todo.priority === state.current_filters.priority
    161                 );
    162             }
    163 
    164             // If state != All, filter by current state.
    165             if (state.current_filters.state != "All") {
    166                 state.filtered_todos = state.filtered_todos.filter(
    167                     (todo) => todo.done == state.current_filters.state
    168                 );
    169             }
    170         },
    171     },
    172 });
    173 
    174 export const {
    175     add_todo,
    176     change_done,
    177     remove_todo,
    178     edit_todo,
    179     set_sort_todo,
    180     sort_todo,
    181     set_filters,
    182     refresh_filtered_todos,
    183 } = todo_slice.actions;
    184 
    185 export const select_todos = (state) => state.todo_list.filtered_todos;
    186 export const select_last_index = (state) => state.todo_list.last_id;
    187 export const select_current_sorting = (state) =>
    188     state.todo_list.current_sorting;
    189 
    190 export default todo_slice.reducer;