| 1 | 'use client' |
| 2 | |
| 3 | import type { ReactNode } from 'react' |
| 4 | import { useEffect, useState |
| 5 | import { Button } |
| 6 | import { |
| 7 | Dialog, |
| 8 | DialogContent, |
| 9 | DialogDescription, |
| 10 | DialogFooter, |
| 11 | DialogHeader, |
| 12 | DialogTitle, |
| 13 | } from '@/components/ui/dialog' |
| 14 | import { Field } |
| 15 | import { Input } |
| 16 | import { Label } |
| 17 | |
| 18 | interface Props { |
| 19 | actionLabel?: string |
| 20 | confirmationText: string |
| 21 | description: ReactNode |
| 22 | loading?: boolean |
| 23 | onConfirm: () |
| 24 | onOpenChange: (open |
| 25 | open: boolean |
| 26 | title: string |
| 27 | } |
| 28 | |
| 29 | export function ConfirmDeleteDialog({ |
| 30 | open, |
| 31 | onOpenChange, |
| 32 | title, |
| 33 | description, |
| 34 | confirmationText, |
| 35 | actionLabel = 'Delete' |
| 36 | loading = false, |
| 37 | onConfirm, |
| 38 | }: Props) |
| 39 | const [value, |
| 40 | |
| 41 | useEffect(() => |
| 42 | if (!open) |
| 43 | setValue('') |
| 44 | } |
| 45 | }, [open]) |
| 46 | |
| 47 | const matches = |
| 48 | |
| 49 | const handleSubmit = |
| 50 | e.preventDefault() |
| 51 | if (!matches) |
| 52 | return |
| 53 | } |
| 54 | await onConfirm() |
| 55 | } |
| 56 | |
| 57 | return ( |
| 58 | <Dialog onOpenChange={ |
| 59 | <DialogContent className=' |
| 60 | <DialogHeader> |
| 61 | <DialogTitle>{title} |
| 62 | <DialogDescription>{description} |
| 63 | </DialogHeader> |
| 64 | <form onSubmit={handleSubmit |
| 65 | <Field className=' |
| 66 | <Label> |
| 67 | Type |
| 68 | <span className='max-w-fit font-semibold |
| 69 | "{confirmationText}" |
| 70 | </span> |
| 71 | to confirm |
| 72 | </Label> |
| 73 | <Input |
| 74 | autoFocus |
| 75 | onChange={(e) |
| 76 | placeholder={confirmationText} |
| 77 | value={value} |
| 78 | /> |
| 79 | </Field> |
| 80 | <DialogFooter className=' |
| 81 | <Button |
| 82 | onClick={() => |
| 83 | type='button' |
| 84 | variant='outline' |
| 85 | > |
| 86 | Cancel |
| 87 | </Button> |
| 88 | <Button |
| 89 | disabled={!matches} |
| 90 | loading={loading} |
| 91 | type='submit' |
| 92 | variant='destructive' |
| 93 | > |
| 94 | {actionLabel} |
| 95 | </Button> |
| 96 | </DialogFooter> |
| 97 | </form> |
| 98 | </DialogContent> |
| 99 | </Dialog> |
| 100 | ) |
| 101 | } |