"use client" import * as React from "react" import { CheckIcon, MapPinIcon, PencilIcon, PlusIcon, XIcon } from "lucide-react" import { toast } from "sonner" import { getAccessDevices } from "@/lib/api/access-devices" import { assignPlaceDevice, createPlace, getPlaces, setPlaceStatus, updatePlace } from "@/lib/api/places" import type { Place } from "@/lib/models/place" import { useFetch } from "@/lib/use-fetch" import { useMutation, runMutation } from "@/lib/use-mutation" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Spinner } from "@/components/ui/spinner" import { Textarea } from "@/components/ui/textarea" import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip" import { DataTable, type DataTableColumn } from "@/components/hexa/data-table" import { InfoBanner } from "@/components/hexa/info-banner" import { PageSkeleton } from "@/components/hexa/page-skeleton" import { StatusBadge } from "@/components/hexa/status-badge" interface PlaceFormValues { name: string description: string } const EMPTY_VALUES: PlaceFormValues = { name: "", description: "" } interface PlaceDialogProps { open: boolean onOpenChange: (open: boolean) => void onSubmit: (values: PlaceFormValues) => Promise | void mode: "create" | "edit" initialValues?: Partial } function PlaceDialog({ open, onOpenChange, onSubmit, mode, initialValues }: PlaceDialogProps) { const [values, setValues] = React.useState(EMPTY_VALUES) const { busy, run } = useMutation() const [prevOpen, setPrevOpen] = React.useState(open) if (open !== prevOpen) { setPrevOpen(open) if (open) setValues({ ...EMPTY_VALUES, ...initialValues }) } const canSubmit = !!values.name.trim() && !busy async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!canSubmit) return try { await run(() => Promise.resolve(onSubmit(values))) onOpenChange(false) } catch { // run() already toasted the real error; keep the dialog open for retry. } } return (
{mode === "create" ? "Add Place" : "Edit Place"} A physical access zone (e.g. "Pool Area", "Indoor Gym"). Assign its fingerprint terminal from the table after creating it.
setValues((v) => ({ ...v, name: e.target.value }))} required />