import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@repo/shadcn-ui/components/button"; import { Card, CardContent } from "@repo/shadcn-ui/components/card"; import { Checkbox } from "@repo/shadcn-ui/components/checkbox"; import { FormActions, FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { Input } from "@repo/shadcn-ui/components/input"; import { Label } from "@repo/shadcn-ui/components/label"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@repo/shadcn-ui/components/table"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useId, useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import type { CompanyDTO, DepartmentDTO, DepartmentRequestDTO } from "../../common"; import { DepartmentRequestSchema } from "../../common"; import { EntityHelp } from "../access/entity-help"; import { companyApi, companyKeys } from "../api/company-api"; import { CompanyFeedback } from "./company-feedback"; function DepartmentForm({ companyId, department, csrfToken, onSaved, }: { companyId: string; department?: DepartmentDTO | undefined; csrfToken: string; onSaved: () => void; }) { const { t } = useTranslation("companies"); const id = useId(); const client = useQueryClient(); const form = useForm({ resolver: zodResolver(DepartmentRequestSchema), defaultValues: { name: department?.name ?? "", active: department?.active ?? true }, }); const mutation = useMutation({ mutationFn: (values: DepartmentRequestDTO) => companyApi.department(companyId, department?.id, values, csrfToken), onSuccess: (response) => { client.setQueryData(companyKeys.detail(companyId), response); onSaved(); }, }); return (
mutation.mutate(values))}>
form.setValue("active", checked === true, { shouldDirty: true }) } />
{department && (

{t("departmentDeactivationHelp")}

)} 0} /> ); } export function DepartmentSection({ company, csrfToken, }: { company: CompanyDTO; csrfToken: string; }) { const { t } = useTranslation("companies"); const [editor, setEditor] = useState(); const selected = company.departments.find((item) => item.id === editor); return (

{t("departments")}

{company.departments.length ? ( {t("name")} {t("status")} {t("people")} {t("actions")} {company.departments.map((department) => ( {department.name} {t(department.active ? "active" : "inactive")} { company.assignments.filter( (item) => item.departmentId === department.id && item.endDate === null ).length } ))}
) : (

{t("noDepartments")}

)} { if (!open) { setEditor(undefined); } }} open={editor !== undefined} title={t(selected ? "editDepartment" : "newDepartment")} > setEditor(undefined)} />
); }