Skip to content

Added changes #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions components/AddTask.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
export default function AddTask() {
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(props) {
const { token } = useAuth();
const [addtask, setAddtask] = useState("");

const addTaskFieldsAreValid = (addtask) => {
if (addtask === "") {
toast.warn("Please fill a Task....", { position: "top-center" });
return false;
}
return true;
};
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 (addTaskFieldsAreValid(addtask)) {
toast.info("Please wait...", { position: "top-center", autoClose: 1000 });

const dataForApiRequest = {
title: addtask,
};

const headersForApiRequest = {
headers: { Authorization: "Token " + token },
};

axios
.post("/todo/create/", dataForApiRequest, headersForApiRequest)
.then(function ({ data, status }) {
setAddtask("");
toast.success("Task Added in the Todo Succesfully....", {
position: "top-center",
});
})
.catch(function (err) {
toast.error("Task Not Added in the Todo ....", {
position: "top-center",
});
});
}
};
return (
<div className='flex items-center max-w-sm mt-24'>
<div className="flex items-center max-w-sm mt-24">
<ToastContainer />
<input
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'
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="addTask"
id="addTask"
value={addtask}
onChange={(e) => setAddtask(e.target.value)}
/>
<button
type='button'
className='todo-add-task bg-transparent hover:bg-green-500 text-green-700 text-sm hover:text-white px-3 py-2 border border-green-500 hover:border-transparent rounded'
type="button"
className="todo-add-task bg-transparent 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={addTask}
>
Add Task
</button>
</div>
)
);
}
98 changes: 73 additions & 25 deletions components/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,91 @@
export default function RegisterForm() {
import React, { useState } from "react";
import axios from "../utils/axios";
import { useAuth } from "../context/auth";
import { useRouter } from "next/router";
import no_authRequired from "../middlewares/no_auth_required";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

export default function LoginForm() {
const { setToken } = useAuth();
const router = useRouter();

no_authRequired();

const [password, setPassword] = useState("");
const [username, setUsername] = useState("");

const loginFieldsAreValid = (username, password) => {
if (username === "" || password === "") {
toast.warn("Please fill all the fields correctly....", {
position: "top-center",
});
return false;
}
return true;
};
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)
*/
}
if (loginFieldsAreValid(username, password)) {
console.log("Please wait...");

const dataForApiRequest = {
username: username,
password: password,
};

axios
.post("auth/login/", dataForApiRequest)
.then(function ({ data, status }) {
setToken(data.token);
toast.success("Logged in Successfully....", {
position: "top-center",
autoClose: 2000,
});
setTimeout(() => router.reload(), 2000);
})
.catch(function (err) {
toast.error("Incorrect Username or Password,Try again ....", {
position: "top-center",
});
});
}
};

return (
<div className='bg-grey-lighter min-h-screen flex flex-col'>
<div className='container max-w-sm mx-auto flex-1 flex flex-col items-center justify-center px-2'>
<div className='bg-white px-6 py-8 rounded shadow-md text-black w-full'>
<h1 className='mb-8 text-3xl text-center'>Login</h1>
<div className="bg-grey-lighter min-h-screen flex flex-col">
<div className="container max-w-sm mx-auto flex-1 flex flex-col items-center justify-center px-2">
<div className="bg-white px-6 py-8 rounded shadow-md text-black w-full">
<h1 className="mb-8 text-3xl text-center"> Login</h1>
<ToastContainer />
<input
type='text'
className='block border border-grey-light w-full p-3 rounded mb-4'
name='inputUsername'
id='inputUsername'
placeholder='Username'
type="text"
className="block border border-grey-light w-full p-3 rounded mb-4"
name="inputUsername"
id="inputUsername"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Username"
/>

<input
type='password'
className='block border border-grey-light w-full p-3 rounded mb-4'
name='inputPassword'
id='inputPassword'
placeholder='Password'
type="password"
className="block border border-grey-light w-full p-3 rounded mb-4"
name="inputPassword"
id="inputPassword"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>

<button
type='submit'
className='w-full text-center py-3 rounded bg-transparent text-green-500 hover:text-white hover:bg-green-500 border border-green-500 hover:border-transparent focus:outline-none my-1'
type="submit"
className="w-full text-center py-3 rounded bg-transparent text-green-500 hover:text-white hover:bg-green-500 border border-green-500 hover:border-transparent focus:outline-none my-1"
onClick={login}
>
Login
</button>
</div>
</div>
</div>
)
);
}
18 changes: 11 additions & 7 deletions components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
/* eslint-disable @next/next/no-img-element */
import Link from 'next/link'
import { useAuth } from '../context/auth'
import 'react-toastify/dist/ReactToastify.css';

