diff --git a/web/app/dashboard/companies/_components/edit.tsx b/web/app/dashboard/companies/_components/edit.tsx deleted file mode 100644 index 5218fdc..0000000 --- a/web/app/dashboard/companies/_components/edit.tsx +++ /dev/null @@ -1,235 +0,0 @@ -"use client" - -import { useEffect, useMemo, useState } from "react" -import { zodResolver } from "@hookform/resolvers/zod" -import type { Resolver } from "react-hook-form" -import { useForm } from "react-hook-form" -import { z } from "zod/v4" - -import { ProjectDialog } from "@/components/project-dialog" -import { Button } from "@/components/ui/button" -import { - Field, - FieldContent, - FieldError, - FieldLabel, -} from "@/components/ui/field" -import { Input } from "@/components/ui/input" -import { Textarea } from "@/components/ui/textarea" -import { - fetchCompany, - type AdminCompany, - type CreateAdminCompanyPayload, -} from "@/lib/api/company" -import { useI18n } from "@/i18n/provider" - -type CompanyEditDialogProps = { - open: boolean - saving: boolean - itemId: number | null - initialValues?: Partial - onOpenChange: (open: boolean) => void - onSubmit: (payload: CreateAdminCompanyPayload) => Promise -} - -type EditForm = { - name: string - code: string - remark: string -} - -function createSchema(t: (key: string) => string) { - return z.object({ - name: z.string().trim().min(1, t("company.nameRequired")), - code: z.string().trim(), - remark: z.string().trim(), - }) -} - -const emptyForm: EditForm = { - name: "", - code: "", - remark: "", -} - -function buildForm(item: AdminCompany | null): EditForm { - if (!item) { - return emptyForm - } - return { - name: item.name, - code: item.code, - remark: item.remark, - } -} - -function buildPayload(form: EditForm): CreateAdminCompanyPayload { - return { - name: form.name.trim(), - code: form.code.trim(), - remark: form.remark.trim(), - } -} - -function buildInitialForm(initialValues?: Partial): EditForm { - return { - name: initialValues?.name?.trim() ?? "", - code: initialValues?.code?.trim() ?? "", - remark: initialValues?.remark?.trim() ?? "", - } -} - -export function EditDialog({ - open, - saving, - itemId, - initialValues, - onOpenChange, - onSubmit, -}: CompanyEditDialogProps) { - if (!open) { - return null - } - return ( - - ) -} - -type CompanyEditDialogBodyProps = CompanyEditDialogProps - -function CompanyEditDialogBody({ - open, - saving, - itemId, - initialValues, - onOpenChange, - onSubmit, -}: CompanyEditDialogBodyProps) { - const t = useI18n() - const formId = "company-edit-form" - const [loading, setLoading] = useState(false) - const companyFormSchema = useMemo(() => createSchema(t), [t]) - const editFormResolver = useMemo( - () => - zodResolver(companyFormSchema as never) as Resolver< - z.input, - undefined, - z.output - >, - [companyFormSchema] - ) - const form = useForm< - z.input, - undefined, - z.output - >({ - resolver: editFormResolver, - defaultValues: emptyForm, - }) - const { - handleSubmit, - register, - reset, - formState: { errors }, - } = form - - useEffect(() => { - async function loadDetail() { - if (!itemId) { - reset(buildInitialForm(initialValues)) - return - } - setLoading(true) - try { - const data = await fetchCompany(itemId) - reset(buildForm(data)) - } finally { - setLoading(false) - } - } - void loadDetail() - }, [initialValues, itemId, reset]) - - async function onFormSubmit(values: EditForm) { - await onSubmit(buildPayload(values)) - } - - return ( - - - - - } - > - {loading ? ( -
-
{t("company.loadingDetail")}
-
- ) : ( -
- - {t("company.columnName")} - - - - - - - {t("company.columnCode")} - - - - - - - {t("company.columnRemark")} - -