import React, { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
select_last_index,
select_current_filters,
select_current_sorting,
select_current_page,
} from "../features/todo/reducer";
import { new_todo_function } from "../api/axios_methods";
import { refresh_todos } from "../refreshToDos";
function new_button(trigger_id) {
return (
);
}
function new_modal(modal_id, modal_header, modal_body, modal_footer) {
return (
{modal_header}
{modal_body}
{modal_footer}
);
}
export function NewToDo() {
const my_last_idx = useSelector(select_last_index);
const dispatch = useDispatch();
const [new_text, set_new_text] = useState("");
const [new_due_date, set_new_due_date] = useState("");
const [new_priority, set_new_priority] = useState("Low");
const my_filters = useSelector(select_current_filters);
const my_sorters = useSelector(select_current_sorting);
const my_curr_page = useSelector(select_current_page);
const new_todo_api = new_todo_function();
function handle_exit_modal() {
// https://stackoverflow.com/questions/27826381/clearing-form-input-fields-in-bootstrap
$("form").get(0).reset(); // Reset form
set_new_text("");
set_new_due_date("");
set_new_priority("Low");
}
function handle_add_todo() {
new_todo_api({
text: new_text,
due_date:
new_due_date.length == 0
? ""
: new Date(new_due_date).toISOString(),
priority: new_priority,
});
setTimeout(() => {
refresh_todos(my_filters, my_sorters, my_curr_page, dispatch);
}, 100);
handle_exit_modal();
}
// Define modal to add a new to do.
var modal_header = (
Add a new to do
);
var modal_footer = (
);
var modal_body = (
);
return (
<>
{new_button("#NewToDo")}
{new_modal("NewToDo", modal_header, modal_body, modal_footer)}
>
);
}