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

modified all the files needed #9

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
23 changes: 23 additions & 0 deletions components/AddTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ export default function AddTask() {
* @todo 1. Send the request to add the task to the backend server.
* @todo 2. Add the task in the dom.
*/

const inputField = document.querySelector('.todo-add-task-input');
const task = inputField.value;

// Step 1: Send the request to add the task to the backend server
// Example implementation using fetch API
fetch('/api/tasks', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ task }),
})
.then(response => response.json())
.then(data => {
// Step 2: Add the task in the DOM
const taskElement = document.createElement('div');
taskElement.textContent = data.task;
document.body.appendChild(taskElement);
})
.catch(error => {
console.error('Error:', error);
});
};

return (
Expand Down
39 changes: 39 additions & 0 deletions components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,46 @@ export default function RegisterForm() {
* @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 login = () => {
const inputUsername = document.getElementById('inputUsername');
const inputPassword = document.getElementById('inputPassword');

// Step 1: Form validation
if (inputUsername.value.trim() === '') {
alert('Please enter your username.');
return;
}

if (inputPassword.value.trim() === '') {
alert('Please enter your password.');
return;
}

// Step 2: Fetch the auth token from the backend and login the user
fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: inputUsername.value,
password: inputPassword.value,
}),
})
.then(response => response.json())
.then(data => {
// Step 3: Set the token in the context (assuming you have an AuthContext)
AuthContext.setToken(data.token);

// Optional: Perform additional actions after successful login
// For example, navigate to a different page or update the UI
})
.catch(error => {
console.error('Error:', error);
});
};
}

return (
<div className="bg-grey-lighter min-h-screen flex flex-col">
Expand Down
18 changes: 15 additions & 3 deletions components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useAuth } from "../context/auth";
*/

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

return (
<nav className="bg-blue-600">
Expand All @@ -23,13 +23,25 @@ export default function Nav() {
</li>
</ul>
<ul className="flex">
{isAuthenticated }? (

<li className="text-white mr-2">{profileName}</li>
<li className="text-white">
<a href="#" onClick={logout}>
Logout
</a>
</li>) :
(

<li className="text-white mr-2">
<Link href="/login">Login</Link>
</li>
<li className="text-white">
<Link href="/register">Register</Link>
</li>
</li>)
</ul>
{isAuthenticated && (

<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">
Expand All @@ -53,7 +65,7 @@ export default function Nav() {
</li>
</ul>
</div>
</div>
</div>)}
</ul>
</nav>
);
Expand Down
50 changes: 50 additions & 0 deletions components/TodoListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export default function TodoListItem() {
* @todo Complete this function.
* @todo 1. Update the dom accordingly
*/
const editInput = document.getElementById(`input-button-${id}`);
const doneButton = document.getElementById(`done-button-${id}`);
const taskText = document.getElementById(`task-${id}`);
const taskActions = document.getElementById(`task-actions-${id}`);

// Step 1: Update the DOM accordingly
editInput.classList.remove('hideme');
doneButton.classList.remove('hideme');
taskText.classList.add('hideme');
taskActions.classList.add('hideme');
};

const deleteTask = (id) => {
Expand All @@ -14,6 +24,19 @@ export default function TodoListItem() {
* @todo 1. Send the request to delete the task to the backend server.
* @todo 2. Remove the task from the dom.
*/
// Step 1: Send the request to delete the task to the backend server
fetch(`/api/tasks/${id}`, {
method: 'DELETE',
})
.then(response => response.json())
.then(data => {
// Step 2: Remove the task from the DOM
const taskElement = document.getElementById(`task-${id}`);
taskElement.remove();
})
.catch(error => {
console.error('Error:', error);
});
};

const updateTask = (id) => {
Expand All @@ -22,6 +45,33 @@ export default function TodoListItem() {
* @todo 1. Send the request to update the task to the backend server.
* @todo 2. Update the task in the dom.
*/
const editInput = document.getElementById(`input-button-${id}`);
const doneButton = document.getElementById(`done-button-${id}`);
const taskText = document.getElementById(`task-${id}`);
const taskActions = document.getElementById(`task-actions-${id}`);

// Step 1: Send the request to update the task to the backend server
fetch(`/api/tasks/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ task: editInput.value }),
})
.then(response => response.json())
.then(data => {
// Step 2: Update the task in the DOM
taskText.textContent = data.task;

// Step 3: Update the DOM accordingly
editInput.classList.add('hideme');
doneButton.classList.add('hideme');
taskText.classList.remove('hideme');
taskActions.classList.remove('hideme');
})
.catch(error => {
console.error('Error:', error);
});
};

return (
Expand Down
17 changes: 17 additions & 0 deletions middlewares/auth_required.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/***
* @todo Redirect the user to login page if token is not present.
*/

import { useRouter } from "next/router";
import { useAuth } from "../context/auth";

export default function TodoListItem() {
const { isAuthenticated } = useAuth();
const router = useRouter();


// Redirect to login if token is not present
if (!isAuthenticated) {
router.push("/login");
return null; // Render nothing while redirecting
}


}
15 changes: 15 additions & 0 deletions middlewares/no_auth_required.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/***
* @todo Redirect the user to main page if token is present.
*/

import { useRouter } from "next/router";
import { useAuth } from "../context/auth";

export default function TodoListItem() {
const { isAuthenticated } = useAuth();
const router = useRouter();


// Redirect to main page if token is present
if (isAuthenticated) {
router.push("/");
return null; // Render nothing while redirecting
}
}
Loading