|
2 | 2 |
|
3 | 3 | In this lesson, we will add capability to delete to-do item. |
4 | 4 |
|
5 | | -First, let's edit the `todos.js` file in `router` folder to add this capability. To delete an entry in database, we use `delete` request. So let's add such a route. |
| 5 | +We will first add capability to remove an item to our model. Edit the `models/todo.js` to have a `remove` method. |
6 | 6 |
|
7 | 7 | ```js |
8 | | -app.delete('/todos/:id', async function (req, res, next) { |
9 | | - await db.Todo.removeTask(req.params.id); |
10 | | - res.json({ success: true }) |
11 | | -}); |
| 8 | +static async remove(id) { |
| 9 | + return this.destroy({ |
| 10 | + where: { |
| 11 | + id, |
| 12 | + }, |
| 13 | + }); |
| 14 | + } |
12 | 15 | ``` |
13 | 16 |
|
14 | | -Here, the `id` of the todo item which is to be deleted is passed in the url. We just need to invoke the `removeTask` method which we have already implemented earlier. |
15 | | - |
16 | | -To add this capability to the front end, let's open the `todos.ejs` file in the `views` folder. Now, we need to add capability to delete the to-do when clicked on the trash icon. |
| 17 | +Next, let's edit the `app.js` file to add this capability. To delete an entry in database, we use `delete` request. So let's add such a route. |
17 | 18 |
|
18 | | -We will wrap the `<a>` tags in its own `form`. Then invoke `deleteTodo` when clicked on it. Here we also pass the id to `deleteTodo` to identify which item was invoked. |
19 | | - |
20 | | -```html |
21 | | -<form> |
22 | | - <a class="hidden trash-icon" href="#!" onclick="deleteTodo(<%= dueToday[i].id %>)"> |
23 | | - <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" |
24 | | - stroke="currentColor" stroke-width="2"> |
25 | | - <path stroke-linecap="round" stroke-linejoin="round" |
26 | | - d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /> |
27 | | - </svg> |
28 | | - </a> |
29 | | -</form> |
| 19 | +```js |
| 20 | +app.delete("/todos/:id", async (request, response) => { |
| 21 | + try { |
| 22 | + await Todo.remove(request.params.id); |
| 23 | + return response.json({ success: true }); |
| 24 | + } catch (error) { |
| 25 | + return response.status(422).json(error); |
| 26 | + } |
| 27 | +}); |
30 | 28 | ``` |
31 | 29 |
|
32 | | -We will do the same for `dueLater` items also |
| 30 | +Here, the `id` of the todo item which is to be deleted is passed in the url. We just need to invoke the `remove` method which we have already implemented earlier. |
33 | 31 |
|
34 | | -```html |
35 | | -<form> |
36 | | - <a class="hidden trash-icon" href="#!" onclick="deleteTodo(<%= dueLater[i].id %>)"> |
37 | | - <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" |
38 | | - stroke="currentColor" stroke-width="2"> |
39 | | - <path stroke-linecap="round" stroke-linejoin="round" |
40 | | - d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /> |
41 | | - </svg> |
42 | | - </a> |
43 | | -</form> |
44 | | -``` |
| 32 | +To add this capability to the front end, let's open the `todos.ejs` file in the `views` folder. Now, we need to add capability to delete the to-do when clicked on the trash icon. |
45 | 33 |
|
46 | | -Next, we have to update the `overdue` items. |
| 34 | +We will add `onClick` handler to invoke a `deleteTodo` function when clicked on it. Here we also pass the id to `deleteTodo` to identify which item was invoked. |
47 | 35 |
|
48 | 36 | ```html |
49 | | -<form> |
50 | | - <a class="hidden trash-icon" href="#!" onclick="deleteTodo(<%= overdue[i].id %>)"> |
51 | | - <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" |
52 | | - stroke="currentColor" stroke-width="2"> |
53 | | - <path stroke-linecap="round" stroke-linejoin="round" |
54 | | - d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /> |
55 | | - </svg> |
56 | | - </a> |
57 | | -</form> |
| 37 | +<a |
| 38 | + href="#" |
| 39 | + class="hidden trash-icon ml-2" |
| 40 | + onclick="deleteTodo(<%= data[i].id %>)" |
| 41 | +> |
| 42 | + <svg |
| 43 | + xmlns="http://www.w3.org/2000/svg" |
| 44 | + fill="none" |
| 45 | + viewBox="0 0 24 24" |
| 46 | + stroke-width="1.5" |
| 47 | + stroke="currentColor" |
| 48 | + class="w-4 h-4" |
| 49 | + > |
| 50 | + <path |
| 51 | + stroke-linecap="round" |
| 52 | + stroke-linejoin="round" |
| 53 | + d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" |
| 54 | + /> |
| 55 | + </svg> |
| 56 | +</a> |
58 | 57 | ``` |
59 | 58 |
|
60 | 59 | Now, we have to implement the actual JavaScript function `deleteTodo` which actually sends the delete request to the server. |
61 | 60 |
|
62 | 61 | ```js |
63 | 62 | function deleteTodo(id) { |
64 | | - fetch(`/todos/${id}`, { |
65 | | - method: 'delete', |
66 | | - headers: { 'Content-Type': 'application/json' }, |
67 | | - }).then((res) => { |
68 | | - window.location.reload(); |
69 | | - }).catch(err => console.error(err)) |
70 | | - } |
| 63 | + fetch(`/todos/${id}`, { |
| 64 | + method: "delete", |
| 65 | + headers: { "Content-Type": "application/json" }, |
| 66 | + }) |
| 67 | + .then((res) => { |
| 68 | + window.location.reload(); |
| 69 | + }) |
| 70 | + .catch((err) => console.error(err)); |
| 71 | +} |
71 | 72 | ``` |
72 | 73 |
|
73 | 74 | Here, We have used the built in `fetch` to do the network request of type `delete`. |
74 | 75 |
|
75 | 76 | Now, let's run the express server. |
76 | 77 |
|
77 | 78 | ```sh |
78 | | -DEBUG=todo-manager:* npm start |
| 79 | +npm start |
79 | 80 | ``` |
80 | | -Open the browser and visit `http://localhost:3000/todos` to view our to-do application. Now we should be able to click on thrash icon of the todo items and delete it. |
| 81 | + |
| 82 | +Open the browser and visit `http://localhost:3000/` to view our to-do application. Now we should be able to click on thrash icon of the todo items and delete it. |
0 commit comments