From 4285674ab37c74a24b265ace4d15d4848457d375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Mon, 29 May 2023 12:42:46 -0600 Subject: Pagination now works on to do's list. Only 10 to dos will be shown at a time. You can sort by priority or due date and it should work on all of our to dos. Edit and delete also works. --- src/refreshToDos.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/refreshToDos.js (limited to 'src/refreshToDos.js') diff --git a/src/refreshToDos.js b/src/refreshToDos.js new file mode 100644 index 0000000..c599606 --- /dev/null +++ b/src/refreshToDos.js @@ -0,0 +1,67 @@ +import { set_todo, empty_todos, change_page } from "./features/todo/reducer"; + +import { + set_fil_sort_function, + get_todos_page_function, + get_nu_pages_function, +} from "./api/axios_methods"; + +export function refresh_todos(my_filters, my_sorters, my_curr_page, dispatch) { + // Call the API to refresh sorters and filters. + const set_fil_sor_api = set_fil_sort_function(); + + set_fil_sor_api({ + filter: { + name: my_filters.name, + priority: my_filters.priority, + state: my_filters.state, + }, + sort: { + field: my_sorters.split("/")[0], + order: my_sorters.split("/")[1], + }, + }); + + // Get the total number of pages. Change the current page if necessary. + const get_nu_pages_api = get_nu_pages_function(); + const total_pages_api = get_nu_pages_api(); + + if (total_pages_api < my_curr_page) { + dispatch( + change_page({ + page: total_pages_api, + }) + ); + } + + // Delete all current to dos we have stored and retrieve new to dos from API. + const get_todos_api = get_todos_page_function(); + + function handle_new_todos_page(todos_list) { + dispatch(empty_todos()); + todos_list.map((todo) => { + // API stores dates in ISO format and in GMT. We have to add the + // timezone offset. + const due_date = new Date(todo.dueDate); + const offset = due_date.getTimezoneOffset(); + + dispatch( + set_todo({ + id: todo.id, + text: todo.text, + due_date: + todo.dueDate != null + ? new Date(due_date - offset * 60 * 1000) + .toISOString() + .slice(0, -1) + : "", + done: todo.done, + priority: todo.priority, + creation_date: todo.creationDate, + }) + ); + }); + } + + get_todos_api(handle_new_todos_page, { page: my_curr_page }); +} -- cgit v1.2.3