From 2edf3b66528ef96ca1d8c47d4e8014f8cb375f9b Mon Sep 17 00:00:00 2001 From: yash6318 Date: Fri, 1 Jul 2022 21:19:21 +0530 Subject: [PATCH] task completed --- index.html | 56 ++----------- login/index.html | 2 +- package-lock.json | 181 +++++++++++++++++++++++++++++++++++++++- src/auth_required.js | 3 + src/init.js | 46 +++++++++- src/main.js | 173 +++++++++++++++++++++++++++++++++----- src/no_auth_required.js | 5 +- style.css | 5 ++ 8 files changed, 395 insertions(+), 76 deletions(-) diff --git a/index.html b/index.html index ffda4cf..1e53493 100644 --- a/index.html +++ b/index.html @@ -54,63 +54,19 @@
- +
-
diff --git a/login/index.html b/login/index.html index 305d461..aa66084 100644 --- a/login/index.html +++ b/login/index.html @@ -50,7 +50,7 @@ - + diff --git a/package-lock.json b/package-lock.json index 3a9cab7..9508f6d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,187 @@ { "name": "csoc-task-2-web", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "csoc-task-2-web", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "axios": "^0.21.2", + "vite": "^2.3.7" + } + }, + "node_modules/axios": { + "version": "0.21.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz", + "integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==", + "dependencies": { + "follow-redirects": "^1.14.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.15.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==", + "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.2", diff --git a/src/auth_required.js b/src/auth_required.js index 7f5e7bc..e6c288e 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.getItem('token') == null|| localStorage.token == undefined) { + window.location.href = (`${window.location.href}login/`) +} diff --git a/src/init.js b/src/init.js index 3a88d74..fc672a1 100644 --- a/src/init.js +++ b/src/init.js @@ -2,9 +2,47 @@ import axios from 'axios'; const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/'; function getTasks() { - /*** - * @todo Fetch the tasks created by the user and display them in the dom. - */ + + const headerForGetting={ + 'Authorization': `Token ${window.localStorage.getItem('token')}`, + 'Content-Type': "application/json" + } + + axios({ + url: API_BASE_URL + 'todo/', + method: 'GET', + headers: headerForGetting + }).then(function({data, status}) { + + let tasks =document.getElementById("list-of-todos"); + tasks.innerHTML=` Available Tasks ` + for (let task of data){ + tasks.innerHTML += + ` +
  • + +
    + +
    +
    + ${task.title} +
    + + + + + +
  • + ` + } + }) } axios({ @@ -18,3 +56,5 @@ axios({ document.getElementById('profile-name').innerHTML = data.name; getTasks(); }) + +window.getTasks=getTasks; \ No newline at end of file diff --git a/src/main.js b/src/main.js index 05849df..10cb11b 100644 --- a/src/main.js +++ b/src/main.js @@ -70,19 +70,115 @@ function register() { } 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; + + // Checking validation + + if (username === '' || password === '') { + displayErrorToast("Do not use empty fields!"); + return; + } + const dataForApiRequest = { + username: username, + password: password + } + + //fetching auth token from backend + + axios({ + url: API_BASE_URL + 'auth/login/', + method: 'POST', + data: dataForApiRequest, + }).then(function({data, status}) { + window.localStorage.setItem("token",data.token); + window.location.href = '/'; + displaySuccessToast('Login was successful!!') + }).catch(function(err) { + displayErrorToast('Invalid credentials! :('); + }) } 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 newtask = document.getElementById("new-inp-task").value; + + const dataForNewTask = { + title: newtask.trim() + } + axios({ + url: API_BASE_URL + 'todo/create/', + method: 'POST', + headers: { + 'Authorization': `token ${localStorage.getItem('token')}`, + 'Content-Type': "application/json" + }, + data: dataForNewTask + }).then(() => { + getTasks(); + document.getElementById("new-inp-task").value=''; + document.getElementById("new-inp-task").placeholder="Enter Task"; + displaySuccessToast('Task added successfully!'); + }).catch(function(err) { + displayErrorToast("There was an error! Try again!"); + }) +} + +function searchTask() { + + const search = document.getElementById('search-task').value.trim(); + + if(!search) { + displayErrorToast("Task cannot be empty!"); + } + else { + + axios({ + url: API_BASE_URL + 'todo/', + method: 'get', + headers: { + Authorization: 'Token ' + localStorage.getItem('token'), + } + }).then(function({data, status}) { + + for(let i=0; i + +
    + +
    +
    + ${data[i].title} +
    + + + + + + ` + ; + return; + } + } + document.getElementById('search-task').value = null; + displayErrorToast("There do not exist such task! :("); + }).catch(function(err) { + document.getElementById('search-task').value = null; + displayErrorToast("There was an error! Try again!"); + }) + } } function editTask(id) { @@ -93,17 +189,54 @@ function editTask(id) { } 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. - */ + + if(confirm("Are you sure you want to delete this task?")) { + + axios({ + url: API_BASE_URL + `todo/${id}/`, + method: 'DELETE', + headers: { + 'Authorization': `Token ${window.localStorage.getItem('token')}`, + 'Content-Type': "application/json" + } + }).then(() => { + displaySuccessToast("Task Deleted Succesfully!") + getTasks(); + }).catch(function(err) { + displayErrorToast("There was an error! 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. - */ + const updatedTask=document.getElementById(`input-button-${id}`).value; + + if(updatedTask==="") displayErrorToast("Cannot be a null string!"); + else{ + axios({ + url: API_BASE_URL + `todo/${id}/`, + method: 'PUT', + headers: { + 'Authorization': `Token ${window.localStorage.getItem('token')}`, + 'Content-Type': "application/json" + }, + data: {title: updatedTask} + }).then(() => { + getTasks(); + displaySuccessToast('Task updated successfully!') + }).catch(function(err) { + displayErrorToast("There was an error! Try again!"); + }) + } + + } + +window.register=register; +window.login=login; +window.logout=logout; +window.addTask=addTask; +window.searchTask=searchTask; +window.editTask=editTask; +window.updateTask=updateTask; +window.deleteTask=deleteTask; diff --git a/src/no_auth_required.js b/src/no_auth_required.js index 82558d4..df91c7a 100644 --- a/src/no_auth_required.js +++ b/src/no_auth_required.js @@ -1,3 +1,6 @@ /*** * @todo Redirect the user to main page if token is present. - */ \ No newline at end of file + */ + if(localStorage.token != null || localStorage.token != undefined){ + window.location.href = '/' +} \ No newline at end of file diff --git a/style.css b/style.css index 2817a0d..aa7358a 100644 --- a/style.css +++ b/style.css @@ -7,6 +7,11 @@ max-width:400px; } +.search-task { + margin-top:20px; + max-width:400px; +} + .todo-available-tasks { margin-bottom:30px; max-width:400px;