diff --git a/components/AddTask.js b/components/AddTask.js index 9652adb..8fa8f0c 100644 --- a/components/AddTask.js +++ b/components/AddTask.js @@ -1,25 +1,50 @@ +import axios from "axios"; +import { useState } from "react"; +import { useAuth } from "../context/auth"; export default function AddTask() { + const [task, setTask] = useState(""); + const { token1, bool, setBool } = useAuth(); const addTask = () => { + let data = { + title: task, + }; + axios + .post("https://todo-app-csoc.herokuapp.com/todo/create/", data, { + headers: { + Authorization: "Token " + token1, + }, + }) + .then((res) => { + setBool(!bool); + setTask(null); + }) + .catch((e) => { + console.log(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. */ - } + }; return ( -
+
{ + setTask(e.target.value); + }} />
- ) + ); } diff --git a/components/LoginForm.js b/components/LoginForm.js index fa28f9e..5ede4aa 100644 --- a/components/LoginForm.js +++ b/components/LoginForm.js @@ -1,37 +1,72 @@ +import React, { useState } from "react"; +import axios from "../utils/axios"; +import { useAuth } from "../context/auth"; +import router, { useRouter } from "next/router"; + export default function RegisterForm() { - const login = () => { - /*** - * @todo Complete this function. - * @todo 1. Write code for form validation. - * @todo 2. Fetch the auth token from backend and login the user. - * @todo 3. Set the token in the context (See context/auth.js) - */ - } + const [password, setPassword] = useState(""); + const [username, setUsername] = useState(""); + const { setToken, setToken1 } = useAuth(); + const login = (e) => { + e.preventDefault(); + // console.log("Yes"); + if (username != "" && password != "") { + axios + .post("https://todo-app-csoc.herokuapp.com/auth/login/", { + username: username, + password: password, + }) + .then(({ data }) => { + console.log(data.token); + setToken(data.token); + setToken1(data.token); + router.push("/"); + }) + .catch((e) => { + console.log(e); + alert("Something went wrong"); + }); + /*** + * @todo Complete this function. + * @todo 1. Write code for form validation. + * @todo 2. Fetch the auth token from backend and login the user. + * @todo 3. Set the token in the context (See context/auth.js) + */ + } + }; return ( -
-
-
-

Login

+
+
+
+

Login

{ + setUsername(e.target.value); + }} + value={username} /> { + setPassword(e.target.value); + }} + value={password} />
- ) + ); } diff --git a/components/Nav.js b/components/Nav.js index e03cb0f..aa07bfb 100644 --- a/components/Nav.js +++ b/components/Nav.js @@ -1,53 +1,55 @@ /* eslint-disable jsx-a11y/alt-text */ /* eslint-disable @next/next/no-img-element */ -import Link from 'next/link' -import { useAuth } from '../context/auth' +import Link from "next/link"; +import { useAuth } from "../context/auth"; /** * * @todo Condtionally render login/register and Profile name in NavBar */ export default function Nav() { - const { logout, profileName, avatarImage } = useAuth() + const { logout, profileName, avatarImage, token1 } = useAuth(); return ( -