diff --git a/.eslintrc b/.eslintrc index a2ceebe..d227306 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,7 @@ { - "extends": ["next/babel", "next/core-web-vitals"] -} + "extends": ["next", "next/core-web-vitals"], + "rules": { + // Other rules + "@next/next/no-img-element": "off" + } +} \ No newline at end of file diff --git a/components/AddTask.js b/components/AddTask.js index 9652adb..8e2ce00 100644 --- a/components/AddTask.js +++ b/components/AddTask.js @@ -1,17 +1,61 @@ -export default function AddTask() { - const addTask = () => { +import { useState } from 'react' +import { useAuth } from '../context/auth' +import 'react-toastify/dist/ReactToastify.css'; +import { ToastContainer, toast } from 'react-toastify'; +import axios from '../utils/axios' + + + +export default function AddTask({getTasks}) { + const [Task, setTask] = useState('') + const {token} =useAuth() + const registerFieldsAreValid = (Task ) => { + if ( Task === '' ) { + toast.error('Title cannot be empty',{position: 'bottom-right'}) + return false + } + return true + } + const addTask = (e) => { /** * @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. */ - } + e.preventDefault() + if ( + registerFieldsAreValid(Task) + ) { + const dataForApiRequest = { + title: Task + } + + axios.post( + 'todo/create/',dataForApiRequest,{ + headers: { + Authorization: 'Token ' + token, + },} + + ) + .then(function () { + toast.success('Task created successfully',{position: 'bottom-right'}) + getTasks() + setTask('') + }) + .catch(function (err) { + toast.error('Error!!',{position: 'bottom-right'}) + }) + } + } + return (