Skip to content
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

1st commit #22

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
57 changes: 43 additions & 14 deletions components/AddTask.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
export default function AddTask() {
import { useState } from "react";
import { useAuth } from "../context/auth";
import axios from "axios";
export default function AddTask({tasks,setTasks}) {
const { token } = useAuth();
// console.log(e);
const API_BASE_URL = "https://todo-app-csoc.herokuapp.com/";
const [taskEntry, setTaskEntry] = 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.
*/
}
axios({
headers: {
Authorization: "Token " + token,
},
url: API_BASE_URL + "todo/create/",
method: "post",
data: { title: taskEntry },
})
.then(function (response) {
axios({
headers: {
Authorization: "Token " + token,
},
url: API_BASE_URL + "todo/",
method: "get",
}).then(function ({ data, status }) {
setTasks(data);
setTaskEntry("")
});
})
.catch(function (err) {
console.log(err);
console.log("Error encountered");
});
// console.log(tasks);
};
return (
<div className='flex items-center max-w-sm mt-24'>
<div className="flex items-center max-w-sm mt-24">
<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"
value={taskEntry}
onChange={(e) => setTaskEntry(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>
)
);
}
86 changes: 61 additions & 25 deletions components/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,79 @@
export default function RegisterForm() {
import { useState } from "react";
import { useRouter } from "next/router";
import { useAuth } from "../context/auth";
import axios from "axios";
import { stringify } from "postcss";

export default function LoginForm() {
const router = useRouter();
const API_BASE_URL = "https://todo-app-csoc.herokuapp.com/";
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const { token, setToken } = useAuth();
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)
*/
}
// e.preventDefault();
if (username && password) {
console.log("Entered something");

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

axios({
url: API_BASE_URL + "auth/login/",
method: "post",
data: dataForApiRequest,
})
.then(function ({ data, status }) {
setToken(data.token);
console.log(token);
router.replace("/");
})
.catch(function (err) {
console.log(
"No such account exists. Make a new account by regsitering."
);
});
} else {
console.log("Enter Something");
}
};

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>
<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"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>

<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"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>

<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>
)
);
}
87 changes: 47 additions & 40 deletions components/Nav.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
/* 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, token } = useAuth();

return (
<nav className='bg-blue-600'>
<ul className='flex items-center justify-between p-5'>
<ul className='flex items-center justify-between space-x-4'>
<nav className="bg-blue-600">
<ul className="flex items-center justify-between p-5">
<ul className="flex items-center justify-between space-x-4">
<li>
<Link href="/" passHref={true}>
<a>
<h1 className='text-white font-bold text-xl'>Todo</h1>
<h1 className="text-white font-bold text-xl">Todo</h1>
</a>
</Link>
</li>
</ul>
<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>
<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>
<svg
className='fill-current h-4 w-4'
xmlns='http://www.w3.org/2000/svg'
viewBox='0 0 20 20'
>
<path d='M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z' />
</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'
href='#'
onClick={logout}
>
Logout
</a>
</li>
</ul>
{!token && (
<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>
)}

<div className="inline-block relative w-28">
<div className="group inline-block relative">
{token && (
<>
<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>
<svg
className="fill-current h-4 w-4"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
>
<path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z" />
</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"
href="#"
onClick={logout}
>
Logout
</a>
</li>
</ul>
</>
)}
</div>
</div>
</ul>
</nav>
)
);
}
Loading