129 lines
4.7 KiB
TypeScript
129 lines
4.7 KiB
TypeScript
"use client"
|
|
|
|
import { useState } from "react";
|
|
import { useForm, SubmitHandler } from "react-hook-form";
|
|
import { useTranslations } from "next-intl";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { addPosition, editPosition } from "@/actions/positions";
|
|
import { toast } from "sonner";
|
|
import { Check, X } from "lucide-react";
|
|
import type { Position } from "@/types";
|
|
|
|
interface CreateProps {
|
|
mode: "create";
|
|
trigger: React.ReactNode;
|
|
onSuccess: (position: Position) => void;
|
|
}
|
|
|
|
interface EditProps {
|
|
mode: "edit";
|
|
position: Position;
|
|
trigger: React.ReactNode;
|
|
onSuccess: (position: Position) => void;
|
|
}
|
|
|
|
type Props = CreateProps | EditProps;
|
|
|
|
interface FormData {
|
|
code: string;
|
|
name: string;
|
|
description: string;
|
|
}
|
|
|
|
export function PositionDialog(props: Props) {
|
|
const t = useTranslations("MePage.items.admin.positions")
|
|
const isEdit = props.mode === "edit"
|
|
|
|
const [open, setOpen] = useState(false)
|
|
const { register, handleSubmit, reset, formState: { errors, isSubmitting } } = useForm<FormData>({
|
|
defaultValues: isEdit
|
|
? { code: props.position.code, name: props.position.name, description: props.position.description ?? "" }
|
|
: { code: "", name: "", description: "" },
|
|
})
|
|
|
|
function handleOpenChange(o: boolean) {
|
|
setOpen(o)
|
|
if (!o) {
|
|
reset(isEdit
|
|
? { code: props.position.code, name: props.position.name, description: props.position.description ?? "" }
|
|
: { code: "", name: "", description: "" }
|
|
)
|
|
}
|
|
}
|
|
|
|
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
|
const result = isEdit
|
|
? await editPosition((props as EditProps).position.code, data.name, data.description || undefined)
|
|
: await addPosition(data.code, data.name, data.description || undefined)
|
|
|
|
if (!result.success) {
|
|
toast(isEdit ? t("edit.error") : t("create.error"), { icon: <X size={16} /> })
|
|
return
|
|
}
|
|
|
|
const updated: Position = isEdit
|
|
? { ...(props as EditProps).position, name: data.name, description: data.description }
|
|
: { code: data.code, name: data.name, description: data.description, permissions: [] }
|
|
|
|
toast(isEdit ? t("edit.success") : t("create.success"), { icon: <Check size={16} /> })
|
|
props.onSuccess(updated)
|
|
reset()
|
|
setOpen(false)
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={handleOpenChange}>
|
|
<DialogTrigger asChild>{props.trigger}</DialogTrigger>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{isEdit ? t("edit.title") : t("create.title")}</DialogTitle>
|
|
</DialogHeader>
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<FieldGroup>
|
|
<Field data-invalid={!!errors.code}>
|
|
<FieldLabel htmlFor="code">{isEdit ? t("edit.code") : t("create.code")}</FieldLabel>
|
|
<Input
|
|
id="code"
|
|
disabled={isEdit || isSubmitting}
|
|
aria-invalid={!!errors.code}
|
|
{...register("code", { validate: (v) => isEdit || v.trim().length > 0 || t("create.codeRequired") })}
|
|
/>
|
|
{errors.code && <FieldError>{errors.code.message}</FieldError>}
|
|
</Field>
|
|
<Field data-invalid={!!errors.name}>
|
|
<FieldLabel htmlFor="name">{isEdit ? t("edit.name") : t("create.name")}</FieldLabel>
|
|
<Input
|
|
id="name"
|
|
disabled={isSubmitting}
|
|
aria-invalid={!!errors.name}
|
|
{...register("name", { validate: (v) => v.trim().length > 0 || t("create.nameRequired") })}
|
|
/>
|
|
{errors.name && <FieldError>{errors.name.message}</FieldError>}
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="description">{isEdit ? t("edit.description") : t("create.description")}</FieldLabel>
|
|
<Input
|
|
id="description"
|
|
disabled={isSubmitting}
|
|
{...register("description")}
|
|
/>
|
|
</Field>
|
|
</FieldGroup>
|
|
<DialogFooter className="mt-6">
|
|
<DialogClose asChild>
|
|
<Button variant="outline">{t("create.cancel") || t("edit.cancel")}</Button>
|
|
</DialogClose>
|
|
<Button type="submit" disabled={isSubmitting}>
|
|
{isSubmitting && <Spinner className="mr-2 size-4" />}
|
|
{isEdit ? t("edit.save") : t("create.save")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
} |