Skip to content

CSO-Week3 React Task Done #22

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 18 commits 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
48 changes: 43 additions & 5 deletions components/AddTask.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import TodoListItem from '../components/TodoListItem'
import { useEffect, useState } from 'react'
import axios from '../utils/axios'
import { useAuth } from '../context/auth'
import { API_URL }from '../utils/constants'
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

export function displaySuccessToast(message) {
toast.success(message);
}
export function displayWarnToast(message) {
toast.warn(message);
}

export function displayErrorToast(message) {
toast.error(message);
}
export default function AddTask() {
const {token} = useAuth();
const [task, setTask] = useState("")
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==""){
displayWarnToast("Task is empty!!");
}
else{
axios({
headers: {
Authorization: 'Token ' + token
},
url: API_URL + "todo/create/",
method: 'POST',
data: {
title: task
},
}).then(res => {
displaySuccessToast("Task added!!!");
setTask("");
props.displayTasks();

}).catch(function (err) {
})
}
}
return (
<div className='flex items-center max-w-sm mt-24'>
<input
onChange={(e)=>setTask(e.target.value)}
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'
value={task}
/>
<button
type='button'
Expand Down
73 changes: 67 additions & 6 deletions components/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,90 @@
import React from "react";
import axios from '../utils/axios'
import { useAuth } from '../context/auth'
import { useRouter } from 'next/router'
import noAuthRequired from "../middlewares/no_auth_required";
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

const BASE_URL = "todo-app-csoc.herokuapp.com/" ;

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

const [loginData, setLoginData] = React.useState({
inputUsername: "",
inputPassword: ""
})

function handleChange(event){
const {name, value} = event.target;
setLoginData(prev => {
return {
...prev,
[name] : value
}
})
}

noAuthRequired();

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 userData = {
username: loginData.inputUsername,
password: loginData.inputPassword
}
if(userData.username==="" || userData.password===""){
toast.warn("Enter username or password",{position: "top-center"});
return;
}

axios({
url: "auth/login/",
method: 'POST',
data: userData,
}).then(res => {
toast.success("Login Successfull!!!",{position: "top-center"});
const authToken = res.data.token;
setToken(authToken)
router.push('/error', '/');

}).catch(function (err) {
// console.log(err);
toast.error("Enter correct Username Or Password!!!",{position: "top-center"});
setLoginData({
inputUsername: "",
inputPassword: ""
})
})

}

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>
<input
onChange={handleChange}
type='text'
className='block border border-grey-light w-full p-3 rounded mb-4'
name='inputUsername'
id='inputUsername'
placeholder='Username'
value={loginData.inputUsername}
/>

<input
onChange={handleChange}
type='password'
className='block border border-grey-light w-full p-3 rounded mb-4'
name='inputPassword'
id='inputPassword'
placeholder='Password'
value={loginData.inputPassword}
/>

<button
Expand All @@ -39,5 +97,8 @@ export default function RegisterForm() {
</div>
</div>
</div>
<ToastContainer/>
</>
)
}

24 changes: 12 additions & 12 deletions components/Nav.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/* eslint-disable jsx-a11y/alt-text */
/* eslint-disable @next/next/no-img-element */
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, token } = useAuth()

return (
<nav className='bg-blue-600'>
Expand All @@ -22,19 +17,22 @@ export default function Nav() {
</Link>
</li>
</ul>
{token===undefined ?
<ul className='flex'>
<li className='text-white mr-2'>
<Link href='/login'>Login</Link>
</li>

<li className='text-white'>
<Link href='/register'>Register</Link>
</li>
</ul>
</li>
</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 @@ -44,6 +42,7 @@ export default function Nav() {
</svg>
</button>
<ul className='absolute hidden text-gray-700 pt-1 group-hover:block'>

<li className=''>
<a
className='rounded-b bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap'
Expand All @@ -53,9 +52,10 @@ export default function Nav() {
Logout
</a>
</li>

</ul>
</div>
</div>
</div>}
</ul>
</nav>
)
Expand Down
15 changes: 10 additions & 5 deletions components/RegisterForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { useState } from 'react'
import axios from '../utils/axios'
import { useAuth } from '../context/auth'
import { useRouter } from 'next/router'
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

export default function Register() {
const { setToken } = useAuth()
Expand All @@ -27,11 +29,11 @@ export default function Register() {
username === '' ||
password === ''
) {
console.log('Please fill all the fields correctly.')
toast.warn("Please 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("Please enter a valid email address.",{position: "top-center"})
return false
}
return true
Expand All @@ -43,7 +45,7 @@ export default function Register() {
if (
registerFieldsAreValid(firstName, lastName, email, username, password)
) {
console.log('Please wait...')
toast.info('Please wait...',{position: "top-center"})

const dataForApiRequest = {
name: firstName + ' ' + lastName,
Expand All @@ -61,14 +63,16 @@ export default function Register() {
router.push('/')
})
.catch(function (err) {
console.log(
'An account using same email or username is already created'
toast.error(
"An account using same email or username is already created",{position: "top-center"}
)
})
}
}

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 +136,6 @@ export default function Register() {
</div>
</div>
</div>
</>
)
}
Loading