diff --git a/.eslintrc b/.eslintrc index a2ceebe..dffe840 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,3 @@ { - "extends": ["next/babel", "next/core-web-vitals"] + "extends": ["next"] } diff --git a/components/AddTask.js b/components/AddTask.js index 9652adb..50eed94 100644 --- a/components/AddTask.js +++ b/components/AddTask.js @@ -1,17 +1,87 @@ -export default function AddTask() { - const addTask = () => { - /** - * @todo Complete this function. - * @todo 1. Send the request to add the task to the backend server. - * @todo 2. Add the task in the dom. - */ +import axios from "../utils/axios" +import { useState } from "react" +import { useAuth } from "../context/auth" +import {toast} from "react-toastify" +import 'react-toastify/dist/ReactToastify.css'; + +export default function AddTask({tasks, allTasks, setTasks, setAllTasks, query}) { + const {token} = useAuth() + const [_title, setTitle] = useState('') + let adding = false + const addTask = async () => { + if (adding) return + if (_title === '') { + toast.error('Task title cannot be empty', { + position: "bottom-right", + autoClose: 2000, + hideProgressBar: true, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "colored", + }) + return + } + adding = true + toast.info('Adding task', { + position: "bottom-right", + autoClose: 500, + hideProgressBar: true, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "colored", + }) + await axios({ + method: 'POST', + url: 'todo/create/', + headers: {Authorization: `Token ${token}`}, + data: {title: _title} + }).catch( () => { + toast.error('Something went wrong', { + position: "bottom-right", + autoClose: 2000, + hideProgressBar: true, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "colored", + }); + return + }) + toast.success('Task added successfully!', { + position: "bottom-right", + autoClose: 2000, + hideProgressBar: true, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "colored", + }); + setTitle('') + const newTask = await axios({ + method: 'GET', + url: 'todo/', + headers: {Authorization: `token ${token}`} + }).then(res => res.data.pop()) + + if (newTask.title.toLowerCase().indexOf(query) !== -1) setTasks([...tasks, newTask]) + setAllTasks([...allTasks, newTask]) + adding = false } + return ( -
+
setTitle(e.target.value)} />