In this episode, we're tackling how to let users cancel an appointment. We start by updating our backend so that when a booking is canceled, the cancelled_at
column in the database gets set to the current timestamp. This helps the system know the slot is available again.
But canceling bookings isn't just about updating the database—we also want to do this securely. We set up a dedicated route and controller for deleting (canceling) an appointment, making sure to use CSRF protection and spoof the HTTP DELETE method properly since forms only support POST out of the box.
On the frontend, we swap out anchor links for a form button to trigger the cancellation. To make sure users don't accidentally cancel, we add a simple browser confirmation using Alpine.js—so when you hit "Cancel Booking", you have to confirm first.
Finally, we polish off the user experience by updating the UI when an appointment is canceled. The "Cancel" button only shows if the appointment is still active, and the appointment title updates to indicate when one has been canceled. By the end, it's all wrapped up cleanly, and you have a secure, smooth cancel booking flow!