In this section, we’ll learn how to delete a post by sending a DELETE
HTTP request to our local json-server
. Luckily, deleting a post takes much less effort than updating it.
Adding Delete Buttons
The first thing we need to do is add a delete button next to each row in ListPosts.elm
.
When the Delete button is clicked, it sends the DeletePost
message to the Elm Runtime. Let’s add that message to the Msg
type in ListPosts.elm
.
Add a new branch to the update
function in ListPosts.elm
to handle the DeletePost
message.
Creating Delete HTTP Request
The DeletePost postId ->
branch delegates all work to the deletePost
function. Let’s add that below update
in ListPosts.elm
.
Once again, we’re using the low-level function Http.request
to create a delete request. To delete a resource on the server, we’re required to use the DELETE
method. The url
for deleting and updating a resource is exactly the same. The only information a server needs to delete a resource is its URL. That’s why we’ve assigned Http.emptyBody
to the body
field.
json-server
responds with {}
— an empty JSON object — when asked to delete a resource. Since there’s nothing to decode inside an empty JSON object, we passed Http.expectString
instead of Http.expectJson
to the expect
field. Next we need to add PostDeleted
to the Msg
type in ListPosts.elm
.
Just like the PostSaved
message from EditPost.elm
, the payload for PostDeleted
doesn’t need to be of type WebData
. We also aren’t interested in tracking all the states our DELETE
request goes through. All we need to know is whether the request is successful or not. The Result
type is perfect for that. Let’s handle PostDeleted
by adding two new branches to update
in ListPosts.elm
.
After a post is deleted, we want to refresh the list by re-fetching remaining posts. That’s why we’re returning a command to fetch posts from the PostDeleted (Ok response) ->
branch. However, if we encounter an error while deleting a post we need to save the error message in deleteError
. Let’s add that field to our model in ListPosts.elm
.
We also need to initialize it to Nothing
in init
. While we’re at it, let’s extract the code for initializing the model out to a separate function.
Displaying Delete Error
The only thing remaining is to display a delete error. Add the following code to the bottom of ListPosts.elm
.
And call viewDeleteError
from the view
function in ListPosts.elm
.
Testing
We’re now ready to test the delete functionality. Run json-server
and elm-live
from the beginning-elm
directory in separate terminal windows if they aren’t running already.
Check the elm-live
window in terminal to make sure everything compiled successfully. Now go to http://localhost:8000
and you should see a delete button on each row.
Click the delete button in first row and that post should disappear from the page.
Its JSON representation should also disappear from the server/db.json
file.
Additionally, json-server
deleted the following comment that was associated with the first post. Notice how the comment below uses postId
field to tie itself to the post we just deleted.
This is a common behavior on the server-side. When a resource is deleted, it’s likely that other dependent resources also get deleted.
Summary
In this section, we learned how to delete a post using the DELETE
HTTP method. Compared to other operations we have covered so far — fetching and updating — deleting a post is relatively easy. We don’t need to encode or decode any JSON value. All we need to do is specify the location of the resource. In the next section, we’ll build a separate page for creating a new post.