|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { deleteTaskAction } from "../actions/delete-task"; |
| 4 | +import { Task } from "@comp/db/types"; |
| 5 | +import { Button } from "@comp/ui/button"; |
| 6 | +import { |
| 7 | + Dialog, |
| 8 | + DialogContent, |
| 9 | + DialogDescription, |
| 10 | + DialogFooter, |
| 11 | + DialogHeader, |
| 12 | + DialogTitle, |
| 13 | +} from "@comp/ui/dialog"; |
| 14 | +import { Form } from "@comp/ui/form"; |
| 15 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 16 | +import { Trash2 } from "lucide-react"; |
| 17 | +import { useAction } from "next-safe-action/hooks"; |
| 18 | +import { useRouter } from "next/navigation"; |
| 19 | +import { useState } from "react"; |
| 20 | +import { useForm } from "react-hook-form"; |
| 21 | +import { toast } from "sonner"; |
| 22 | +import { z } from "zod"; |
| 23 | + |
| 24 | +const formSchema = z.object({ |
| 25 | + comment: z.string().optional(), |
| 26 | +}); |
| 27 | + |
| 28 | +type FormValues = z.infer<typeof formSchema>; |
| 29 | + |
| 30 | +interface TaskDeleteDialogProps { |
| 31 | + isOpen: boolean; |
| 32 | + onClose: () => void; |
| 33 | + task: Task; |
| 34 | +} |
| 35 | + |
| 36 | +export function TaskDeleteDialog({ |
| 37 | + isOpen, |
| 38 | + onClose, |
| 39 | + task, |
| 40 | +}: TaskDeleteDialogProps) { |
| 41 | + const router = useRouter(); |
| 42 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 43 | + |
| 44 | + const form = useForm<FormValues>({ |
| 45 | + resolver: zodResolver(formSchema), |
| 46 | + defaultValues: { |
| 47 | + comment: "", |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const deleteTask = useAction(deleteTaskAction, { |
| 52 | + onSuccess: () => { |
| 53 | + toast.info("Task deleted! Redirecting to tasks list..."); |
| 54 | + onClose(); |
| 55 | + router.push(`/${task.organizationId}/tasks`); |
| 56 | + }, |
| 57 | + onError: () => { |
| 58 | + toast.error("Failed to delete task."); |
| 59 | + setIsSubmitting(false); |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + const handleSubmit = async (values: FormValues) => { |
| 64 | + setIsSubmitting(true); |
| 65 | + deleteTask.execute({ |
| 66 | + id: task.id, |
| 67 | + entityId: task.id, |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}> |
| 73 | + <DialogContent className="sm:max-w-[425px]"> |
| 74 | + <DialogHeader> |
| 75 | + <DialogTitle>Delete Task</DialogTitle> |
| 76 | + <DialogDescription> |
| 77 | + Are you sure you want to delete this task? This action |
| 78 | + cannot be undone. |
| 79 | + </DialogDescription> |
| 80 | + </DialogHeader> |
| 81 | + <Form {...form}> |
| 82 | + <form |
| 83 | + onSubmit={form.handleSubmit(handleSubmit)} |
| 84 | + className="space-y-4" |
| 85 | + > |
| 86 | + <DialogFooter className="gap-2"> |
| 87 | + <Button |
| 88 | + type="button" |
| 89 | + variant="outline" |
| 90 | + onClick={onClose} |
| 91 | + disabled={isSubmitting} |
| 92 | + > |
| 93 | + Cancel |
| 94 | + </Button> |
| 95 | + <Button |
| 96 | + type="submit" |
| 97 | + variant="destructive" |
| 98 | + disabled={isSubmitting} |
| 99 | + className="gap-2" |
| 100 | + > |
| 101 | + {isSubmitting ? ( |
| 102 | + <span className="flex items-center gap-2"> |
| 103 | + <span className="h-3 w-3 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 104 | + Deleting... |
| 105 | + </span> |
| 106 | + ) : ( |
| 107 | + <span className="flex items-center gap-2"> |
| 108 | + <Trash2 className="h-3 w-3" /> |
| 109 | + Delete |
| 110 | + </span> |
| 111 | + )} |
| 112 | + </Button> |
| 113 | + </DialogFooter> |
| 114 | + </form> |
| 115 | + </Form> |
| 116 | + </DialogContent> |
| 117 | + </Dialog> |
| 118 | + ); |
| 119 | +} |
0 commit comments