Skip to content

Csoc week3 #7

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

Open
wants to merge 5 commits 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
36 changes: 35 additions & 1 deletion components/addTask.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<label for="add task" class="flex-1">
<input
type="text"
v-model.trim="inputText"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on using trim.

name="add task"
class="
todo-add-task-input
Expand Down Expand Up @@ -35,6 +36,7 @@
hover:border-transparent
rounded
"
id="addbtn"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use camel case naming convention.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok,next time I will keep it in mind.

@click="addTask"
>
Add Task
Expand All @@ -47,15 +49,47 @@ import { defineComponent } from '@nuxtjs/composition-api'

export default defineComponent({
emits: ['newTask'],
data(){
return{
inputText:""
}
},
methods: {
addTask() {
async 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.
* @hint use emit to make a event that parent can observe
*/
console.log("debyg");
console.log(this.inputText);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all console log statements before submitting PR.
Ditto below.

Copy link
Author

@abhishek-sultaniya abhishek-sultaniya Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok,Should i comment all console logs or erase it?

Sometimes I have to use it for debugging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to remove all console log statements before submitting PRs. When you submit code to a larger repo which is open-source, you don't want these unnecessary statements to sneak into the codebase.


if(this.inputText=='')
{
this.$toast.error('Cannot Add empty Todo');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation and capitalize the error message properly. Ditto below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From next time I would use prettier.Will that be fine?

I will keep it in mind.
Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, using prettier is fine. However, a habit of writing clean code goes a long way.

return
}

const data={title: this.inputText,}
const headers={Authorization: 'Token ' + this.$store.getters.token}

this.$axios.post('todo/create/',data,{headers})
.then((response) => {
this.$toast.success('Todo added!...')
this.$emit('newTask')
})
.catch((err) => {
this.$toast.error("Unable to add task!..")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good error callback would be to prompt the user to try again later.

})
},
},
})
</script>

<style>
#addbtn
{
background-color: rgb(105, 208, 105);
}
</style>
21 changes: 21 additions & 0 deletions components/navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<li class="font-semibold text-white">
<nuxt-link to="/">Tasks</nuxt-link>
</li>
<li>

</li>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying something but removed that but forgot to erase li tag.

</ul>
<ul v-else class="flex">
<li class="text-white mr-2">
Expand All @@ -19,6 +22,7 @@
</li>
</ul>
</transition>
<div v-if="auth"><h2 class="font-bold text-white text-xl">Welcome {{name}}</h2></div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition!

<div v-if="auth" class="w-28 inline-block relative">
<div class="group inline-block relative">
<a
Expand All @@ -32,6 +36,7 @@
hover:bg-gray-400
"
href="#"
id="idol"
@click="logout"
>
Logout
Expand All @@ -46,16 +51,32 @@
import { defineComponent } from '@nuxtjs/composition-api'

export default defineComponent({
// data(){return{
// username:""
// }},
computed: {
auth() {
return this.$store.getters.auth
},
name: function(){
return this.$store.getters.name
},
},

methods: {
logout() {
this.$store.commit('setToken', null)
this.$router.replace('/login')
this.$toast.success('Logged out!...')
},
},
})
</script>

<style>
#idol{
color: white;
/* border: 10px red solid; */
background-color: black;
}
</style>
17 changes: 17 additions & 0 deletions middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ export default defineNuxtMiddleware((context) => {
* @todo Redirect the user to main page if token is present in store
* @hints check what propeties context has
*/
// hh
let y = context.store.getters.token;
let x = context.route.fullPath;
console.log("hola")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console log statements.

console.log(y)
console.log(x)
console.log("hola2")

if (y === null && x === '/')
{
context.redirect('login/');
}
else if (y != null && x != '/')
{
context.redirect('/');
}

})
148 changes: 112 additions & 36 deletions pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<main class="max-w-lg mx-auto px-6">
<main class="max-w-lg mx-auto px-8">
<add-task @newTask="getTasks" />
<transition>
<span v-if="loading">Fetching Tasks....</span>
Expand All @@ -24,16 +24,17 @@
: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',
'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"
v-model.trim="editTitle"
v-show="!todos[index].editing"
/>
</label>
<div class="">
<button
class="
hideme
bg-transparent
hover:bg-gray-500
text-gray-700 text-sm
Expand All @@ -46,15 +47,18 @@
todo-update-task
"
type="button"
v-show="!todos[index].editing"
@click="updateTask(index, todo.id)"
>
Done
</button>
</div>

<div :class="['todo-task text-gray-600']">
{{ todo.title }}
</div>
<span class="">

