Files

195 lines
8.5 KiB
TypeScript

// R-P6 (P6 CRM core) Interim-UI (AD-28) — contact custom-field admin (D-CRM
// "Custom-Felder", archetype A2), composed from ~/ui. Pure/presentational: renders
// the server-built CustomFieldAdminVM and calls actions.onArchive / onCreate; the
// route POSTs the intents. Final Subframe optics = tracked design-nachzug.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherSlidersHorizontal } from "@subframe/core";
import type { CustomFieldAdminVM, CustomFieldDatentyp } from "@hivegrid/view-models";
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { Button } from "~/ui/components/Button";
import { Card } from "~/ui/components/Card";
import { EmptyState } from "~/ui/components/EmptyState";
import { Select } from "~/ui/components/Select";
import { Table } from "~/ui/components/Table";
import { TextInput } from "~/ui/components/TextInput";
const DATENTYPEN: readonly CustomFieldDatentyp[] = [
"text",
"textarea",
"zahl",
"waehrung",
"datum",
"datum_range",
"select",
"multiselect",
"boolean",
"email",
"telefon",
"datei",
"prozent",
"datum_zeit",
"person_team",
"adresse",
"link",
];
export type CustomFieldCreateInput = { key: string; label: string; datentyp: string; optionen: string };
export type CustomFieldAdminPageActions = {
onCreate: (input: CustomFieldCreateInput) => void;
onArchive: (definitionId: string) => void;
};
export type CustomFieldAdminPageProps = {
vm: CustomFieldAdminVM;
moduleActive: boolean;
submitting: boolean;
errorKey?: string | null;
actions: CustomFieldAdminPageActions;
};
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
export function CustomFieldAdminPage(props: CustomFieldAdminPageProps) {
const { t } = useTranslation();
const [draft, setDraft] = useState<CustomFieldCreateInput>({ key: "", label: "", datentyp: "text", optionen: "" });
const set = (patch: Partial<CustomFieldCreateInput>) => setDraft((s) => ({ ...s, ...patch }));
if (!props.moduleActive) {
return (
<PageShell uiId="ui.crm.admin.customFields">
<PageHeader
icon={<FeatherSlidersHorizontal className={PAGE_HEADER_ICON_CLASS} />}
title={t("crm.admin.customFields.title")}
subtitle={t("crm.admin.customFields.subtitle")}
/>
<PageBody>
<div data-ui-id="ui.crm.admin.customFields.locked">
<EmptyState title={t("crm.locked.headline")} body={t("crm.locked.body")} />
</div>
</PageBody>
</PageShell>
);
}
const rows = props.vm.status === "ready" && props.vm.data ? props.vm.data.rows : [];
return (
<PageShell uiId="ui.crm.admin.customFields">
<PageHeader
icon={<FeatherSlidersHorizontal className={PAGE_HEADER_ICON_CLASS} />}
title={t("crm.admin.customFields.title")}
subtitle={t("crm.admin.customFields.subtitle")}
/>
<PageBody>
{props.errorKey ? <BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback> : null}
{/* Create first, list below: this page exists to add fields, and the
table grows over time — a form pushed below it drifts out of reach. */}
<Card data-ui-id="ui.crm.admin.customFields.create">
<span className={sectionHeading}>{t("crm.admin.customFields.addHeading")}</span>
{/* The inputs share the card's full width evenly (basis-0 + grow), so
they line up as one band instead of sizing themselves by content. */}
<div className="flex w-full flex-col items-stretch gap-4 sm:flex-row sm:items-end">
<TextInput className="w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0" label={t("crm.admin.customFields.colKey")} showLabel>
<TextInput.Input value={draft.key} onChange={(e) => set({ key: e.target.value })} data-ui-id="ui.crm.admin.customFields.create.key" />
</TextInput>
<TextInput className="w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0" label={t("crm.admin.customFields.colLabel")} showLabel>
<TextInput.Input value={draft.label} onChange={(e) => set({ label: e.target.value })} data-ui-id="ui.crm.admin.customFields.create.label" />
</TextInput>
<Select
className="w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0"
label={t("crm.admin.customFields.colType")}
showLabel
value={draft.datentyp}
onValueChange={(v) => set({ datentyp: v })}
>
{DATENTYPEN.map((dt) => (
<Select.Item key={dt} value={dt}>
{t(`crm.datentyp.${dt}`)}
</Select.Item>
))}
</Select>
<TextInput className="w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0" label={t("crm.admin.customFields.colOptions")} showLabel>
<TextInput.Input
value={draft.optionen}
onChange={(e) => set({ optionen: e.target.value })}
placeholder={t("crm.admin.customFields.optionsPlaceholder")}
/>
</TextInput>
<Button
className="shrink-0"
disabled={!draft.key.trim() || !draft.label.trim()}
onClick={() => {
props.actions.onCreate(draft);
setDraft({ key: "", label: "", datentyp: "text", optionen: "" });
}}
data-ui-id="ui.crm.admin.customFields.create.submit"
>
{t("crm.actions.create")}
</Button>
</div>
</Card>
{rows.length > 0 ? (
<div className="w-full overflow-hidden rounded-lg border border-solid border-border-default shadow-sm overflow-x-auto">
<Table
data-ui-id="ui.crm.admin.customFields.table"
header={
<Table.HeaderRow>
<Table.HeaderCell>{t("crm.admin.customFields.colKey")}</Table.HeaderCell>
<Table.HeaderCell>{t("crm.admin.customFields.colLabel")}</Table.HeaderCell>
<Table.HeaderCell>{t("crm.admin.customFields.colType")}</Table.HeaderCell>
{/* Action column: just wide enough for the button, centred. */}
<Table.HeaderCell className="w-24"> </Table.HeaderCell>
</Table.HeaderRow>
}
>
{rows.map((row) => (
<Table.Row key={row.id} data-row-id={row.id}>
<Table.Cell>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">{row.key}</span>
</Table.Cell>
<Table.Cell>
<div className="flex items-center gap-2">
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{row.label}</span>
{row.isDemoData ? <BadgeFeedback farbe="subtle-neutral" size="small">{t("crm.admin.demoBadge")}</BadgeFeedback> : null}
{row.archiviert ? <BadgeFeedback farbe="subtle-neutral" size="small">{t("crm.admin.archivedBadge")}</BadgeFeedback> : null}
</div>
</Table.Cell>
<Table.Cell>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">{t(row.datentypLabelKey)}</span>
</Table.Cell>
<Table.Cell className="w-24">
{!row.archiviert ? (
<div className="flex w-full justify-center">
{/* Labelled "delete" but backed by the archive intent: the
definition keeps existing so the values already stored
on contacts are never dropped. */}
<Button
variant="tertiary"
size="small"
loading={props.submitting}
onClick={() => props.actions.onArchive(row.id)}
data-ui-id={`ui.crm.admin.customFields.row.delete.${row.id}`}
>
{t("crm.actions.delete")}
</Button>
</div>
) : null}
</Table.Cell>
</Table.Row>
))}
</Table>
</div>
) : null}
</PageBody>
</PageShell>
);
}
export default CustomFieldAdminPage;