09 - Deleting with a Custom Event
The last step we have is to display a list of all the items for a given user. However, we're going to add a custom click listener to each item so that when item is clicked, it immediately deletes that item from the database. We'll do the display function first, then the delete.
Display All Items for a User
Put this at the bottom of your 03-auth-test.js
file. We'll cover what's happening here in a moment:
Analysis
This is a little different way of making a reference to a DOM element. We're aiming for a
<ul>
element with anid
offourteen
(the#
signals the program to look for anid
rather than aclass
).This should look familiar to you. This is the same way we cleared out the
<section>
elements in the NYT and YouTube API mini-apps.We use a
for of
loop to iterate through the values of eachkey: value
object pair.Given that we're working with a
<ul>
element, each loop will create a different<li>
.We create a string with the
id
andauthtestdata
properties, then put that string into the<li>
element.We add the
id
property of each object as an id for each<li>
. This will allow us to call them individually later.The
<li>
child element is added to the end of the<ul>
parent element.We add our custom listener to run whenever an
<li>
is clicked.
We need to create the removeItem
function before we test this part, or the app will break.
removeItem()
This function will delete an item from the <ul>
element. It will also have the ability to send a DELETE
request, but we're going to hold off on that part for a second. Add this function at the bottom of the 03-auth-test.js
file:
Analysis
Print
e
to the console to check which item we're clicking on.target
is a nested object withine
. This places that object inside its own variable.If the item we're clicking on isn't an
<li>
element, the emptyreturn
statement exits the conditional.We remove the
<li>
child from the<ul>
parent.Earlier we set an
id
for the<li>
. Now we get it back so we can pass it to theDELETE
request.This will become our
DELETE
request. In order for us to test what we have so far, we'll just printx
to the console.
Testing
If they aren't already running, start your client and server.
Refresh your browser, then click on
Display data
in step 14 (Delete Single Item From DOM).You should see the items from the
authtestdata
table with the matchingowner
value for our current user token:Click on one of the items. It should disappear from the list and print something like the following to the console:
Now that we can remove the element from its parent, we need to send a DELETE
request to remove the item from the database. One last step, and we're done!
deleteItemById()
Since this is a DELETE
request, put this function between the deleteItem()
and fetchFromOneDisplayData()
functions:
Analysis
Before we go further, go to the removeItem()
function. Comment out the last line (the console.log
) and uncomment out the line before it, which triggers this function. This function is nearly identical to the previous delete method, so it should look familiar.
The
id
of the<li>
is passed into this function as a parameter, which is then added to the url via the template literal.Print the response to the console to verify the delete worked.
Run the
getall
function again to print the remaining items in the database to the console.
Now for the final test. Follow the instructions from the Testing section above, but this time, you should see something like this: Pre-delete
Post-delete
Wrap Up
And that's it! That's everything you need to build your own CRUD app, both client side and server side. If there's anything that you find unclear, please ask us so that we can help, but try on your own first. The next step is a new challenge, so when you're ready, move on to the next chapter.
Last updated