|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React from "react"; |
| 4 | +import BogModal from "@/components/BogModal/BogModal"; |
| 5 | +import BogButton from "@/components/BogButton/BogButton"; |
| 6 | + |
| 7 | +type ConfirmActionModalProps = { |
| 8 | + open: boolean; |
| 9 | + onOpenChange: (open: boolean) => void; |
| 10 | + onConfirm: () => void | Promise<void>; |
| 11 | + /** Modal heading, e.g. "Cancel this ride?" */ |
| 12 | + title: string; |
| 13 | + /** Body copy explaining what the action does. */ |
| 14 | + description: React.ReactNode; |
| 15 | + /** Label for the destructive confirm button, e.g. "Cancel ride". */ |
| 16 | + confirmLabel: string; |
| 17 | + /** Label shown on the confirm button while the request is in flight. */ |
| 18 | + confirmingLabel?: string; |
| 19 | + /** Label for the dismiss button. */ |
| 20 | + cancelLabel?: string; |
| 21 | + /** True while the confirmed request is running — disables the buttons and |
| 22 | + * prevents the modal from being dismissed. */ |
| 23 | + confirming?: boolean; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Confirmation dialog for destructive admin actions (ride cancellation, |
| 28 | + * user/vehicle deletion). Built on BogModal with the standard palette: |
| 29 | + * navy dismiss button, red confirm button. |
| 30 | + */ |
| 31 | +export default function ConfirmActionModal({ |
| 32 | + open, |
| 33 | + onOpenChange, |
| 34 | + onConfirm, |
| 35 | + title, |
| 36 | + description, |
| 37 | + confirmLabel, |
| 38 | + confirmingLabel, |
| 39 | + cancelLabel = "Nevermind", |
| 40 | + confirming = false, |
| 41 | +}: ConfirmActionModalProps) { |
| 42 | + // While the request is in flight, don't let the modal be dismissed |
| 43 | + // (Escape, outside click, or the close button). |
| 44 | + const setOpen = (value: React.SetStateAction<boolean>) => { |
| 45 | + const next = typeof value === "function" ? value(open) : value; |
| 46 | + if (confirming && !next) return; |
| 47 | + onOpenChange(next); |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <BogModal |
| 52 | + size="medium" |
| 53 | + modal |
| 54 | + openState={{ open, setOpen }} |
| 55 | + trigger={ |
| 56 | + <button |
| 57 | + type="button" |
| 58 | + aria-hidden |
| 59 | + tabIndex={-1} |
| 60 | + style={{ display: "none" }} |
| 61 | + /> |
| 62 | + } |
| 63 | + title={<h3>{title}</h3>} |
| 64 | + > |
| 65 | + <div className="flex flex-col gap-[2.4rem]"> |
| 66 | + <p className="m-0 text-paragraph-1 text-[var(--color-grey-text-strong)]"> |
| 67 | + {description} |
| 68 | + </p> |
| 69 | + <div className="flex flex-wrap justify-end gap-[1.2rem]"> |
| 70 | + <BogButton |
| 71 | + variant="secondary" |
| 72 | + size="medium" |
| 73 | + disabled={confirming} |
| 74 | + onClick={() => setOpen(false)} |
| 75 | + style={{ borderRadius: "0.5rem" }} |
| 76 | + > |
| 77 | + {cancelLabel} |
| 78 | + </BogButton> |
| 79 | + <BogButton |
| 80 | + variant="primary" |
| 81 | + size="medium" |
| 82 | + disabled={confirming} |
| 83 | + onClick={() => void onConfirm()} |
| 84 | + style={ |
| 85 | + { |
| 86 | + "--color-brand-text": "var(--color-status-red-text)", |
| 87 | + "--color-brand-hover": "var(--color-status-red-stroke-strong)", |
| 88 | + borderRadius: "0.5rem", |
| 89 | + } as React.CSSProperties |
| 90 | + } |
| 91 | + > |
| 92 | + {confirming ? (confirmingLabel ?? confirmLabel) : confirmLabel} |
| 93 | + </BogButton> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </BogModal> |
| 97 | + ); |
| 98 | +} |
0 commit comments