diff options
author | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-21 19:09:15 -0600 |
---|---|---|
committer | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-21 19:09:15 -0600 |
commit | c5b628f17da1bfa17251ecf82fcb146751954b72 (patch) | |
tree | 041f3665fc8b60734649364245edee72fb18de61 /src/features/todo | |
parent | f3852c1f5bea14e72e200389759f123b880ccfec (diff) | |
download | ToDo-App-FE-c5b628f17da1bfa17251ecf82fcb146751954b72.tar.gz ToDo-App-FE-c5b628f17da1bfa17251ecf82fcb146751954b72.zip |
First attempt at filtering to do's.
We have a separate list from the original `todos_list`. It saves all currently
filtered to do's for the listing to be as easy as possible.
Diffstat (limited to '')
-rw-r--r-- | src/features/todo/reducer.js | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/src/features/todo/reducer.js b/src/features/todo/reducer.js index 5fafb6d..960ab97 100644 --- a/src/features/todo/reducer.js +++ b/src/features/todo/reducer.js @@ -3,9 +3,17 @@ import { createSlice } from "@reduxjs/toolkit"; export const todo_slice = createSlice({ name: "todo_list", initialState: { - todos: [], last_id: 0, + todos: [], + current_sorting: "created_time", + + filtered_todos: [], + current_filters: { + name: "", + priority: "All", + state: "All", + }, }, reducers: { @@ -127,6 +135,39 @@ export const todo_slice = createSlice({ break; } }, + + set_filters: (state, action) => { + state.current_filters = { + name: action.payload.name, + priority: action.payload.priority, + state: action.payload.state, // True is "1" and False is "0". + }; + }, + + refresh_filtered_todos: (state) => { + state.filtered_todos = [...state.todos]; + + // If name, filter by names. + if (state.current_filters.name.length != 0) { + state.filtered_todos = state.filtered_todos.filter((todo) => + todo.text.includes(state.current_filters.name) + ); + } + + // If priority != all, filter by priorities. + if (state.current_filters.priority != "All") { + state.filtered_todos = state.filtered_todos.filter( + (todo) => todo.priority === state.current_filters.priority + ); + } + + // If state != All, filter by current state. + if (state.current_filters.state != "All") { + state.filtered_todos = state.filtered_todos.filter( + (todo) => todo.done == state.current_filters.state + ); + } + }, }, }); @@ -137,9 +178,11 @@ export const { edit_todo, set_sort_todo, sort_todo, + set_filters, + refresh_filtered_todos, } = todo_slice.actions; -export const select_todos = (state) => state.todo_list.todos; +export const select_todos = (state) => state.todo_list.filtered_todos; export const select_last_index = (state) => state.todo_list.last_id; export const select_current_sorting = (state) => state.todo_list.current_sorting; |