/**
*
* @todo Condtionally render login/register and Profile name in NavBar
*/

export default function Nav() {
const { logout, profileName, avatarImage } = useAuth()
const { logout, profileName, avatarImage,token } = useAuth()

return (
<nav className='bg-blue-600'>
<nav className='bg-blue-600' id='navBar'>
<ul className='flex items-center justify-between p-5'>
<ul className='flex items-center justify-between space-x-4'>
<li>
Expand All @@ -22,19 +24,20 @@ export default function Nav() {
</Link>
</li>
</ul>
{token===undefined?
<ul className='flex'>
<li className='text-white mr-2'>
<li className='text-white mr-2' id='btn1'>
<Link href='/login'>Login</Link>
</li>
<li className='text-white'>
<li className='text-white' id='btn2'>
<Link href='/register'>Register</Link>
</li>
</ul>
</ul>:
<div className='inline-block relative w-28'>
<div className='group inline-block relative'>
<button className='bg-gray-300 text-gray-700 font-semibold py-2 px-4 rounded inline-flex items-center'>
<img src={avatarImage} />
<span className='mr-1'>{profileName}</span>
<img src={token!==undefined?avatarImage:null} />
<span className='mr-1'>{token!==undefined?profileName:null}</span>
<svg
className='fill-current h-4 w-4'
xmlns='http://www.w3.org/2000/svg'
Expand All @@ -56,6 +59,7 @@ export default function Nav() {
</ul>
</div>
</div>
}
</ul>
</nav>
)
Expand Down
21 changes: 16 additions & 5 deletions components/RegisterForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import axios from '../utils/axios'
import { useAuth } from '../context/auth'
import { useRouter } from 'next/router'

import no_authRequired from '../middlewares/no_auth_required'
import { ToastContainer, toast } from 'react-toastify';
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('')
Expand All @@ -27,11 +33,11 @@ export default function Register() {
username === '' ||
password === ''
) {
console.log('Please fill all the fields correctly.')
toast.warn('Please fill all the fields correctly.',{position: "bottom-right"})
return false
}
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
console.log('Please enter a valid email address.')
toast.warn('Please enter a valid email address.',{position: "bottom-right"})
return false
}
return true
Expand All @@ -58,17 +64,21 @@ export default function Register() {
)
.then(function ({ data, status }) {
setToken(data.token)
router.push('/')
toast.success("Registered Successfully...",{position: "top-center",autoClose: 2000})
setTimeout(()=>router.push('/'),2000)
})
.catch(function (err) {
console.log(
toast.error(
'An account using same email or username is already created'
)
,{position: "bottom-right"})
})
}
}

return (
<>

<ToastContainer />
<div className='bg-grey-lighter min-h-screen flex flex-col'>
<div className='container max-w-sm mx-auto flex-1 flex flex-col items-center justify-center px-2'>
<div className='bg-white px-6 py-8 rounded shadow-md text-black w-full'>
Expand Down Expand Up @@ -132,5 +142,6 @@ export default function Register() {
</div>
</div>
</div>
</>
)
}
Loading