Improving User Safety: Building a Reusable Confirmation Dialog
In the admin-ecommerce project, we are constantly refining our admin interface to prevent accidental data loss. One of the most critical UI patterns in a CMS is the 'Delete' action. If a user clicks a delete button by mistake, they shouldn't immediately trigger a server request. We need a safety net.
The Problem: Accidental Deletions
Previously, our category management page lacked a protective layer. A single click on a button was enough to wipe a category from the database. While efficient for power users, it is a recipe for disaster in a fast-paced admin environment.
The Solution: A Dedicated Confirmation Component
I implemented a reusable ConfirmationDialog component using React. Instead of hard-coding logic into each view, we now have a centralized component that handles the 'Are you sure?' flow.
Here is the pattern we adopted for our React components:
const CategoryDeleteButton = ({ onConfirm }) => {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<button onClick={() => setIsOpen(true)}>Delete Category</button>
<ConfirmationDialog
open={isOpen}
onClose={() => setIsOpen(false)}
onConfirm={onConfirm}
message="Are you sure you want to delete this category?"
/>
</>
);
};
The Workflow
By decoupling the dialog logic from the business logic, we gain several advantages:
- Consistency: All delete actions across the dashboard look and feel identical.
- Reduced Boilerplate: We no longer need to manage local state for dialogs inside every parent page.
- Safety: The user is forced to make a deliberate choice before an irreversible API call is dispatched.
The Takeaway
Don't let your UI assume the user's intent. When dealing with destructive actions, always implement a confirmation layer. Moving this into a generic component makes your codebase cleaner and your users' data much safer.
Generated with Gitvlg.com