109 lines
4.1 KiB
TypeScript
109 lines
4.1 KiB
TypeScript
"use client"
|
|
|
|
import { useAuthApi } from "@/shared/useApi"
|
|
import { useRouter } from "next/navigation"
|
|
import { Button } from "@/shared/components/ui/button"
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/shared/components/ui/dialog"
|
|
import { ScrollArea } from "@/shared/components/ui/scroll-area"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/shared/components/ui/dropdown-menu"
|
|
import { Ellipsis, Pencil, Trash2, Play, CheckCircle2 } from "lucide-react"
|
|
import { toast } from "sonner"
|
|
import { useFormDialog } from "@/shared/components/form-dialog"
|
|
import { InspectionForm } from "./inspection-form"
|
|
|
|
type InspectionActionsProps = {
|
|
inspectionId: string
|
|
status?: string
|
|
onStatusChange?: () => void
|
|
}
|
|
|
|
const STATUS_TRANSITIONS: Record<string, { next: string; label: string; icon: typeof Play }> = {
|
|
open: { next: "in_progress", label: "Start Inspection", icon: Play },
|
|
in_progress: { next: "completed", label: "Mark Completed", icon: CheckCircle2 },
|
|
}
|
|
|
|
export function InspectionActions({ inspectionId, status, onStatusChange }: InspectionActionsProps) {
|
|
const api = useAuthApi()
|
|
const router = useRouter()
|
|
const editDialog = useFormDialog("inspection-details-edit")
|
|
|
|
const handleDelete = async () => {
|
|
const promise = api.inspections.destroy(inspectionId)
|
|
toast.promise(promise, {
|
|
loading: "Deleting inspection...",
|
|
success: "Inspection deleted successfully",
|
|
error: "Failed to delete inspection",
|
|
})
|
|
await promise
|
|
router.push("/sales/inspections")
|
|
}
|
|
|
|
const handleStatusChange = async (newStatus: string) => {
|
|
const promise = api.inspections.changeStatus({
|
|
status: newStatus,
|
|
} as never)
|
|
toast.promise(promise, {
|
|
loading: "Updating status...",
|
|
success: "Status updated successfully",
|
|
error: "Failed to update status",
|
|
})
|
|
await promise
|
|
onStatusChange?.()
|
|
router.refresh()
|
|
}
|
|
|
|
const transition = status ? STATUS_TRANSITIONS[status] : undefined
|
|
|
|
return (
|
|
<>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon">
|
|
<Ellipsis className="size-4" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem onClick={() => editDialog.open(inspectionId)}>
|
|
<Pencil className="size-4" />
|
|
Edit
|
|
</DropdownMenuItem>
|
|
{transition && (
|
|
<DropdownMenuItem onClick={() => handleStatusChange(transition.next)}>
|
|
<transition.icon className="size-4" />
|
|
{transition.label}
|
|
</DropdownMenuItem>
|
|
)}
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem variant="destructive" onClick={handleDelete}>
|
|
<Trash2 className="size-4" />
|
|
Delete
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
|
|
<Dialog open={editDialog.isOpen} onOpenChange={(v) => { if (!v) editDialog.close() }}>
|
|
<DialogContent className="min-w-xl lg:min-w-4xl">
|
|
<DialogHeader>
|
|
<DialogTitle className="text-2xl font-bold">Edit Inspection</DialogTitle>
|
|
</DialogHeader>
|
|
<ScrollArea className="max-h-[80vh] px-4">
|
|
<InspectionForm
|
|
resourceId={editDialog.resourceId}
|
|
onSuccess={() => {
|
|
editDialog.close()
|
|
router.refresh()
|
|
}}
|
|
/>
|
|
</ScrollArea>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
)
|
|
}
|