282 lines
11 KiB
TypeScript
282 lines
11 KiB
TypeScript
// AK-P17b Interim-UI (AD-28) — sick-leave screen composed from ~/ui (D-HR "Krank",
|
||
// archetype A2 + A4). Pure presentational: URL-driven tabs (own / office). The AU
|
||
// certificate is sensitive — never rendered; a reveal action decrypts + opens it
|
||
// on demand (route owns the fetch/open). Report / attach / close run through
|
||
// injected actions. Final Subframe optics = tracked design-nachzug.
|
||
|
||
import { useState } from "react";
|
||
import { useTranslation } from "react-i18next";
|
||
import { FeatherBell, FeatherPlus } from "@subframe/core";
|
||
import type { HrSickLeaveOfficeVM, HrSickLeaveOwnVM, HrSickLeaveRow } from "@hivegrid/view-models";
|
||
import type { FormDefinition } from "@hivegrid/form-widget";
|
||
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 { Dialog } from "~/ui/components/Dialog";
|
||
import { EmptyState } from "~/ui/components/EmptyState";
|
||
import { Tabs } from "~/ui/components/Tabs";
|
||
import { DateField } from "~/components/screens/tasks/DateField";
|
||
import { AppLink } from "~/components/app/AppLink";
|
||
import {
|
||
InternalFormRenderer,
|
||
type InternalFormValues,
|
||
} from "~/modules/forms/internal/InternalFormRenderer";
|
||
|
||
export type HrSickTab = "own" | "office";
|
||
|
||
export type HrSickActions = {
|
||
onReport: (values: InternalFormValues) => void;
|
||
onReportFor: (values: InternalFormValues) => void;
|
||
onAttach: (reportId: string, certificate: File) => void;
|
||
onRevealCertificate: (reportId: string) => void;
|
||
onClose: (reportId: string, returnDate: string) => void;
|
||
};
|
||
|
||
export type HrSickPageProps = {
|
||
tab: HrSickTab;
|
||
canManage: boolean;
|
||
ownVm: HrSickLeaveOwnVM;
|
||
officeVm: HrSickLeaveOfficeVM;
|
||
ownFormDefinition: FormDefinition;
|
||
officeFormDefinition: FormDefinition;
|
||
tabHref: (tab: HrSickTab) => string;
|
||
submitting: boolean;
|
||
actions: HrSickActions;
|
||
};
|
||
|
||
type DialogState =
|
||
| { type: "report" }
|
||
| { type: "reportFor" }
|
||
| { type: "attach"; reportId: string }
|
||
| { type: "close"; reportId: string }
|
||
| null;
|
||
|
||
function readDate(el: HTMLElement | null, name: string): string {
|
||
return el?.querySelector<HTMLInputElement>(`input[name='${name}']`)?.value ?? "";
|
||
}
|
||
|
||
export function HrSickPage({ tab, canManage, ownVm, officeVm, ownFormDefinition, officeFormDefinition, tabHref, submitting, actions }: HrSickPageProps) {
|
||
const { t } = useTranslation();
|
||
const [dialog, setDialog] = useState<DialogState>(null);
|
||
const [certificate, setCertificate] = useState<File | null>(null);
|
||
|
||
const tabs: HrSickTab[] = canManage ? ["own", "office"] : ["own"];
|
||
const ownRows = ownVm.data?.rows ?? [];
|
||
const officeRows = officeVm.data?.rows ?? [];
|
||
|
||
return (
|
||
<PageShell uiId="ui.hr.sick">
|
||
<PageHeader
|
||
icon={<FeatherBell className={PAGE_HEADER_ICON_CLASS} />}
|
||
title={t("hr.sick.title")}
|
||
subtitle={t("hr.sick.subtitle")}
|
||
actions={
|
||
tab === "own" ? (
|
||
<Button icon={<FeatherPlus />} onClick={() => setDialog({ type: "report" })} data-ui-id="ui.hr.sick.report">
|
||
{t("hr.sick.action.report")}
|
||
</Button>
|
||
) : (
|
||
<Button icon={<FeatherPlus />} onClick={() => setDialog({ type: "reportFor" })} data-ui-id="ui.hr.sick.reportFor">
|
||
{t("hr.sick.action.reportFor")}
|
||
</Button>
|
||
)
|
||
}
|
||
/>
|
||
<PageBody>
|
||
<div data-ui-id="ui.hr.sick.tabs">
|
||
<Tabs>
|
||
{tabs.map((entry) => (
|
||
<AppLink key={entry} href={tabHref(entry)} className="contents no-underline">
|
||
<Tabs.Item active={tab === entry}>{t(`hr.sick.tab.${entry}`)}</Tabs.Item>
|
||
</AppLink>
|
||
))}
|
||
</Tabs>
|
||
</div>
|
||
|
||
{tab === "own" ? (
|
||
ownRows.length === 0 ? (
|
||
<div data-ui-id="ui.hr.sick.own.empty">
|
||
<EmptyState title={t("hr.sick.own.empty.headline")} body={t("hr.sick.own.empty.body")} />
|
||
</div>
|
||
) : (
|
||
<div className="flex w-full flex-col gap-3" data-ui-id="ui.hr.sick.own.list">
|
||
{ownRows.map((row) => (
|
||
<SickCard
|
||
key={row.id}
|
||
row={row}
|
||
submitting={submitting}
|
||
onReveal={() => actions.onRevealCertificate(row.id)}
|
||
onAttach={() => setDialog({ type: "attach", reportId: row.id })}
|
||
onClose={() => setDialog({ type: "close", reportId: row.id })}
|
||
/>
|
||
))}
|
||
</div>
|
||
)
|
||
) : null}
|
||
|
||
{tab === "office" ? (
|
||
officeRows.length === 0 ? (
|
||
<div data-ui-id="ui.hr.sick.office.empty">
|
||
<EmptyState title={t("hr.sick.office.empty.headline")} body={t("hr.sick.office.empty.body")} />
|
||
</div>
|
||
) : (
|
||
<div className="flex w-full flex-col gap-2" data-ui-id="ui.hr.sick.office.list">
|
||
{officeRows.map((row) => (
|
||
<Card key={row.id} data-row-id={row.id}>
|
||
<div className="flex w-full items-center justify-between gap-4">
|
||
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{row.employeeLabel}</span>
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{row.startDateIso}</span>
|
||
<BadgeFeedback farbe="subtle-neutral" size="small">
|
||
{t(row.statusLabelKey)}
|
||
</BadgeFeedback>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
))}
|
||
</div>
|
||
)
|
||
) : null}
|
||
|
||
{/* Report dialog (own or office) */}
|
||
<Dialog open={dialog?.type === "report" || dialog?.type === "reportFor"} onOpenChange={(open) => !open && setDialog(null)}>
|
||
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.sick.reportDialog">
|
||
<div className="flex w-full flex-col gap-6 p-2">
|
||
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">
|
||
{dialog?.type === "reportFor" ? t("hr.sick.action.reportFor") : t("hr.sick.action.report")}
|
||
</span>
|
||
<InternalFormRenderer
|
||
definition={
|
||
dialog?.type === "reportFor"
|
||
? officeFormDefinition
|
||
: ownFormDefinition
|
||
}
|
||
submitting={submitting}
|
||
uiId="ui.hr.sick.m05"
|
||
onCancel={() => setDialog(null)}
|
||
onSubmit={(values) => {
|
||
if (dialog?.type === "reportFor") actions.onReportFor(values);
|
||
else actions.onReport(values);
|
||
setDialog(null);
|
||
}}
|
||
/>
|
||
</div>
|
||
</Dialog.Content>
|
||
</Dialog>
|
||
|
||
{/* Attach certificate dialog */}
|
||
<Dialog open={dialog?.type === "attach"} onOpenChange={(open) => !open && setDialog(null)}>
|
||
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.sick.attachDialog">
|
||
<div className="flex w-full flex-col gap-6 p-2">
|
||
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("hr.sick.action.attachCertificate")}</span>
|
||
<label className="flex w-full flex-col gap-2 text-body-small-medium-0 text-text-neutral-secondary">
|
||
{t("hr.sick.field.certificateFileId")}
|
||
<input
|
||
type="file"
|
||
accept=".pdf,image/*"
|
||
onChange={(event) => setCertificate(event.target.files?.[0] ?? null)}
|
||
data-ui-id="ui.hr.sick.attachDialog.file"
|
||
/>
|
||
</label>
|
||
<div className="flex justify-end gap-2">
|
||
<Button variant="neutral" onClick={() => setDialog(null)}>
|
||
{t("hr.action.cancel")}
|
||
</Button>
|
||
<Button
|
||
loading={submitting}
|
||
disabled={!certificate}
|
||
onClick={() => {
|
||
if (dialog?.type === "attach" && certificate) actions.onAttach(dialog.reportId, certificate);
|
||
setCertificate(null);
|
||
setDialog(null);
|
||
}}
|
||
>
|
||
{t("hr.action.confirm")}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
</Dialog.Content>
|
||
</Dialog>
|
||
|
||
{/* Close dialog */}
|
||
<Dialog open={dialog?.type === "close"} onOpenChange={(open) => !open && setDialog(null)}>
|
||
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.sick.closeDialog">
|
||
<div className="flex w-full flex-col gap-6 p-2">
|
||
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("hr.sick.action.close")}</span>
|
||
<DateField label={t("hr.sick.field.returnDate")} name="returnDate" />
|
||
<div className="flex justify-end gap-2">
|
||
<Button variant="neutral" onClick={() => setDialog(null)}>
|
||
{t("hr.action.cancel")}
|
||
</Button>
|
||
<Button
|
||
loading={submitting}
|
||
onClick={(event) => {
|
||
const el = (event.currentTarget as HTMLElement).closest("[data-ui-id='ui.hr.sick.closeDialog']");
|
||
const ret = readDate(el as HTMLElement, "returnDate");
|
||
if (!ret || dialog?.type !== "close") return;
|
||
actions.onClose(dialog.reportId, ret);
|
||
setDialog(null);
|
||
}}
|
||
>
|
||
{t("hr.action.confirm")}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
</Dialog.Content>
|
||
</Dialog>
|
||
</PageBody>
|
||
</PageShell>
|
||
);
|
||
}
|
||
|
||
function SickCard({
|
||
row,
|
||
submitting,
|
||
onReveal,
|
||
onAttach,
|
||
onClose,
|
||
}: {
|
||
row: HrSickLeaveRow;
|
||
submitting: boolean;
|
||
onReveal: () => void;
|
||
onAttach: () => void;
|
||
onClose: () => void;
|
||
}) {
|
||
const { t } = useTranslation();
|
||
return (
|
||
<Card data-row-id={row.id}>
|
||
<div className="flex w-full items-center justify-between gap-4">
|
||
<div className="flex flex-col gap-1">
|
||
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
|
||
{row.startDateIso}
|
||
{row.returnDateIso ? ` – ${row.returnDateIso}` : ""}
|
||
</span>
|
||
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
|
||
{row.hasCertificate ? t("hr.sick.certificate.present") : t("hr.sick.certificate.missing")}
|
||
</span>
|
||
</div>
|
||
<div className="flex items-center gap-2">
|
||
<BadgeFeedback farbe="subtle-neutral" size="small">
|
||
{t(row.statusLabelKey)}
|
||
</BadgeFeedback>
|
||
{row.hasCertificate ? (
|
||
<Button variant="tertiary" size="small" loading={submitting} onClick={onReveal} data-ui-id="ui.hr.sick.reveal">
|
||
{t("hr.sick.action.revealCertificate")}
|
||
</Button>
|
||
) : (
|
||
<Button variant="tertiary" size="small" onClick={onAttach}>
|
||
{t("hr.sick.action.attachCertificate")}
|
||
</Button>
|
||
)}
|
||
{row.canClose ? (
|
||
<Button variant="tertiary" size="small" onClick={onClose}>
|
||
{t("hr.sick.action.close")}
|
||
</Button>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
);
|
||
}
|