diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 337fabc..25fa914 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -2,10 +2,10 @@ I have completed the following tasks -- [ ] Login -- [ ] `auth_required.js` and `no_auth_required.js` -- [ ] Add Task -- [ ] Get Tasks -- [ ] Edit Task -- [ ] Delete Task -- [ ] Bonus Task +- [X] Login +- [X] `auth_required.js` and `no_auth_required.js` +- [X] Add Task +- [X] Get Tasks +- [X] Edit Task +- [X] Delete Task +- [X] Bonus Task diff --git a/index.html b/index.html index ffda4cf..5c1f3da 100644 --- a/index.html +++ b/index.html @@ -43,9 +43,10 @@ @@ -54,63 +55,27 @@
- +
- +
+ +
+ +
+ +
+
+
diff --git a/login/index.html b/login/index.html index 305d461..7983c60 100644 --- a/login/index.html +++ b/login/index.html @@ -50,7 +50,7 @@ - + diff --git a/package-lock.json b/package-lock.json index 58aab2e..3a9cab7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3,191 +3,13 @@ "version": "1.0.0", "lockfileVersion": 1, "requires": true, - "packages": { - "": { - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "axios": "^0.21.1", - "vite": "^2.3.7" - } - }, - "node_modules/axios": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", - "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", - "dependencies": { - "follow-redirects": "^1.10.0" - } - }, - "node_modules/colorette": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.2.tgz", - "integrity": "sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==" - }, - "node_modules/esbuild": { - "version": "0.12.8", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.12.8.tgz", - "integrity": "sha512-sx/LwlP/SWTGsd9G4RlOPrXnIihAJ2xwBUmzoqe2nWwbXORMQWtAGNJNYLBJJqa3e9PWvVzxdrtyFZJcr7D87g==", - "hasInstallScript": true, - "bin": { - "esbuild": "bin/esbuild" - } - }, - "node_modules/follow-redirects": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz", - "integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, - "node_modules/function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" - }, - "node_modules/has": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", - "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", - "dependencies": { - "function-bind": "^1.1.1" - }, - "engines": { - "node": ">= 0.4.0" - } - }, - "node_modules/is-core-module": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.4.0.tgz", - "integrity": "sha512-6A2fkfq1rfeQZjxrZJGerpLCTHRNEBiSgnu0+obeJpEPZRUooHgsizvzv0ZjJwOz3iWIHdJtVWJ/tmPr3D21/A==", - "dependencies": { - "has": "^1.0.3" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/nanoid": { - "version": "3.1.23", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", - "integrity": "sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw==", - "bin": { - "nanoid": "bin/nanoid.cjs" - }, - "engines": { - "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" - } - }, - "node_modules/path-parse": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", - "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" - }, - "node_modules/postcss": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.3.4.tgz", - "integrity": "sha512-/tZY0PXExXXnNhKv3TOvZAOUYRyuqcCbBm2c17YMDK0PlVII3K7/LKdt3ScHL+hhouddjUWi+1sKDf9xXW+8YA==", - "dependencies": { - "colorette": "^1.2.2", - "nanoid": "^3.1.23", - "source-map-js": "^0.6.2" - }, - "engines": { - "node": "^10 || ^12 || >=14" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/postcss/" - } - }, - "node_modules/resolve": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", - "integrity": "sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==", - "dependencies": { - "is-core-module": "^2.2.0", - "path-parse": "^1.0.6" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/rollup": { - "version": "2.51.2", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.51.2.tgz", - "integrity": "sha512-ReV2eGEadA7hmXSzjxdDKs10neqH2QURf2RxJ6ayAlq93ugy6qIvXMmbc5cWMGCDh1h5T4thuWO1e2VNbMq8FA==", - "bin": { - "rollup": "dist/bin/rollup" - }, - "engines": { - "node": ">=10.0.0" - }, - "optionalDependencies": { - "fsevents": "~2.3.1" - } - }, - "node_modules/source-map-js": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-0.6.2.tgz", - "integrity": "sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/vite": { - "version": "2.3.7", - "resolved": "https://registry.npmjs.org/vite/-/vite-2.3.7.tgz", - "integrity": "sha512-Y0xRz11MPYu/EAvzN94+FsOZHbSvO6FUvHv127CyG7mV6oDoay2bw+g5y9wW3Blf8OY3chaz3nc/DcRe1IQ3Nw==", - "dependencies": { - "esbuild": "^0.12.5", - "postcss": "^8.3.0", - "resolve": "^1.19.0", - "rollup": "^2.38.5" - }, - "bin": { - "vite": "bin/vite.js" - }, - "engines": { - "node": ">=12.0.0" - }, - "optionalDependencies": { - "fsevents": "~2.3.1" - } - } - }, "dependencies": { "axios": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", - "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "version": "0.21.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz", + "integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==", "requires": { - "follow-redirects": "^1.10.0" + "follow-redirects": "^1.14.0" } }, "colorette": { @@ -201,9 +23,9 @@ "integrity": "sha512-sx/LwlP/SWTGsd9G4RlOPrXnIihAJ2xwBUmzoqe2nWwbXORMQWtAGNJNYLBJJqa3e9PWvVzxdrtyFZJcr7D87g==" }, "follow-redirects": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz", - "integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==" + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==" }, "fsevents": { "version": "2.3.2", diff --git a/package.json b/package.json index 1f8de65..5ee209d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "homepage": "https://github.com/COPS-CSOC-2022/csoc-task-2-web#readme", "dependencies": { - "axios": "^0.21.1", + "axios": "^0.21.2", "vite": "^2.3.7" } } diff --git a/register/index.html b/register/index.html index 3a1f03d..75705a3 100644 --- a/register/index.html +++ b/register/index.html @@ -64,8 +64,8 @@ - + - + \ No newline at end of file diff --git a/src/auth_required.js b/src/auth_required.js index 7f5e7bc..935c0d7 100644 --- a/src/auth_required.js +++ b/src/auth_required.js @@ -1,3 +1,6 @@ /*** * @todo Redirect the user to login page if token is not present. */ +if (localStorage.token === undefined) { + window.location.href = 'login/'; +} diff --git a/src/init.js b/src/init.js index 3a88d74..d5891cf 100644 --- a/src/init.js +++ b/src/init.js @@ -1,11 +1,12 @@ import axios from 'axios'; -const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/'; +import { updateTask, deleteTask, editTask } from './main'; -function getTasks() { - /*** - * @todo Fetch the tasks created by the user and display them in the dom. - */ -} +window.deleteTask = deleteTask; +window.updateTask = updateTask; +window.editTask = editTask; + +const taskList = document.getElementById("taskList"); +const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/'; axios({ headers: { @@ -13,8 +14,60 @@ axios({ }, url: API_BASE_URL + 'auth/profile/', method: 'get', -}).then(function({data, status}) { - document.getElementById('avatar-image').src = 'https://ui-avatars.com/api/?name=' + data.name + '&background=fff&size=33&color=007bff'; - document.getElementById('profile-name').innerHTML = data.name; - getTasks(); +}).then(function ({ data, status }) { + document.getElementById('avatar-image').src = 'https://ui-avatars.com/api/?name=' + data.name + '&background=fff&size=33&color=007bff'; + document.getElementById('profile-name').innerHTML = data.name; + getTasks(); }) + +export function getTasks() { + const headersForApiRequest = { + Authorization: 'Token ' + localStorage.getItem('token') + } + + axios({ + headers: headersForApiRequest, + url: API_BASE_URL + 'todo/', + method: 'GET', + }).then(function ({ data, status }) { + console.log(data); + taskList.innerHTML = ""; + for (var index = 0; index < data.length; index++) displayTask(data[index].id, data[index].title); + }) +} + + +export function displayTask(id, title) { + + var template = ` +
  • + +
    + +
    +
    + REPLACE_TASK_TITLE +
    + + + + + +
  • + ` + var result1 = template.replace(/REPLACE_ID/g, id); + var result2 = result1.replace("REPLACE_TASK_TITLE", title); + + taskList.innerHTML += result2; + + // var updateTaskButton = document.getElementById("updateTaskButton" + id); + // if (updateTaskButton) updateTaskButton.addEventListener("click", updateTask(id)); + // var editTaskButton = document.getElementById("editTaskButton" + id); + // if (editTaskButton) editTaskButton.addEventListener("click", editTask(id)); + // var deleteTaskButton = document.getElementById("deleteTaskButton" + id); + // if (deleteTaskButton) deleteTaskButton.addEventListener("click", deleteTask(id)); +} diff --git a/src/main.js b/src/main.js index 05849df..2693526 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,11 @@ import axios from 'axios'; +import { displayTask, getTasks } from './init'; + +window.deleteTask = deleteTask; +window.updateTask = updateTask; +window.editTask = editTask; + + function displaySuccessToast(message) { iziToast.success({ title: 'Success', @@ -19,6 +26,17 @@ function displayInfoToast(message) { message: message }); } +const taskList = document.getElementById("taskList"); +const logoutButton = document.getElementById("logoutButton"); +if (logoutButton) logoutButton.onclick = logout; +const loginButton = document.getElementById("loginButton"); +if (loginButton) loginButton.onclick = login; +const registerButton = document.getElementById("registerButton"); +if (registerButton) registerButton.onclick = register; +const addTaskButton = document.getElementById("addTaskButton"); +if (addTaskButton) addTaskButton.onclick = addTask; +const searchTaskButton = document.getElementById("searchTaskButton"); +if (searchTaskButton) searchTaskButton.onclick = searchTask; const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/'; @@ -44,10 +62,10 @@ function register() { const lastName = document.getElementById('inputLastName').value.trim(); const email = document.getElementById('inputEmail').value.trim(); const username = document.getElementById('inputUsername').value.trim(); - const password = document.getElementById('inputPassword').value; + const password = document.getElementById('inputPassword').value.trim(); if (registerFieldsAreValid(firstName, lastName, email, username, password)) { - displayInfoToast("Please wait..."); + displayInfoToast("Loading..."); const dataForApiRequest = { name: firstName + " " + lastName, @@ -60,50 +78,164 @@ function register() { url: API_BASE_URL + 'auth/register/', method: 'post', data: dataForApiRequest, - }).then(function({data, status}) { - localStorage.setItem('token', data.token); - window.location.href = '/'; - }).catch(function(err) { - displayErrorToast('An account using same email or username is already created'); + }).then(function ({ data, status }) { + localStorage.setItem('token', data.token); + window.location.href = '/'; + }).catch(function (err) { + displayErrorToast('Account with same username or passord has been created'); }) } } function login() { - /*** - * @todo Complete this function. - * @todo 1. Write code for form validation. - * @todo 2. Fetch the auth token from backend, login and direct user to home page. - */ + const username = document.getElementById('inputUsername').value.trim(); + const password = document.getElementById('inputPassword').value; + + if (username == '' || password == '') { + displayErrorToast("Please enter the required fields."); + return; + } + + displayInfoToast("Loading..."); + + const dataForApiRequest = { + username: username, + password: password + } + + axios({ + url: API_BASE_URL + 'auth/login/', + method: 'POST', + data: dataForApiRequest, + }).then(function ({ data, status }) { + displaySuccessToast("You are logged in successfully"); + localStorage.setItem('token', data.token); + window.location.href = '/'; + }).catch(function (err) { + displayErrorToast("The credentials used are invalid."); + document.getElementById('inputUsername').value = ''; + document.getElementById('inputPassword').value = ''; + }) + +} + + +function searchTask(){ + const task = document.getElementById('searchTask').value.trim(); + + if (task == '') { + displayErrorToast("Todo cannot be blank"); + return; + } + + displayInfoToast("Loading..."); + + const headersForApiRequest = { + Authorization: 'Token ' + localStorage.getItem('token') + } + + axios({ + headers: headersForApiRequest, + url: API_BASE_URL + 'todo/', + method: 'GET', + }).then(function ({ data, status }) { + console.log(data); + for (var index = 0; index < data.length; index++) if (data[index].title == task){ + taskList.innerHTML = ""; + displaySuccessToast("This task was found"); + displayTask(data[index].id, data[index].title); + return; + } + displayErrorToast("The given task wasn't found") + }) } function 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. - */ + const task = document.getElementById('inputTask').value.trim(); + + if (task == '') { + displayErrorToast("Todo cannot be blank"); + return; + } + + displayInfoToast("Loading..."); + + const dataForApiRequest = { + title: task + } + const headersForApiRequest = { + Authorization: 'Token ' + localStorage.getItem('token') + } + + axios({ + headers: headersForApiRequest, + url: API_BASE_URL + 'todo/create/', + method: 'POST', + data: dataForApiRequest, + }).then(function ({ data, status }) { + displaySuccessToast("Todo added successfully"); + document.getElementById('inputTask').value = ''; + getTasks(); + }).catch(function (err) { + displayErrorToast("Unable to add the todo. Please try again"); + }) + } -function editTask(id) { + +export function editTask(id) { document.getElementById('task-' + id).classList.add('hideme'); document.getElementById('task-actions-' + id).classList.add('hideme'); document.getElementById('input-button-' + id).classList.remove('hideme'); document.getElementById('done-button-' + id).classList.remove('hideme'); } -function deleteTask(id) { - /** - * @todo Complete this function. - * @todo 1. Send the request to delete the task to the backend server. - * @todo 2. Remove the task from the dom. - */ +export function deleteTask(id) { + displayInfoToast("Please wait..."); + + const headersForApiRequest = { + Authorization: 'Token ' + localStorage.getItem('token') + } + + axios({ + headers: headersForApiRequest, + url: API_BASE_URL + 'todo/' + id + '/', + method: 'DELETE', + }).then(function ({ data, status }) { + displaySuccessToast("Todo deleted successfully..."); + getTasks(); + }).catch(function (err) { + displayErrorToast("Unable to delete the given todo. Please try again"); + }) } -function updateTask(id) { - /** - * @todo Complete this function. - * @todo 1. Send the request to update the task to the backend server. - * @todo 2. Update the task in the dom. - */ +export function updateTask(id) { + const newTask = document.getElementById("input-button-" + id).value.trim(); + const taskItem = document.getElementById("task-" + id); + + if (newTask==""){ + displayErrorToast("The todo title can't be blank"); + return; + } + + const dataForApiRequest = { + title: newTask + } + const headersForApiRequest = { + Authorization: 'Token ' + localStorage.getItem('token') + } + + axios({ + headers: headersForApiRequest, + url: API_BASE_URL + 'todo/' + id + '/', + method: 'PUT', + data: dataForApiRequest, + }).then(function ({ data, status }) { + taskItem.textContent=data.title; + editTask(data.id); + displaySuccessToast("Todo updated successfully..."); + getTasks(); + }).catch(function (err) { + displayErrorToast("Unable to update the todo. Please try again..."); + }) } diff --git a/src/no_auth_required.js b/src/no_auth_required.js index 82558d4..b31cac4 100644 --- a/src/no_auth_required.js +++ b/src/no_auth_required.js @@ -1,3 +1,8 @@ /*** * @todo Redirect the user to main page if token is present. - */ \ No newline at end of file + */ + + +if (localStorage.token) { + window.location.href = '/'; +} diff --git a/style.css b/style.css index 2817a0d..28897e6 100644 --- a/style.css +++ b/style.css @@ -7,6 +7,10 @@ max-width:400px; } +.todo-search-task { + margin-top:10px; + max-width:400px; +} .todo-available-tasks { margin-bottom:30px; max-width:400px;