By now, ListBooksComponent
files and BMS service should be look like this,
In this section, we will add the last feature of the BMS, which is book deletion. As we have done in earlier parts, we will need to modify the module's service, templates and components.
We start by defining two functions in the BMS service, deleteBook
and deleteBookByID
:
deleteBook
is responsible for communicating with the API endpoint. We pass to it the book's ISBN as a parameter and the latter will be used as the endpoint's parameter to delete the book from the database.
deleteBookByID
will delete the book from the local list that we have created so that we avoid fetching the list again and provide a better UX.
In deleteBook
we are using the HTTP Client to make a DELETE
HTTP request to the backend server through the API endpoint. Note that we are not specifying the response's type (unlike what we did when fetching the list of books), that is why the function's call type parameter is set to any
.
Once the request has been successfully made, we log the response to the console at line 14 (though this is an optional step, we are just doing so as a sanity check), then we invoke deleteBookByID
to delete the book from the local list.
Caution: Again, in an ideal setting you should properly handle errors by displaying them to your user so they would be aware if the system is not acting properly.
Coming to the deleteBookByID
function, we first create an auxiliary variable of type Book[]
where we store the subject's current value and we operate on that variable. We iterate over the array to find the book with that specific ID. Once found, we delete it from the array using the method splice
, that takes the starting index and the number of elements to be deleted (in the case it is just 1). Finally, we broadcast our new list to the subject.
We will now update the ListBooksComponent
, so that we allow the user to actually delete books from the list. What we will do first is to add some changes to the template (i.e. the HTML file) of the component and then we will modify the component's TypeScript class.
Basically, what we want to achieve is to display a red button labeled "Delete" , when clicked, will remove the row from the table and display a message confirming that indeed the book has been removed.
Go to list-books.component.html
and modify it as follow,
What we have done is that we have simply created a button inside the for
loop (so that it is created at each row) and we have bound a function deleteBook()
that takes the book's ISBN as a parameter to the click
event (if you want to read more about Event Binding
in Angular). What will happen, is that if the user clicks on the button, that function will be triggered.
What remains now is to add the confirmation message once the user has deleted the book. Go to the top of the file add the following code block,
If you look closely, you will notice that the message has an if
directive, and this makes perfect sens since we do not want the message to be always displayed, rather, it should be visible only after deleting, so its status would depend on the value of bookRemoved
(which is a boolean variable).
The last step is to define the functions and variables (deleteBook()
and bookRemoved
) in the component's logic.
What the deleteBook()
is actually doing, is calling the previous method that we have implemented in the Book Management System's Service that is responsible for deleting books from the list and database , and then we are changing the state of bookRemoved
to true
so that the confirmation message is displayed.