aboutsummaryrefslogtreecommitdiff
path: root/src/ToDo-UI/NewToDo.jsx
blob: e81cb4e4d6de08f0c9863c77704cdbd7867f25cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
    add_todo,
    sort_todo,
    refresh_filtered_todos,
    select_last_index,
} from "../features/todo/reducer";

function new_button(trigger_id) {
    return (
        <div className="container mt-3 mb-3">
            <button
                type="button"
                className="btn btn-outline-dark"
                data-bs-toggle="modal"
                data-bs-target={trigger_id}
            >
                + New To Do
            </button>
        </div>
    );
}

function new_modal(modal_id, modal_header, modal_body, modal_footer) {
    return (
        <div className="modal fade" id={modal_id} tabIndex="-1" role="dialog">
            <div className="modal-dialog modal-dialog-centered" role="document">
                <div className="modal-content">
                    {modal_header}
                    {modal_body}
                    {modal_footer}
                </div>
            </div>
        </div>
    );
}

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");

    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() {
        dispatch(
            add_todo({
                text: new_text,
                due_date: new_due_date,
                priority: new_priority,
                creation_date: new Date().toString(),
            })
        );
        dispatch(sort_todo());
        dispatch(refresh_filtered_todos());
        handle_exit_modal();
    }

    // Define modal to add a new to do.
    var modal_header = (
        <div className="modal-header">
            <h5 className="modal-title">Add a new to do</h5>
            <button
                type="button"
                className="close"
                data-bs-dismiss="modal"
                aria-label="Close new to do window"
                onClick={handle_exit_modal}
            >
                <span>&times;</span>
            </button>
        </div>
    );
    var modal_footer = (
        <div className="modal-footer">
            <button
                type="button"
                className="btn btn-secondary"
                data-bs-dismiss="modal"
                onClick={handle_exit_modal}
            >
                Cancel
            </button>
            <button
                type="button"
                className="btn btn-primary"
                onClick={handle_add_todo}
                data-bs-dismiss="modal"
            >
                Add
            </button>
        </div>
    );
    var modal_body = (
        <div className="modal-body">
            <form>
                <div className="form-floating mb-3">
                    <input
                        className="form-control"
                        id="new-todo-id"
                        value={my_last_idx + 1}
                        disabled
                    />
                    <label htmlFor="new-todo-id">ID</label>
                </div>
                <div className="form-floating mb-3">
                    <input
                        className="form-control"
                        placeholder="Text"
                        id="new-todo-text"
                        onChange={(e) => set_new_text(e.target.value)}
                    />
                    <label htmlFor="new-todo-text">Text</label>
                </div>
                <div className="input-group mb-3">
                    <div className="form-floating">
                        <input
                            className="form-control"
                            type="datetime-local"
                            id="new-todo-due-date"
                            placeholder="Due date"
                            onChange={(e) => {
                                set_new_due_date(e.target.value);
                            }}
                        />
                        <label htmlFor="new-todo-due-date">Due Date</label>
                    </div>
                </div>
                <div className="form-floating mb-3">
                    <select
                        className="form-select"
                        id="new-todo-priority"
                        onChange={(e) => set_new_priority(e.target.value)}
                    >
                        <option value="Low" defaultValue>
                            Low
                        </option>
                        <option value="Medium">Medium</option>
                        <option value="High">High</option>
                    </select>
                    <label htmlFor="new-todo-priority">Priority</label>
                </div>
            </form>
        </div>
    );

    return (
        <>
            {new_button("#NewToDo")}
            {new_modal("NewToDo", modal_header, modal_body, modal_footer)}
        </>
    );
}