From b3d36b3b61b29a165e2784581f9786bd728706b5 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:03:55 +0530 Subject: [PATCH 01/22] Update auth_required.js --- middlewares/auth_required.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/middlewares/auth_required.js b/middlewares/auth_required.js index 7f5e7bc..b657d32 100644 --- a/middlewares/auth_required.js +++ b/middlewares/auth_required.js @@ -1,3 +1,24 @@ /*** * @todo Redirect the user to login page if token is not present. */ + import { useEffect} from 'react' + + import { useAuth } from '../context/auth' + import { useRouter } from 'next/router' + + export function authRequired(){ + + const { token } = useAuth() + + const router = useRouter() + + useEffect(()=>{ + if(token===undefined){ + router.push('/login'); + } + },[token]) + + + } ; + + From 95773ddea70cae31f5365e125632471993d090e5 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:05:15 +0530 Subject: [PATCH 02/22] Update no_auth_required.js --- middlewares/no_auth_required.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/middlewares/no_auth_required.js b/middlewares/no_auth_required.js index 82558d4..fb02b20 100644 --- a/middlewares/no_auth_required.js +++ b/middlewares/no_auth_required.js @@ -1,3 +1,21 @@ /*** * @todo Redirect the user to main page if token is present. - */ \ No newline at end of file + */ + import { useEffect, useState } from 'react' + import { useAuth } from '../context/auth' + import { useRouter } from 'next/router' + + export function noAuthRequired(){ + + const { token } = useAuth() + + const router = useRouter() + + useEffect(()=>{ + if(token){ + router.push('/'); + } + },[token]) + + + }; From 6ab611f7841581509b37e94d8625352e0da11799 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:06:02 +0530 Subject: [PATCH 03/22] Update .eslintrc --- .eslintrc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.eslintrc b/.eslintrc index a2ceebe..0b5b69d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": ["next/babel", "next/core-web-vitals"] + "extends": [ + "next", + "next/core-web-vitals" + ] } From e464281412e5bd430e22720295a9dac38de0ebc0 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:23:51 +0530 Subject: [PATCH 04/22] Update AddTask.js --- components/AddTask.js | 48 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/components/AddTask.js b/components/AddTask.js index 9652adb..44e1561 100644 --- a/components/AddTask.js +++ b/components/AddTask.js @@ -1,10 +1,44 @@ +import React, { useState} from 'react' +import axios from '../utils/axios' +import { useAuth } from "../context/auth" +import { toast, ToastContainer } from 'react-toastify'; +import 'react-toastify/dist/ReactToastify.css'; + export default function AddTask() { + const [task, setTask] = useState("") + const {token} = useAuth() + 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. - */ + + if(task==="") { + toast.warn("Don't leave task empty......") + return; + } + + toast.info('Please wait...',{position: "top-center",autoClose: 1000}) + + const dataForApiRequest = { + title: task + } + + const headersForApiRequest = { + headers: {Authorization: 'Token ' + token,} + } + + + axios.post( + '/todo/create/',dataForApiRequest,headersForApiRequest, + ) + .then(function ({ data, status }) { + setTask(""); + toast.success("Your Task has been added in the Todo Succesfully....",{position: "top-center"}) + }) + .catch(function (err) { + toast.error("Unable to add Task. Please try again ....",{position: "top-center"}) + }) + } + + } return (
@@ -12,6 +46,10 @@ export default function AddTask() { type='text' className='todo-add-task-input px-4 py-2 placeholder-blueGray-300 text-blueGray-600 bg-white rounded text-sm border border-blueGray-300 outline-none focus:outline-none focus:ring w-full' placeholder='Enter Task' + name='task' + id='task' + value={task} + onChange={(e) => setTask(e.target.value)} />
+ } ) From 9203dbb64d78394dd5286d6c4528e2af1d7ad27b Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 20:50:45 +0530 Subject: [PATCH 08/22] Update RegisterForm.js --- components/RegisterForm.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/components/RegisterForm.js b/components/RegisterForm.js index a6ef2e3..e97a7ba 100644 --- a/components/RegisterForm.js +++ b/components/RegisterForm.js @@ -1,12 +1,21 @@ import React, { useState } from 'react' import axios from '../utils/axios' import { useAuth } from '../context/auth' +import no_authRequired from '../middlewares/no_auth_required' +import { ToastContainer, toast } from 'react-toastify'; import { useRouter } from 'next/router' +import 'react-toastify/dist/ReactToastify.css'; + + export default function Register() { const { setToken } = useAuth() const router = useRouter() + + no_authRequired(); + + const [firstName, setFirstName] = useState('') const [lastName, setLastName] = useState('') const [email, setEmail] = useState('') @@ -27,11 +36,11 @@ export default function Register() { username === '' || password === '' ) { - console.log('Please fill all the fields correctly.') + toast.warn('Fill all the fields correctly...',{position: "top-center"}) return false } if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) { - console.log('Please enter a valid email address.') + toast.warn('Enter a valid email address...',{position: "top-center"}) return false } return true @@ -61,18 +70,20 @@ export default function Register() { router.push('/') }) .catch(function (err) { - console.log( + toast.error( 'An account using same email or username is already created' - ) + ,{position: "top-center"}) }) } } return ( +

Register

+ Date: Sun, 26 Jun 2022 20:59:36 +0530 Subject: [PATCH 09/22] Update TodoListItem.js --- components/TodoListItem.js | 89 +++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/components/TodoListItem.js b/components/TodoListItem.js index 7965f3b..1f87d50 100644 --- a/components/TodoListItem.js +++ b/components/TodoListItem.js @@ -1,55 +1,94 @@ -/* eslint-disable @next/next/no-img-element */ +import React, { useState} from 'react' +import axios from '../utils/axios' +import 'react-toastify/dist/ReactToastify.css'; +import { ToastContainer, toast } from 'react-toastify'; +import { useAuth } from "../context/auth" +import { useRouter } from 'next/router' -export default function TodoListItem() { + +export default function TodoListItem(props) { const editTask = (id) => { - /** - * @todo Complete this function. - * @todo 1. Update the dom accordingly - */ + document.getElementById("input-button-"+id).classList.remove("hideme"); + document.getElementById("done-button-"+id).classList.remove("hideme"); + document.getElementById("task-"+id).classList.add("hideme"); + document.getElementById("task-actions-"+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. - */ + toast.info("Please wait...",{position: "top-center"}); + + const headersForApiRequest = { + headers: {Authorization: 'Token ' + token,} + } + + + axios.delete( + 'todo/' + id + '/',headersForApiRequest, + ).then(function ({ data, status }) { + toast.success("Task has been deleted successfully.....",{position: "top-center"}) + }).catch(function (err) { + toast.error("Couldn't delete the task ..Please try again....",{position: "top-center"}) + }) + } 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 (updatetask===""){ + toast.warn("Don't Leave any empty Spaces...Please enter a task...",{position: "top-center"}); + return; + } + const dataForApiRequest={ + title: updatetask + } + + const headersForApiRequest = { + headers: {Authorization: 'Token ' + token,} +} + axios.patch( + 'todo/' + id + '/',dataForApiRequest,headersForApiRequest, + ).then(function ({ data, status }) { + document.getElementById("input-button-"+id).classList.add("hideme"); + document.getElementById("done-button-"+id).classList.add("hideme"); + document.getElementById("task-"+id).classList.remove("hideme"); + document.getElementById("task-actions-"+id).classList.remove("hideme"); + toast.success("Item in todo list Updated successfully... ",{position: "top-center"}) + }).catch(function (err) { + toast.warn("Not able to add task.Please try again.....",{position: "top-center"}); + }) + } return ( <> -
  • + +
  • setUpdatetask(e.target.value)} /> -
    +
    -
    - Sample Task 1 +
    + {props.title}
    - +
    - ) -} + ) + + } + + From 95be33b597d324fb6a4f64e35a55074884a7771f Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:35:09 +0530 Subject: [PATCH 17/22] Update index.js --- pages/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/index.js b/pages/index.js index 3df225f..766e09e 100644 --- a/pages/index.js +++ b/pages/index.js @@ -5,7 +5,7 @@ import { useEffect, useState } from 'react' import { useAuth } from '../context/auth' import axios from '../utils/axios' import { ToastContainer, toast } from 'react-toastify'; -import authRequired from '../middlewares/auth_required' +import {authRequired} from '../middlewares/auth_required' export default function Home() { From b51378db9ba921c99e10bbbb645380684a0b1076 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:36:35 +0530 Subject: [PATCH 18/22] Update LoginForm.js --- components/LoginForm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/LoginForm.js b/components/LoginForm.js index 5848ebd..fbd88ff 100644 --- a/components/LoginForm.js +++ b/components/LoginForm.js @@ -1,6 +1,6 @@ import React, { useState } from 'react' import { useAuth } from '../context/auth' -import noAuthRequired from '../middlewares/no_auth_required' +import {noAuthRequired} from '../middlewares/no_auth_required' import { useRouter } from 'next/router' import axios from '../utils/axios' import 'react-toastify/dist/ReactToastify.css'; From c58064c6c6305c26e0cb14c31a91982745d29742 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:37:09 +0530 Subject: [PATCH 19/22] Update RegisterForm.js --- components/RegisterForm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/RegisterForm.js b/components/RegisterForm.js index 606dc1b..3481012 100644 --- a/components/RegisterForm.js +++ b/components/RegisterForm.js @@ -1,7 +1,7 @@ import React, { useState } from 'react' import axios from '../utils/axios' import { useAuth } from '../context/auth' -import noAuthRequired from '../middlewares/no_auth_required' +import {noAuthRequired} from '../middlewares/no_auth_required' import { ToastContainer, toast } from 'react-toastify'; import { useRouter } from 'next/router' import 'react-toastify/dist/ReactToastify.css'; From e21b15be4e7ec844035fa656faa9578629067e06 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:41:39 +0530 Subject: [PATCH 20/22] Update TodoListItem.js --- components/TodoListItem.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/TodoListItem.js b/components/TodoListItem.js index 1f87d50..f440fcb 100644 --- a/components/TodoListItem.js +++ b/components/TodoListItem.js @@ -7,6 +7,11 @@ import { useRouter } from 'next/router' export default function TodoListItem(props) { + + const { token } = useAuth() + + const [updatetask, setUpdatetask] = useState('') + const editTask = (id) => { document.getElementById("input-button-"+id).classList.remove("hideme"); document.getElementById("done-button-"+id).classList.remove("hideme"); From a6edf726f0afbee67ae3d115f8dfbffaac4d68e4 Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:46:18 +0530 Subject: [PATCH 21/22] Update auth.js --- context/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context/auth.js b/context/auth.js index 9910f38..6370be0 100644 --- a/context/auth.js +++ b/context/auth.js @@ -1,7 +1,7 @@ import { useEffect, useState, useContext, createContext } from 'react' import axios from '../utils/axios' import { ToastContainer, toast } from 'react-toastify'; -import authRequired from '../middlewares/auth_required' +import {authRequired} from '../middlewares/auth_required' import { useRouter } from 'next/router' import 'react-toastify/dist/ReactToastify.css'; import { useCookies } from 'react-cookie' From a0b15cc015fb85bac28feaa844c5e231a95f512c Mon Sep 17 00:00:00 2001 From: Archit Pattanaik <98776672+archit-2003@users.noreply.github.com> Date: Sun, 26 Jun 2022 21:59:34 +0530 Subject: [PATCH 22/22] Update index.js --- pages/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/index.js b/pages/index.js index 766e09e..32e481d 100644 --- a/pages/index.js +++ b/pages/index.js @@ -24,6 +24,7 @@ export default function Home() { ).then(function ({ data, status }) { setTasks(data); }).catch(function (err) { + console.log("Error") }) } useEffect(()=>{