-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathform-delete.tsx
104 lines (97 loc) · 2.99 KB
/
form-delete.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useFetcher, useLocation } from "@remix-run/react"
import { useState } from "react"
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "~/components/ui/alert-dialog"
import { Button } from "~/components/ui/button"
import { ButtonLoading } from "~/components/ui/button-loading"
import { Iconify } from "~/components/ui/iconify"
export function FormDelete({
action,
intentValue,
itemText,
name = "id",
defaultValue,
buttonText = "Delete",
requireUser,
userId,
disabled,
extraComponent,
className,
customButton,
}: {
action: string // Example: /user/posts/delete
intentValue: string // Example: delete-post-by-id
itemText: string // Example: post name
name?: string // Optional because can be with/without input name=id
defaultValue?: string // Optional because can be with/without id value
buttonText?: string
requireUser?: boolean
userId?: string
disabled?: boolean
extraComponent?: React.ReactNode
className?: string
customButton?: React.ReactNode
}) {
const [open, setOpen] = useState<boolean>()
const location = useLocation()
const fetcher = useFetcher()
const isSubmitting =
fetcher.state === "submitting" && fetcher.formMethod === "DELETE"
return (
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger asChild className={className}>
{customButton || <Button variant="outline" size="xs" disabled={disabled}>
<Iconify icon="ph:trash-duotone" />
<span>{buttonText}</span>
</Button>}
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete {itemText}?</AlertDialogTitle>
<AlertDialogDescription>
This will permanently delete {itemText}. This action cannot be
undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<fetcher.Form
action={action || location.pathname}
method="DELETE"
onSubmit={event => {
fetcher.submit(event.currentTarget.form, { method: "DELETE" })
setOpen(false)
}}
>
{name && defaultValue && (
<input type="hidden" name={name} defaultValue={defaultValue} />
)}
{requireUser && userId && (
<input type="hidden" name="userId" defaultValue={userId} />
)}
{extraComponent}
<ButtonLoading
type="submit"
size="sm"
variant="destructive"
name="intent"
value={intentValue}
loadingText="Deleting"
isLoading={isSubmitting}
>
Delete
</ButtonLoading>
</fetcher.Form>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}