You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
"use client";
|
|
|
|
import { useActionState, useState } from "react";
|
|
import { Loader2Icon, Trash2Icon } from "lucide-react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
AlertDialogTrigger,
|
|
} from "@/components/ui/alert-dialog";
|
|
import { hapusRak, type HapusRakState } from "../actions";
|
|
|
|
type HapusRakButtonProps = {
|
|
idRak: number;
|
|
nama: string;
|
|
};
|
|
|
|
const initial: HapusRakState = { ok: false };
|
|
|
|
export function HapusRakButton({ idRak, nama }: HapusRakButtonProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const [state, formAction, pending] = useActionState(
|
|
async (prevState: HapusRakState, formData: FormData) => {
|
|
const result = await hapusRak(prevState, formData);
|
|
if (result.ok) setOpen(false);
|
|
return result;
|
|
},
|
|
initial,
|
|
);
|
|
|
|
return (
|
|
<AlertDialog open={open} onOpenChange={setOpen}>
|
|
<AlertDialogTrigger
|
|
render={
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="text-destructive hover:bg-destructive/10"
|
|
title="Hapus rak"
|
|
/>
|
|
}
|
|
>
|
|
<Trash2Icon />
|
|
<span className="sr-only">Hapus</span>
|
|
</AlertDialogTrigger>
|
|
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Hapus {nama || `rak ${idRak}`}?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Data rak {idRak} akan dihapus permanen.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
|
|
<form action={formAction}>
|
|
<input type="hidden" name="idRak" value={idRak} />
|
|
{state.error && (
|
|
<p className="mb-4 text-sm text-destructive">{state.error}</p>
|
|
)}
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={pending}>Batal</AlertDialogCancel>
|
|
<Button type="submit" variant="destructive" disabled={pending}>
|
|
{pending && <Loader2Icon className="size-4 animate-spin" />}
|
|
Hapus
|
|
</Button>
|
|
</AlertDialogFooter>
|
|
</form>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
);
|
|
}
|