146 lines
9.1 KiB
TypeScript
146 lines
9.1 KiB
TypeScript
import { useMemo, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { AcceptanceListScreenVM } from "@hivegrid/view-models";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { TextArea } from "~/ui/components/TextArea";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
|
|
export type AcceptanceNewPageProps = {
|
|
vm: AcceptanceListScreenVM;
|
|
submitting: boolean;
|
|
errorKey?: string | null;
|
|
actions: { onSubmit: (intent: string, fields?: Record<string, string>) => void };
|
|
};
|
|
|
|
type DefectDraft = { classification: "minor" | "critical" | "cosmetic"; title: string; photoFileId: string };
|
|
type SignatureDraft = { signerRole: "kunde" | "monteur"; signerName: string; signatureFileId: string };
|
|
|
|
const headingText = "text-body-large-0 font-body-large-0 text-text-neutral-primary";
|
|
const mutedText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary";
|
|
|
|
function nonEmpty(value: string): string | null {
|
|
const normalized = value.trim();
|
|
return normalized.length > 0 ? normalized : null;
|
|
}
|
|
|
|
export function AcceptanceNewPage(props: AcceptanceNewPageProps) {
|
|
const { t } = useTranslation();
|
|
const [form, setForm] = useState({ maengelChecklistId: "", customerEmail: "", bemerkungen: "", clientMutationId: "" });
|
|
const [defects, setDefects] = useState<DefectDraft[]>([{ classification: "minor", title: "", photoFileId: "" }]);
|
|
const [signatures, setSignatures] = useState<SignatureDraft[]>([
|
|
{ signerRole: "kunde", signerName: "", signatureFileId: "" },
|
|
{ signerRole: "monteur", signerName: "", signatureFileId: "" },
|
|
]);
|
|
|
|
const serializedDefects = useMemo(
|
|
() => JSON.stringify(defects.map((defect) => ({ classification: defect.classification, title: nonEmpty(defect.title), photoFileId: nonEmpty(defect.photoFileId) })).filter((defect) => defect.title || defect.photoFileId)),
|
|
[defects],
|
|
);
|
|
const serializedSignatures = useMemo(
|
|
() => JSON.stringify(signatures.map((signature) => ({ signerRole: signature.signerRole, signerName: signature.signerName, signatureFileId: nonEmpty(signature.signatureFileId) })).filter((signature) => signature.signerName.trim().length > 0)),
|
|
[signatures],
|
|
);
|
|
|
|
if (props.vm.status !== "ready") return null;
|
|
const data = props.vm.data;
|
|
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-6" data-ui-id="ui.projects.acceptance.new">
|
|
<header className="flex flex-col gap-1">
|
|
<h1 className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("projects.acceptance.newTitle")}</h1>
|
|
<span className={mutedText}>{data.projectNumber} · {data.projectName}</span>
|
|
</header>
|
|
|
|
{props.errorKey ? <BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback> : null}
|
|
|
|
<div className="grid gap-6 xl:grid-cols-[minmax(0,1fr)_360px]">
|
|
<Card data-ui-id="ui.projects.acceptance.new.form">
|
|
<span className={headingText}>{t("projects.acceptance.protocol")}</span>
|
|
<div className="grid gap-4 md:grid-cols-2">
|
|
<TextInput showLabel label={t("projects.fields.defectChecklistId")} className="max-w-none">
|
|
<TextInput.Input value={form.maengelChecklistId} onChange={(event) => setForm((current) => ({ ...current, maengelChecklistId: event.target.value }))} />
|
|
</TextInput>
|
|
<TextInput showLabel label={t("projects.fields.customerEmail")} className="max-w-none">
|
|
<TextInput.Input type="email" value={form.customerEmail} onChange={(event) => setForm((current) => ({ ...current, customerEmail: event.target.value }))} />
|
|
</TextInput>
|
|
<TextInput showLabel label={t("projects.fields.clientMutationId")} className="max-w-none">
|
|
<TextInput.Input value={form.clientMutationId} onChange={(event) => setForm((current) => ({ ...current, clientMutationId: event.target.value }))} />
|
|
</TextInput>
|
|
<TextArea showLabel label={t("projects.fields.notes")} className="max-w-none md:col-span-2">
|
|
<TextArea.Input value={form.bemerkungen} onChange={(event) => setForm((current) => ({ ...current, bemerkungen: event.target.value }))} />
|
|
</TextArea>
|
|
</div>
|
|
</Card>
|
|
|
|
<Card data-ui-id="ui.projects.acceptance.new.summary">
|
|
<span className={headingText}>{t("projects.acceptance.summary")}</span>
|
|
<span className={mutedText}>{t("projects.acceptance.defectCount", { count: JSON.parse(serializedDefects).length })}</span>
|
|
<span className={mutedText}>{t("projects.acceptance.signatureCount", { count: JSON.parse(serializedSignatures).length })}</span>
|
|
<Button
|
|
loading={props.submitting}
|
|
onClick={() =>
|
|
props.actions.onSubmit("submitAcceptanceProtocol", {
|
|
...form,
|
|
defects: serializedDefects,
|
|
signatures: serializedSignatures,
|
|
})
|
|
}
|
|
>
|
|
{t("projects.actions.submitAcceptance")}
|
|
</Button>
|
|
</Card>
|
|
</div>
|
|
|
|
<Card data-ui-id="ui.projects.acceptance.new.defects">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<span className={headingText}>{t("projects.acceptance.defects")}</span>
|
|
<Button variant="tertiary" size="small" onClick={() => setDefects((current) => [...current, { classification: "minor", title: "", photoFileId: "" }])}>{t("projects.actions.addDefect")}</Button>
|
|
</div>
|
|
<div className="grid gap-4">
|
|
{defects.map((defect, index) => (
|
|
<div key={index} className="grid gap-3 rounded-md border border-solid border-border-default p-4 md:grid-cols-[180px_minmax(0,1fr)_minmax(0,1fr)]">
|
|
<Select showLabel label={t("projects.fields.classification")} value={defect.classification} onValueChange={(classification) => setDefects((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, classification: classification as DefectDraft["classification"] } : item))} className="max-w-none">
|
|
<Select.Item value="minor">{t("projects.defectClass.minor")}</Select.Item>
|
|
<Select.Item value="critical">{t("projects.defectClass.critical")}</Select.Item>
|
|
<Select.Item value="cosmetic">{t("projects.defectClass.cosmetic")}</Select.Item>
|
|
</Select>
|
|
<TextInput showLabel label={t("projects.fields.checklistItem")} className="max-w-none">
|
|
<TextInput.Input value={defect.title} onChange={(event) => setDefects((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, title: event.target.value } : item))} />
|
|
</TextInput>
|
|
<TextInput showLabel label={t("projects.fields.fileId")} className="max-w-none">
|
|
<TextInput.Input value={defect.photoFileId} onChange={(event) => setDefects((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, photoFileId: event.target.value } : item))} />
|
|
</TextInput>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
|
|
<Card data-ui-id="ui.projects.acceptance.new.signatures">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<span className={headingText}>{t("signature.fields.signers")}</span>
|
|
<Button variant="tertiary" size="small" onClick={() => setSignatures((current) => [...current, { signerRole: "kunde", signerName: "", signatureFileId: "" }])}>{t("projects.actions.addSigner")}</Button>
|
|
</div>
|
|
<div className="grid gap-4">
|
|
{signatures.map((signature, index) => (
|
|
<div key={index} className="grid gap-3 rounded-md border border-solid border-border-default p-4 md:grid-cols-[180px_minmax(0,1fr)_minmax(0,1fr)]">
|
|
<Select showLabel label={t("projects.fields.role")} value={signature.signerRole} onValueChange={(signerRole) => setSignatures((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, signerRole: signerRole as SignatureDraft["signerRole"] } : item))} className="max-w-none">
|
|
<Select.Item value="kunde">{t("projects.signerRole.kunde")}</Select.Item>
|
|
<Select.Item value="monteur">{t("projects.signerRole.monteur")}</Select.Item>
|
|
</Select>
|
|
<TextInput showLabel label={t("projects.fields.name")} className="max-w-none">
|
|
<TextInput.Input value={signature.signerName} onChange={(event) => setSignatures((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, signerName: event.target.value } : item))} />
|
|
</TextInput>
|
|
<TextInput showLabel label={t("projects.fields.fileId")} className="max-w-none">
|
|
<TextInput.Input value={signature.signatureFileId} onChange={(event) => setSignatures((current) => current.map((item, itemIndex) => itemIndex === index ? { ...item, signatureFileId: event.target.value } : item))} />
|
|
</TextInput>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
</main>
|
|
);
|
|
}
|