-
Notifications
You must be signed in to change notification settings - Fork 17
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
Completed #10
base: main
Are you sure you want to change the base?
Completed #10
Changes from 2 commits
e379041
88b9739
7502fb4
5d03707
fa617aa
3687c08
169903d
862a2a5
dac44ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
<template> | ||
<aside class="mx-auto flex justify-between mt-24 px-4"> | ||
<label for="add task" class="flex-1"> | ||
<input | ||
type="text" | ||
name="add task" | ||
class=" | ||
<input type="text" name="add task" class=" | ||
todo-add-task-input | ||
px-4 | ||
py-2 | ||
|
@@ -17,13 +14,9 @@ | |
outline-none | ||
focus:outline-none focus:ring | ||
w-full | ||
" | ||
placeholder="Enter Task" | ||
/> | ||
" v-model.trim="newTask" placeholder="Enter Task" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch on trimming |
||
</label> | ||
<button | ||
type="button" | ||
class=" | ||
<button type="button" class=" | ||
todo-add-task | ||
bg-transparent | ||
hover:bg-green-500 | ||
|
@@ -34,9 +27,7 @@ | |
border border-green-500 | ||
hover:border-transparent | ||
rounded | ||
" | ||
@click="addTask" | ||
> | ||
" @click="addTask"> | ||
Add Task | ||
</button> | ||
</aside> | ||
|
@@ -55,6 +46,25 @@ export default defineComponent({ | |
* @todo 2. Add the task in the dom. | ||
* @hint use emit to make a event that parent can observe | ||
*/ | ||
var newTask = this.newTask | ||
if (newTask == '') { | ||
return | ||
} | ||
this.$toast.info('Adding...') | ||
const headers = { | ||
Authorization: 'Token ' + this.$store.getters.token, | ||
} | ||
this.$axios({ | ||
headers: headers, | ||
url: 'todo/create/', | ||
method: 'post', | ||
data: { title: newTask } | ||
}).then(() => { | ||
this.$toast.success('Todo added successfully!') | ||
this.$router.go() | ||
}).catch(() => | ||
this.$toast.error('Some error occurred!') | ||
) | ||
}, | ||
}, | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,13 @@ export default defineNuxtMiddleware((context) => { | |
* @todo Redirect the user to main page if token is present in store | ||
* @hints check what propeties context has | ||
*/ | ||
const path=context.route.fullPath | ||
const token=context.store.getters.token | ||
|
||
if (token === null && path === '/'){ | ||
context.redirect('login/'); | ||
} | ||
else if(token != null && path != '/'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer using strict equalities |
||
context.redirect('/'); | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,7 @@ | |
<transition> | ||
<span v-if="loading">Fetching Tasks....</span> | ||
<ul v-else class="flex-col mt-9 mx-auto"> | ||
<li | ||
v-for="(todo, index) in todos" | ||
:key="todo.id" | ||
class=" | ||
<li v-for="(todo, index) in todos" :key="todo.id" class=" | ||
border | ||
flex | ||
border-gray-500 | ||
|
@@ -17,23 +14,14 @@ | |
justify-between | ||
items-center | ||
mb-2 | ||
" | ||
> | ||
"> | ||
<label :for="todo.id"> | ||
<input | ||
:id="todo.id" | ||
type="text" | ||
:class="[ | ||
'hideme appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring todo-edit-task-input', | ||
]" | ||
:name="todo.title" | ||
placeholder="Edit The Task" | ||
/> | ||
<input v-show="todo.editing" :id="todo.id" type="text" :class="[ | ||
'appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring todo-edit-task-input', | ||
]" v-model.trim="todos[index].title" :name="todo.title" placeholder="Edit The Task" /> | ||
</label> | ||
<div class=""> | ||
<button | ||
class=" | ||
hideme | ||
<button v-show="todo.editing" class=" | ||
bg-transparent | ||
hover:bg-gray-500 | ||
text-gray-700 text-sm | ||
|
@@ -44,57 +32,37 @@ | |
hover:border-transparent | ||
rounded | ||
todo-update-task | ||
" | ||
type="button" | ||
@click="updateTask(index, todo.id)" | ||
> | ||
" type="button" @click="updateTask(index, todo.id)"> | ||
Done | ||
</button> | ||
</div> | ||
<div :class="['todo-task text-gray-600']"> | ||
<div v-show="!todo.editing" :class="['todo-task text-gray-600']"> | ||
{{ todo.title }} | ||
</div> | ||
<span class=""> | ||
<button | ||
style="margin-right: 5px" | ||
type="button" | ||
class=" | ||
<span v-show="!todo.editing" class=""> | ||
<button style="margin-right: 5px" type="button" class=" | ||
bg-transparent | ||
hover:bg-yellow-500 hover:text-white | ||
border border-yellow-500 | ||
hover:border-transparent | ||
rounded | ||
px-2 | ||
py-2 | ||
" | ||
@click="editTask(index)" | ||
> | ||
<img | ||
src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486663/CSOC/edit.png" | ||
width="18px" | ||
height="20px" | ||
alt="Edit" | ||
/> | ||
" @click="editTask(index)"> | ||
<img src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486663/CSOC/edit.png" width="18px" | ||
height="20px" alt="Edit" /> | ||
</button> | ||
<button | ||
type="button" | ||
class=" | ||
<button type="button" class=" | ||
bg-transparent | ||
hover:bg-red-500 hover:text-white | ||
border border-red-500 | ||
hover:border-transparent | ||
rounded | ||
px-2 | ||
py-2 | ||
" | ||
@click="deleteTask(index, todo.id)" | ||
> | ||
<img | ||
src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486661/CSOC/delete.svg" | ||
width="18px" | ||
height="22px" | ||
alt="Delete" | ||
/> | ||
" @click="deleteTask(index, todo.id)"> | ||
<img src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486661/CSOC/delete.svg" width="18px" | ||
height="22px" alt="Delete" /> | ||
</button> | ||
</span> | ||
</li> | ||
|
@@ -108,27 +76,19 @@ import { defineComponent } from '@nuxtjs/composition-api' | |
import addTask from '~/components/addTask.vue' | ||
|
||
export default defineComponent({ | ||
middleware: 'auth', | ||
components: { addTask }, | ||
data() { | ||
return { | ||
hello: 'hello world!', | ||
todos: [ | ||
{ | ||
title: 'Henlo', | ||
id: 1, | ||
editing: false, | ||
}, | ||
{ | ||
title: 'Frens', | ||
id: 2, | ||
editing: false, | ||
}, | ||
], | ||
loading: false, | ||
} | ||
}, | ||
mounted() { | ||
this.getTasks() | ||
this.loading = true, | ||
this.getTasks() | ||
}, | ||
methods: { | ||
async getTasks() { | ||
|
@@ -138,6 +98,24 @@ export default defineComponent({ | |
* @hints use store and set loading true | ||
* @caution you have to assign new value to todos for it to update | ||
*/ | ||
const headers = { | ||
Authorization: 'Token ' + this.$store.getters.token, | ||
} | ||
this.$axios({ | ||
headers: headers, | ||
url: 'todo/', | ||
method: 'get', | ||
}).then((res) => { | ||
this.loading = false | ||
res.data.forEach((todo) => { | ||
todo.editing = false | ||
}) | ||
this.todos = res.data | ||
}).catch((err) => | ||
$toast.error( | ||
"Some Error Occurred!" | ||
) | ||
) | ||
}, | ||
/** | ||
* Function to update a single todo | ||
|
@@ -147,7 +125,24 @@ export default defineComponent({ | |
* @todo 1. Send the request to update the task to the backend server. | ||
* @todo 2. Update the task in the dom. | ||
*/ | ||
updateTask(_index, _id) {}, | ||
updateTask(_index, _id) { | ||
const headers = { | ||
Authorization: 'Token ' + this.$store.getters.token, | ||
} | ||
const data = { title: this.todos[_index].title } | ||
this.$axios({ | ||
headers: headers, | ||
url: 'todo/' + _id + '/', | ||
method: 'patch', | ||
data: data, | ||
}) | ||
.then(() => { | ||
this.$router.go() | ||
}) | ||
.catch(() => | ||
this.$toast.error('Updation failed. Please try again later.') | ||
) | ||
}, | ||
/** | ||
* toggle visibility of input and buttons for a single todo | ||
* @argument {number} index - index of element to toggle | ||
|
@@ -165,7 +160,24 @@ export default defineComponent({ | |
* @todo 1. Send the request to delete the task to the backend server. | ||
* @todo 2. Remove the task from the dom. | ||
*/ | ||
deleteTask(_index, _id) {}, | ||
deleteTask(_index, _id) { | ||
const headers = { | ||
Authorization: 'Token ' + this.$store.getters.token, | ||
} | ||
this.$toast.info('Deleting....') | ||
this.$axios({ | ||
headers: headers, | ||
url: 'todo/' + _id + '/', | ||
method: 'delete', | ||
}) | ||
.then(() => { | ||
this.$toast.success('Todo deleted successfully!') | ||
this.$router.go() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think of how you can delete the task and prevent reloading the page |
||
}) | ||
.catch(() => | ||
this.$toast.error("Some error occured!"), | ||
) | ||
}, | ||
}, | ||
}) | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name attribute cannot have spaces in between