In this episode, we dive into how to let users delete tasks in our app, and we specifically explore working with browser dialogs—the little popups that ask for confirmation when you try to delete something (like the classic "Are you sure?"). We'll build this functionality step by step, connecting the front end with the backend, and ensuring everything flows seamlessly.
First, we refactor our task item into its own component and add a "Delete" button, hooking it up with an onClick
handler. When a user clicks delete, we use the browser's native window.confirm
dialog to ask if they're sure. If they confirm, we send a delete request to the backend using Inertia's router, and if not, nothing happens.
On the backend, we add a simple destroy method to actually remove the task. We also set up the necessary route to handle the delete request and wire everything together.
But, that's not all! We look at how to write automated browser tests (using Laravel Dusk) to make sure this works as expected. This includes handling and interacting with browser dialogs in tests—specifically, how to accept or cancel them. We also make sure to assert that the task is really gone from both the interface and the database, so we don't end up with phantom deletions that only look successful.
To wrap up, we clean up the delete button selector in our tests to make things robust if the button text ever changes. The end result: we have a solid delete flow, everything is tested, and you know how to work with browser dialogs, whether they're native alerts or future custom modals!