import React, { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; import { change_done, remove_todo, edit_todo, set_sort_todo, select_todos, select_current_filters, select_current_sorting, select_current_page, } from "../features/todo/reducer"; import { edit_todo_function, remove_todo_function, set_done_function, set_undone_function, } from "../api/axios_methods"; import { refresh_todos } from "../refreshToDos"; function sort_table_header(prefix, current_sorting) { if (prefix.toLowerCase().startsWith(current_sorting.substr(0, 3))) { switch (current_sorting.substr(-1)) { case "^": // Write Prefix and an arrow pointing up. return ( <> {prefix} ); case "v": // Write Prefix and an arrow pointing down. return ( <> {prefix} ); } } else { // Write Prefix and four dots. No sorting. return ( <> {prefix} ); } } function list_of_todos(edit_button, delete_button) { const dispatch = useDispatch(); const my_todos = useSelector(select_todos); const my_sorters = useSelector(select_current_sorting); function handle_sort_todos(where_clicked) { dispatch( set_sort_todo({ where_clicked: where_clicked, }) ); } const set_done_api = set_done_function(); const set_undone_api = set_undone_function(); const date_options = { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }; // Table contents var table_head = ( Done Name handle_sort_todos("priority")}> {sort_table_header("Priority", my_sorters)} { handle_sort_todos("due_date"); }} > {sort_table_header("Due Date", my_sorters)} Actions ); var table_body = ( {my_todos.map((item) => (
{ e.target.checked ? set_done_api({ id: item.id }) : set_undone_api({ id: item.id }); dispatch( change_done({ id: item.id, done: e.target.checked, }) ); }} >
{item.text} {item.priority} {item.due_date.length == 0 ? "" : new Date(item.due_date).toLocaleString( undefined, // <- Select locale of computer. date_options )}
{edit_button(item)} {delete_button(item)}
))} ); return (
{table_head} {table_body}
); } function new_modal(modal_id, modal_header, modal_body, modal_footer) { return ( ); } export function ListToDos() { const dispatch = useDispatch(); const [edit_id, set_edit_id] = useState(-1); const [edit_text, set_edit_text] = useState(""); const [edit_due_date, set_edit_due_date] = useState(""); const [edit_priority, set_edit_priority] = useState("Low"); const edit_todo_api = edit_todo_function(); const remove_todo_api = remove_todo_function(); function handle_open_modal(id, text, due_date, done, priority) { set_edit_id(id); set_edit_text(text); if (due_date.length == 0) { set_edit_due_date(""); } else { due_date = new Date(due_date); const offset = due_date.getTimezoneOffset(); set_edit_due_date( new Date(due_date - offset * 60 * 1000) // Ignore the timezone offset. .toISOString() // Convert to ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) .slice(0, -1) // Get rid of the Z at the end. ); } set_edit_priority(priority); } function handle_exit_modal() { // https://stackoverflow.com/questions/27826381/clearing-form-input-fields-in-bootstrap $("form").get(0).reset(); // Reset form set_edit_id(-1); set_edit_text(""); set_edit_priority("Low"); } function handle_edit_todo() { edit_todo_api({ id: edit_id, text: edit_text, due_date: edit_due_date.length == 0 ? new Date(0) : new Date(edit_due_date).toISOString(), priority: edit_priority, }); dispatch( edit_todo({ id: edit_id, text: edit_text, due_date: edit_due_date.length == 0 ? "" : new Date(edit_due_date).toISOString(), priority: edit_priority, }) ); handle_exit_modal(); } // Define Edit and Remove buttons function edit_button(item) { return ( ); } function delete_button(item) { return ( ); } // Define modal to edit a to do. var modal_header = (
Edit a to do
); var modal_footer = (
); var modal_body = (
set_edit_text(e.target.value)} />
{ set_edit_due_date(e.target.value); }} />
); return ( <> {list_of_todos(edit_button, delete_button)} {new_modal("EditToDo", modal_header, modal_body, modal_footer)} ); }