forked from COPS-IITBHU/csoc-2021-task-3-reactjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoListItem.js
170 lines (137 loc) · 4.54 KB
/
TodoListItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* eslint-disable @next/next/no-img-element */
import { info } from "autoprefixer";
import { useState, useRef } from "react"
import { useAuth } from '../context/auth'
import axios from "axios";
import { useRouter } from 'next/router'
import { Flip, toast } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css';
import { errorToast, infoToast, sucsessToast } from "./toast";
export default function TodoListItem({ tasks, task, setTasks, getTasks }) {
const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/';
const { token } = useAuth();
const router = useRouter()
const [isedit, editMode] = useState(false);
const [title, setTitle] = useState('')
const editTask = (id) => {
/**
* @todo Complete this function.
* @todo 1. Update the dom accordingly
*/
editMode(true)
infoToast("Edit task")
}
const 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.
*/
infoToast("Deleting Task...");
axios({
url: `https://todo-app-csoc.herokuapp.com/todo/${id}/`,
method: 'DELETE',
headers: {
Authorization: "Token " + token
}
}).then((res) => {
toast.dismiss()
setTasks(tasks.filter(task => task.id != id));
sucsessToast("Task Deleted")
}).catch((err) => {
console.log(err)
errorToast("Error In Deleting Task")
})
}
const 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.
*/
toast.dismiss();
const dataForApiRequest = {
title: title
}
if (!title) {
editMode(false)
return;
}
infoToast("Udating Task...");
axios({
url: API_BASE_URL + 'todo/' + id + '/',
method: 'PATCH',
data: dataForApiRequest,
headers: {
Authorization: "Token " + token
},
})
.then(({ data }) => {
editMode(false)
toast.dismiss();
setTasks(tasks.map((task) => (
task.id === id ? { ...task, title: data.title } : task
)))
setTitle('')
sucsessToast("Task Updated")
})
.catch(function (err) {
console.log(err)
errorToast("error in updating tasks")
})
}
return (
<>
<li className='border flex border-gray-500 rounded px-2 py-2 justify-between items-center mb-2'>
<input
id={`input-button-${task.id}`}
type='text'
className={`${isedit ? "" : "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`}
placeholder='Edit The Task'
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<div id={`done-button-${task.id}`} className={`${isedit ? "" : "hideme"}`}>
<button
className='bg-transparent hover:bg-gray-500 text-gray-700 text-sm hover:text-white py-2 px-3 border border-gray-500 hover:border-transparent rounded todo-update-task'
type='button'
onClick={() => updateTask(task.id)}
>
Done
</button>
</div>
<div id={`task-${task.id}`} className={`${isedit ? 'hideme' : 'todo-task text-gray-600'}`}>
{task.title}
</div>
<span id={`task-actions-${task.id}`} className={`${isedit ? 'hideme' : ''}`}>
<button
style={{ marginRight: '5px' }}
type='button'
onClick={() => editTask(task.id)}
className='bg-transparent hover:bg-yellow-500 hover:text-white border border-yellow-500 hover:border-transparent rounded px-2 py-2'
>
<img
src='https://res.cloudinary.com/nishantwrp/image/upload/v1587486663/CSOC/edit.png'
width='18px'
height='20px'
alt='Edit'
/>
</button>
<button
type='button'
className='bg-transparent hover:bg-red-500 hover:text-white border border-red-500 hover:border-transparent rounded px-2 py-2'
onClick={() => deleteTask(task.id)}
>
<img
src='https://res.cloudinary.com/nishantwrp/image/upload/v1587486661/CSOC/delete.svg'
width='18px'
height='22px'
alt='Delete'
/>
</button>
</span>
</li>
</>
)
}