-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoListItem.js
153 lines (142 loc) · 5.11 KB
/
TodoListItem.js
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
/* eslint-disable @next/next/no-img-element */
import React, { useState } from "react";
import { useAuth } from "../context/auth";
import axios from "../utils/axios";
import { API_URL } from "../utils/constants";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import useDarkMode from "../pages/useDarkMode";
import DeleteIcon from "@mui/icons-material/Delete";
import ModeEditIcon from "@mui/icons-material/ModeEdit";
export default function TodoListItem(props) {
const [task, setTask] = useState(props.task);
const { token } = useAuth();
const [darkTheme, setDarkTheme] = useDarkMode();
const inputId = "input-button-" + props.id;
const doneId = "done-button-" + props.id;
const taskId = "task-" + props.id;
const taskActionsId = "task-actions-" + props.id;
const editTask = (id) => {
/**
* @todo Complete this function.
* @todo 1. Update the dom accordingly
*/
document.getElementById("done-button-" + id).classList.remove("hideme");
document.getElementById("input-button-" + id).classList.remove("hideme");
document.getElementById("task-actions-" + id).classList.add("hideme");
document.getElementById("task-" + id).classList.add("hideme");
};
const deleteTask = (id) => {
/**
* @todo Complete this function.
* @todo 1. Send the request to delete the task to the backend server.
* @todo 2. Remove the task from the dom.
*/
axios({
headers: {
Authorization: "Token " + token,
},
url: API_URL + "todo/" + id + "/",
method: "DELETE",
})
.then(() => {
toast.success("Task deleted successfully!");
props.renderTasks();
})
.catch((err) => {
toast.error("Something went wrong!");
});
};
const updateTask = (id) => {
/**
* @todo Complete this function.
* @todo 1. Send the request to update the task to the backend server.
* @todo 2. Update the task in the dom.
*/
if (task === "") {
toast.warn("Task title cannot be empty!");
} else {
axios({
headers: {
Authorization: "Token " + token,
},
url: API_URL + "todo/" + id + "/",
method: "PUT",
data: {
title: task,
},
})
.then(() => {
toast.success("Todo has been successfully updated...");
document
.getElementById("input-button-" + props.id)
.classList.add("hideme");
document
.getElementById("done-button-" + props.id)
.classList.add("hideme");
document
.getElementById("task-" + props.id)
.classList.remove("hideme");
document
.getElementById("task-actions-" + props.id)
.classList.remove("hideme");
props.renderTasks();
})
.catch(function (err) {
toast.error("Something went wrong!");
});
}
};
return (
<>
<li className="border dark:bg-gray-700 flex border-gray-500 rounded px-2 py-2 justify-between items-center mb-2 mobile-width-adjust">
<input
onChange={(e) => setTask(e.target.value)}
id={inputId}
type="text"
className="hideme appearance-none dark:bg-gray-500 rounded w-full py-2 px-3 dark:text-gray-100 text-gray-700 leading-tight focus:outline-none focus:ring todo-edit-task-input"
placeholder="Edit The Task"
value={task}
/>
<div id={doneId} className="hideme">
<button
className="bg-transparent dark:bg-green-600 dark:hover:bg-green-700 dark:text-gray-50 hover:bg-gray-500 text-gray-700 text-sm hover:text-white py-2 px-3 border border-gray-500 hover:border-transparent rounded todo-update-task"
type="button"
onClick={() => updateTask(props.id)}
>
Done
</button>
</div>
<div
id={taskId}
className="todo-task dark:text-gray-100 text-gray-600 "
>
{task}
</div>
<span id={taskActionsId} className="">
<button
style={{ marginRight: "5px" }}
type="button"
onClick={() => editTask(props.id)}
className="bg-transparent h-auto dark:text-white dark:bg-yellow-600 dark:hover:bg-yellow-400 hover:bg-yellow-500 hover:text-white border border-yellow-500 hover:border-transparent rounded px-2 py-2"
>
<ModeEditIcon fontSize="small" />
</button>
<button
type="button"
className="bg-transparent h-auto dark:text-white dark:bg-red-600 dark:hover:bg-red-400 text-black hover:bg-red-500 hover:text-white border border-red-500 hover:border-transparent rounded px-2 py-2"
onClick={() => deleteTask(props.id)}
>
<DeleteIcon fontSize="small" />
{/* <img
src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486661/CSOC/delete.svg"
width="18px"
height="22px"
alt="Delete"
/> */}
</button>
</span>
</li>
</>
);
}