<button
style="margin-right: 5px"
type="button"
Expand All @@ -67,7 +71,8 @@
px-2
py-2
"
@click="editTask(index)"
v-show="!todos[index].editing"
@click="editTask(index, todo.id)"
>
<img
src="https://res.cloudinary.com/nishantwrp/image/upload/v1587486663/CSOC/edit.png"
Expand All @@ -76,17 +81,21 @@
alt="Edit"
/>
</button>

<button
type="button"
class="
bg-transparent
hover:bg-red-500 hover:text-white
hover:bg-red-500
hover:text-white
border border-red-500
hover:border-transparent
rounded
px-2
py-2
"
id="styd"
v-show="!todos[index].editing"
@click="deleteTask(index, todo.id)"
>
<img
Expand All @@ -108,37 +117,60 @@ import { defineComponent } from '@nuxtjs/composition-api'
import addTask from '~/components/addTask.vue'

export default defineComponent({
middleware: 'auth',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation.

components: { addTask },
data() {
return {
hello: 'hello world!',
todos: [
{
title: 'Henlo',
id: 1,
editing: false,
},
{
title: 'Frens',
id: 2,
editing: false,
},
],
todos: [],
todoText:"",
editTitle:"",
loading: false,
}
},
mounted() {
this.getTasks()
this.getTasks(),
this.getname()
},
methods: {
async getTasks() {
/***
* @todo Fetch the tasks created by the user and display them.
* @todo also the function to display a single new task added
* @hints use store and set loading true
* @caution you have to assign new value to todos for it to update
*/
this.loading = true;
const headers={
Authorization: 'Token ' + this.$store.getters.token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation. Ditto below

}
this.$axios.get('todo/',{headers})
.then((response) =>
{
console.log("data");
console.log(response.data);
this.todos=response.data;
this.loading = false;
response.data.forEach((value)=>{
value.editing=false
}) })
.catch((err) => {
this.$toast.error("Error!..")
})
},

getname(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow proper naming conventions.

console.log("check getname")
const headers={
Authorization: 'Token ' + this.$store.getters.token
}
this.$axios.get('https://todo-app-csoc.herokuapp.com/auth/profile/',{headers})
.then((response) => {
console.log("inside getname")
console.log(response.data)

this.$store.commit('setName', response.data.name)
})
.catch((err) => {
console.log(err)
})
},


/**
* Function to update a single todo
* @argument {number} _index - index of element to update in todos array
Expand All @@ -147,25 +179,69 @@ 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) {},
async updateTask(_index, _id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should have added some checks to ensure that the new ToDo isn't empty and is not the same as the original one. Accordingly, the necessary toasts should be raised to inform the user.

console.log("updatingg")
console.log("data67");
const data={title: this.editTitle,}
const headers={Authorization: 'Token ' + this.$store.getters.token}

if(this.todos[_index].title!= ''){
await this.$axios.patch(`https://todo-app-csoc.herokuapp.com/todo/${_id}/`,data,{headers})
.then((response) =>
{
console.log("data66");
console.log(response);
console.log(this.todos[_index].editing)
console.log("data77");
this.todos[_index].title=this.editTitle
this.editTitle=''
this.$toast.success('Task Updated')
this.todos[_index].editing = !this.todos[_index].editing;
})
.catch((err) => {
this.$toast.error("Error!..")
})
}
this.todos[_index].editing = !this.todos[_index].editing;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding this here? This is already present on line 199. This might be the reason you are facing difficulties while updating todos.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look it,by mistake during trial I might have added it here.

console.log("errrr")
},
/**
* toggle visibility of input and buttons for a single todo
* @argument {number} index - index of element to toggle
* @todo add in bindings in dom so that 'hideme' class is dynamic or use conditional rendering
* @hint read about class bindings in vue
*/
editTask(index) {
this.todos[index].editing = !this.todos[index].editing
editTask(index,id) {
console.log("editngg")
console.log(this.todos[index].editing);
this.todos[index].editing = !this.todos[index].editing;
console.log(this.todos[index].editing);
},

deleteTask(_index, _id) {
console.log("inside del")
this.$toast.info('Please wait...')
const headers={
Authorization: 'Token ' + this.$store.getters.token
}
this.$axios.delete(`https://todo-app-csoc.herokuapp.com/todo/${_id}/`,{headers})
.then((response) => {
this.todos.splice(_index,1)
this.$toast.success('Task deleted!')
})
.catch((err) => {
this.$toast.error('Unable to delete the task!..')
})
},
/**
* Function to delete a single todo
* @argument {number} _index - index of element to update in todos array
* @argument {number} _id - id of todo obtained from API
* @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.
*/
deleteTask(_index, _id) {},
},
})
</script>

<style>
#styd{
background-color: red;
}
#edbtn{
background:rgb(77, 77, 219);
}
</style>
Loading