metis-desk/modules/companies/src/web/components/configuration-quick-editor.tsx

128 lines
3.5 KiB
TypeScript
Raw Normal View History

import { AssetForm, catalogApi, catalogKeys } from "@metis/assets/client";
import { FormDialog } from "@repo/shadcn-ui/components/form-dialog";
import { useQuery } from "@tanstack/react-query";
import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import type { AccessResponseDTO, CompanyDTO } from "../../common";
import { AccessForm } from "../access/access-form";
import { PersonForm } from "../components/person-form";
2026-10-05 07:53:29 +00:00
import { MailFeedback } from "../mail/mail-feedback";
export type ConfigurationQuickKind = "person" | "account" | "asset" | "authorization";
const titles = {
person: "newPerson",
account: "newAccount",
asset: "newAsset",
authorization: "authorize",
} as const;
2026-10-05 07:53:29 +00:00
export function ConfigurationQuickEditor({
kind,
2026-10-05 15:01:32 +00:00
title,
company,
csrfToken,
values,
data,
onCreated,
onClose,
}: {
2026-10-05 07:53:29 +00:00
kind: ConfigurationQuickKind;
2026-10-05 15:01:32 +00:00
title?: string;
company: CompanyDTO;
csrfToken: string;
2026-10-05 07:53:29 +00:00
values: Readonly<{ personId: string; accountId: string }>;
data: AccessResponseDTO | undefined;
onCreated: (field: "personId" | "accountId" | "assetId", id?: string) => void;
onClose: () => void;
}) {
const { t } = useTranslation("companies");
const active = useRef(true);
useEffect(() => {
active.current = true;
return () => {
active.current = false;
};
}, []);
const created = (field: "personId" | "accountId" | "assetId", id?: string) => {
if (active.current) {
onCreated(field, id);
}
};
const close = () => {
active.current = false;
onClose();
};
const catalog = useQuery({
queryKey: catalogKeys.scope(company.id),
queryFn: () => catalogApi.get(company.id),
enabled: kind === "asset",
});
return (
<FormDialog
cancelLabel={t("cancel")}
formActions
onOpenChange={(open) => {
if (!open) {
close();
}
}}
open
2026-10-05 15:01:32 +00:00
title={title ?? t(`mail.${titles[kind]}`)}
wide
>
{kind === "person" && (
<PersonForm
companyId={company.id}
csrfToken={csrfToken}
onSaved={(id) => created("personId", id)}
requireActive
/>
)}
{(kind === "account" || kind === "authorization") && data && (
<AccessForm
allowQuickCreate={false}
csrfToken={csrfToken}
data={data}
initialValues={
kind === "authorization"
? { personId: values.personId, accountId: values.accountId }
: {}
}
kind={kind === "account" ? "account" : "account_person"}
lockedFields={kind === "authorization" ? ["personId", "accountId"] : []}
onSaved={(id) => {
if (kind === "account") {
created("accountId", id);
} else {
close();
}
}}
people={company.people}
scope={company.id}
/>
)}
{kind === "asset" &&
(catalog.data ? (
<AssetForm
allowQuickCreate={false}
companyId={company.id}
csrfToken={csrfToken}
items={catalog.data.items}
lookups={{
sites: company.sites,
people: company.people,
onCreatePerson: () => undefined,
onCreateSite: () => undefined,
}}
onSaved={(id) => created("assetId", id)}
/>
) : (
<>
<p role="status">{t("loading")}</p>
<MailFeedback error={catalog.error} />
</>
))}
</FormDialog>
);
}