diff --git a/assets/js/sign-in.js b/assets/js/sign-in.js index 74042bd..ec6db1d 100644 --- a/assets/js/sign-in.js +++ b/assets/js/sign-in.js @@ -20,11 +20,6 @@ const setTooltip = (type, message) => { tooltip.style.display = "block"; } -const removeTooltip = () => { - tooltip.classList.remove('visible'); - tooltip.style.display = "none"; -} - const signIn = async (username, password) => { const res = fetch(`${url}/user/login`, { method: "POST", diff --git a/assets/js/user.js b/assets/js/user.js index 8765a58..794b704 100644 --- a/assets/js/user.js +++ b/assets/js/user.js @@ -1,3 +1,6 @@ +const url = "https://task-api.fstropii.com"; +const localhostUrl = "http://localhost:8000"; + // Check if the user is logged in const getCookie = (returnType) => { let decodedCookie = decodeURIComponent(document.cookie); @@ -31,4 +34,91 @@ if (token == undefined) { window.location.href = '/'; } -console.log('user token'); \ No newline at end of file +const setTooltip = (type, message) => { + tooltip.classList.add(type); + tooltip.classList.add('visible'); + tooltip.innerText = message + tooltip.style.display = "block"; +} + + +// loading user tasks +const loadUserTasks = () => { + const tasksPromise = getUserTasks(token); + tasksPromise.then((res) => res.json()).then((json => { + const tasks = json.tasks + if (json.tasks == null) { + return; + } + + for (let i = 0; i < tasks.length; i++) { + const taskItemDiv = document.createElement('div'); + taskItemDiv.className = 'task-item'; + + createTaskItem(tasks[i]['title'], tasks[i]['is_completed'], i); + } + + const checkboxes = document.querySelectorAll('.checkbox'); + + for (let j = 0; j < checkboxes.length; j++) { + checkboxes[j].addEventListener("click", function() { + setTooltip("info", `Updating task '${tasks[j]['title']}'`); + const updateTaskPromise = updateTask(token, tasks[j]['title'], tasks[j]['description'], checkboxes[j].checked); + updateTaskPromise.then((res => { + setTooltip("success", `Task '${tasks[j]['title']}' updated!`); + })); + }) + } + })); +} + +const getUserTasks = (userToken) => { + const res = fetch(`${localhostUrl}/users/me`, { + headers: {Authorization: `Bearer ${userToken}`} + }); + + return res; +} + +const createTaskItem = (title, isCompleted, index) => { + const userTasksDiv = document.querySelector('.user-tasks'); + const taskItemDiv = document.createElement('div'); + taskItemDiv.className = 'task-item'; + + const taskCheckbox = document.createElement('input'); + taskCheckbox.className = `checkbox-${index} checkbox`; + taskCheckbox.type = 'checkbox'; + + if (isCompleted == true) { + taskCheckbox.checked = true; + } + + const taskLabel = document.createElement('li'); + taskLabel.innerHTML = title; + taskLabel.className = `task-${index}`; + + + taskItemDiv.appendChild(taskCheckbox); + taskItemDiv.appendChild(taskLabel); + userTasksDiv.appendChild(taskItemDiv); +} + +const updateTask = (userToken, taskTitle, taskDescription, completedStatus) => { + const body = JSON.stringify({ + "title": taskTitle, + "description": taskDescription, + "is_completed": completedStatus + }); + + console.log(body); + console.log(taskDescription); + const res = fetch(`${localhostUrl}/user/update-tasks`, { + method: "PATCH", + headers: {Authorization: `Bearer ${userToken}`, "Content-type": "application/json"}, + body: body + }); + + return res; +} + +loadUserTasks(); \ No newline at end of file diff --git a/assets/pages/user.html b/assets/pages/user.html index f9b95f9..590f47c 100644 --- a/assets/pages/user.html +++ b/assets/pages/user.html @@ -27,19 +27,10 @@