-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (129 loc) · 4.7 KB
/
index.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
import React from "react";
import TodoListItem from "../components/TodoListItem";
import AddTask from "../components/AddTask";
import { useEffect, useState } from "react";
import axios from "../utils/axios";
import { useAuth } from "../context/auth";
import { API_URL } from "../utils/constants";
import { auth_required } from "../middlewares/auth_required";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import RestartAltIcon from "@mui/icons-material/RestartAlt";
import SortIcon from "@mui/icons-material/Sort";
export default function Home() {
const { token } = useAuth();
const [tasks, setTasks] = useState([]);
const [searchTask, setSearchTask] = useState("");
const [sortType, setSortType] = useState("");
auth_required();
useEffect(() => {
if (token != undefined) getTasks();
}, [tasks]);
const resetSearch = () => {
setSearchTask("");
};
function getTasks() {
/***
* @todo Fetch the tasks created by the user.
* @todo Set the tasks state and display them in the using TodoListItem component
* The user token can be accessed from the context using useAuth() from /context/auth.js
*/
let newTasks = [];
axios({
headers: {
Authorization: "Token " + token,
},
url: API_URL + "todo/",
method: "GET",
})
.then(function ({ data, status }) {
for (let i = 0; i < data.length; i++) {
if (data[i].title.toLowerCase().includes(searchTask.toLowerCase())) {
newTasks.push(data[i]);
}
}
//setTasks(newTasks);
if (sortType == 0) {
newTasks.reverse();
}
setTasks(newTasks);
})
.catch((err) => {
console.log("Error:" + err);
toast.error("Error: " + err);
});
}
const setSortMode = (sortMode) => {
setSortType(sortMode);
};
return (
<div>
<div>
<center>
<AddTask renderTasks={getTasks} />
<div className="py-4 min-w-full">
<input
onChange={(e) => setSearchTask(e.target.value)}
type="text"
className="todo-add-task-input px-4 py-2 placeholder-blueGray-300 dark:placeholder-gray-100 text-blueGray-600 dark:bg-gray-400 dark:text-white bg-white text-sm rounded border border-gray-300 outline-none focus:outline-none focus:ring w-full "
placeholder="Search here.."
value={searchTask}
/>
<button
type="button"
className="todo-add-task bg-transparent dark:bg-green-700 dark:text-white hover:bg-green-500 text-green-700 text-sm hover:text-white px-3 py-2 border border-green-500 hover:border-transparent rounded"
onClick={resetSearch}
>
<RestartAltIcon className="text-base" /> Reset
</button>
</div>
<button
type="button"
className="todo-add-task bg-transparent m-1 dark:bg-green-700 dark:text-white hover:bg-green-500 text-green-700 text-sm hover:text-white px-3 py-2 border border-green-500 hover:border-transparent "
onClick={() => setSortMode(0)}
>
<SortIcon className="text-base rotate-icons" /> Newest First
</button>
<button
type="button"
className="todo-add-task bg-transparent m-1 dark:bg-green-700 dark:text-white hover:bg-green-500 text-green-700 text-sm hover:text-white px-3 py-2 border border-green-500 hover:border-transparent "
onClick={() => setSortMode(1)}
>
<SortIcon className="text-base" /> Oldest First
</button>
{tasks.length == 0 ? (
<div className=" text-gray-400 py-2 text-sm">No To-Do Found!</div>
) : (
<div>
<ul className="flex-col mt-9 max-w-sm mb-3 ">
<span className="inline-block bg-blue-600 py-1 mb-2 px-9 text-sm text-white font-bold rounded-full ">
Available Tasks
</span>
<div className="max-h-80 task-container">
{tasks.map((task) => {
return (
<TodoListItem
id={task.id}
key={task.id}
task={task.title}
renderTasks={getTasks}
/>
);
})}
</div>
</ul>
</div>
)}
</center>
</div>
<ToastContainer
position="bottom-right"
theme="light"
autoClose={3000}
pauseOnHover={false}
newestOnTop={false}
closeOnClick
/>
</div>
);
}