279 lines
13 KiB
TypeScript
279 lines
13 KiB
TypeScript
// AK-P17a Interim-UI (AD-28) — "my documents" screen composed from ~/ui (D-HR
|
|
// "Eigene Dokumente", archetype A2). Renders the server-built HrDocumentListVM;
|
|
// office-uploaded docs are read-only (no archive action), the employee can always
|
|
// open/download. Kind filter is client-side. Self-upload is wired: the dialog
|
|
// collects metadata + a file and hands it to `actions.onUpload`; the route drives
|
|
// the chunked upload (reusing the Files pipeline) and links the resulting File as
|
|
// an HrEmployeeDocument. Upload progress is injected as `upload`.
|
|
// Final Subframe optics = tracked design-nachzug.
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { AppLink } from "~/components/app/AppLink";
|
|
import { FeatherFileText, FeatherUpload } from "@subframe/core";
|
|
import type { HrDocumentKind, HrDocumentListVM } from "@hivegrid/view-models";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell, PageToolbar } from "../shared/PageShell";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Dialog } from "~/ui/components/Dialog";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { Table } from "~/ui/components/Table";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { APP_TIME_ZONE } from "~/locale/format";
|
|
|
|
/** Injected upload state (owned by the route, which posts the file multipart). */
|
|
export type HrDocumentUploadState = {
|
|
status: "idle" | "uploading" | "done" | "error";
|
|
errorKey: string | null;
|
|
};
|
|
|
|
export type HrMyDocumentsUploadInput = {
|
|
file: File;
|
|
title: string;
|
|
documentKind: HrDocumentKind;
|
|
validUntilIso: string | null;
|
|
};
|
|
|
|
export type HrMyDocumentsActions = {
|
|
/** POSTs intent=archive for an own (non-office) document. */
|
|
onArchive: (documentId: string) => void;
|
|
/** Hand a picked file + metadata to the route (multipart upload → createDocument). */
|
|
onUpload: (input: HrMyDocumentsUploadInput) => void;
|
|
/** Reset the injected upload state (called when a fresh dialog session opens). */
|
|
onResetUpload: () => void;
|
|
};
|
|
|
|
export type HrMyDocumentsPageProps = {
|
|
vm: HrDocumentListVM;
|
|
submitting: boolean;
|
|
upload: HrDocumentUploadState;
|
|
actions: HrMyDocumentsActions;
|
|
};
|
|
|
|
const FILTER_ALL = "__all__";
|
|
const KINDS: readonly HrDocumentKind[] = ["contract", "certificate", "identification", "qualification", "other"];
|
|
|
|
export function HrMyDocumentsPage({ vm, submitting, upload, actions }: HrMyDocumentsPageProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const [kind, setKind] = useState<HrDocumentKind | null>(null);
|
|
const [uploadOpen, setUploadOpen] = useState(false);
|
|
const [form, setForm] = useState<{ title: string; documentKind: HrDocumentKind; validUntil: string }>({ title: "", documentKind: "other", validUntil: "" });
|
|
const [file, setFile] = useState<File | null>(null);
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
// Close + reset the dialog once the route reports the upload finished.
|
|
useEffect(() => {
|
|
if (upload.status === "done" && uploadOpen) {
|
|
setUploadOpen(false);
|
|
setForm({ title: "", documentKind: "other", validUntil: "" });
|
|
setFile(null);
|
|
if (fileInputRef.current) fileInputRef.current.value = "";
|
|
}
|
|
}, [upload.status, uploadOpen]);
|
|
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return (
|
|
<PageShell uiId="ui.hr.documents">
|
|
<PageHeader
|
|
icon={<FeatherFileText className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("hr.documents.title")}
|
|
subtitle={t("hr.documents.subtitle")}
|
|
/>
|
|
<PageBody>{null}</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
const { rows, canUpload, emptyHeadlineKey, emptyBodyKey } = vm.data;
|
|
const dateFormatter = new Intl.DateTimeFormat(i18n.language, { timeZone: APP_TIME_ZONE, dateStyle: "medium" });
|
|
const visible = rows.filter((row) => (kind ? row.documentKind === kind : true));
|
|
const busy = upload.status === "uploading";
|
|
|
|
function submitUpload() {
|
|
if (!file || form.title.trim().length === 0) return;
|
|
actions.onUpload({ file, title: form.title.trim(), documentKind: form.documentKind, validUntilIso: form.validUntil || null });
|
|
}
|
|
|
|
return (
|
|
<PageShell uiId="ui.hr.documents">
|
|
<PageHeader
|
|
icon={<FeatherFileText className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("hr.documents.title")}
|
|
subtitle={t("hr.documents.subtitle")}
|
|
actions={
|
|
canUpload ? (
|
|
<Button
|
|
icon={<FeatherUpload />}
|
|
onClick={() => {
|
|
actions.onResetUpload();
|
|
setUploadOpen(true);
|
|
}}
|
|
data-ui-id="ui.hr.documents.upload"
|
|
>
|
|
{t("hr.documents.upload.button")}
|
|
</Button>
|
|
) : undefined
|
|
}
|
|
/>
|
|
{/* --- Filter bar: full-bleed page chrome, same strip as the other list
|
|
screens. Still only shown when there is something to filter. --- */}
|
|
{rows.length > 0 ? (
|
|
<PageToolbar uiId="ui.hr.documents.filter">
|
|
<Select
|
|
className="max-w-[240px]"
|
|
label={t("hr.documents.filter.kind")}
|
|
showLabel
|
|
value={kind ?? FILTER_ALL}
|
|
onValueChange={(value: string) => setKind(value === FILTER_ALL ? null : (value as HrDocumentKind))}
|
|
>
|
|
<Select.Item value={FILTER_ALL}>{t("hr.documents.filter.all")}</Select.Item>
|
|
{KINDS.map((entry) => (
|
|
<Select.Item key={entry} value={entry}>
|
|
{t(`hr.document.kind.${entry}`)}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
</PageToolbar>
|
|
) : null}
|
|
<PageBody>
|
|
{rows.length === 0 ? (
|
|
<div data-ui-id="ui.hr.documents.empty">
|
|
<EmptyState title={t(emptyHeadlineKey)} body={t(emptyBodyKey)} />
|
|
</div>
|
|
) : visible.length === 0 ? (
|
|
<EmptyState variant="no-results" title={t("hr.documents.noMatches")} body={t(emptyBodyKey)} />
|
|
) : (
|
|
<div className="w-full overflow-x-auto">
|
|
<Table
|
|
data-ui-id="ui.hr.documents.table"
|
|
header={
|
|
<Table.HeaderRow>
|
|
<Table.HeaderCell>{t("hr.documents.column.title")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("hr.documents.column.kind")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("hr.documents.column.date")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("hr.documents.column.validity")}</Table.HeaderCell>
|
|
<Table.HeaderCell> </Table.HeaderCell>
|
|
</Table.HeaderRow>
|
|
}
|
|
>
|
|
{visible.map((row) => (
|
|
<Table.Row key={row.id} data-row-id={row.id} data-readonly={row.readOnly}>
|
|
<Table.Cell>
|
|
<div className="flex items-center gap-2">
|
|
<AppLink href={row.fileHref} className="text-body-default-medium-0 font-body-default-medium-0 text-text-feedback-link-default">
|
|
{row.title}
|
|
</AppLink>
|
|
{row.readOnly ? (
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{t("hr.documents.status.readOnly")}
|
|
</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.kindKey)}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
|
|
{dateFormatter.format(new Date(row.createdAtIso))}
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
{row.validUntilIso ? (
|
|
row.isExpired ? (
|
|
<BadgeFeedback farbe="subtle-negative" size="small">
|
|
{t("hr.documents.status.expired")}
|
|
</BadgeFeedback>
|
|
) : (
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
|
|
{dateFormatter.format(new Date(row.validUntilIso))}
|
|
</span>
|
|
)
|
|
) : (
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-tertiary">—</span>
|
|
)}
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
{!row.readOnly ? (
|
|
<div className="flex justify-end">
|
|
<Button variant="tertiary" size="small" loading={submitting} onClick={() => actions.onArchive(row.id)}>
|
|
{t("hr.documents.action.archive")}
|
|
</Button>
|
|
</div>
|
|
) : null}
|
|
</Table.Cell>
|
|
</Table.Row>
|
|
))}
|
|
</Table>
|
|
</div>
|
|
)}
|
|
|
|
{/* Upload dialog (self-service). The route drives the chunked upload + link. */}
|
|
<Dialog open={uploadOpen} onOpenChange={(next) => { if (!busy) setUploadOpen(next); }}>
|
|
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.documents.uploadDialog">
|
|
<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.documents.upload.dialog.title")}</span>
|
|
<div className="flex w-full flex-col gap-4">
|
|
<TextInput label={t("hr.documents.upload.field.title")} showLabel fillContainer>
|
|
<TextInput.Input value={form.title} onChange={(e) => setForm((s) => ({ ...s, title: e.target.value }))} data-ui-id="ui.hr.documents.upload.title" />
|
|
</TextInput>
|
|
<Select
|
|
label={t("hr.documents.upload.field.kind")}
|
|
showLabel
|
|
value={form.documentKind}
|
|
onValueChange={(value: string) => setForm((s) => ({ ...s, documentKind: value as HrDocumentKind }))}
|
|
>
|
|
{KINDS.map((entry) => (
|
|
<Select.Item key={entry} value={entry}>
|
|
{t(`hr.document.kind.${entry}`)}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
<label className="flex flex-col gap-1">
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("hr.documents.upload.field.validUntil")}</span>
|
|
<input
|
|
type="date"
|
|
value={form.validUntil}
|
|
onChange={(e) => setForm((s) => ({ ...s, validUntil: e.target.value }))}
|
|
className="h-9 rounded-[4px] border border-solid border-border-default bg-transparent px-3 text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary outline-none"
|
|
/>
|
|
</label>
|
|
<label className="flex flex-col gap-1">
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("hr.documents.upload.field.file")}</span>
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
onChange={(e) => setFile(e.target.files?.[0] ?? null)}
|
|
className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary file:mr-3 file:rounded-[4px] file:border file:border-solid file:border-border-default file:bg-transparent file:px-3 file:py-1.5 file:text-text-neutral-primary"
|
|
data-ui-id="ui.hr.documents.upload.file"
|
|
/>
|
|
</label>
|
|
|
|
{upload.status !== "idle" ? (
|
|
<div className="flex flex-col gap-2" data-ui-id="ui.hr.documents.upload.progress" data-status={upload.status}>
|
|
{upload.status === "error" ? (
|
|
<Alert variant="error" textSize="small" description={t(upload.errorKey ?? "hr.documents.upload.error.generic")} notDismissible />
|
|
) : upload.status === "uploading" ? (
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">{t("hr.documents.upload.status.uploading")}</span>
|
|
) : null}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="neutral" disabled={busy} onClick={() => setUploadOpen(false)}>
|
|
{t("hr.action.cancel")}
|
|
</Button>
|
|
<Button loading={busy} disabled={!file || form.title.trim().length === 0} onClick={submitUpload} data-ui-id="ui.hr.documents.upload.submit">
|
|
{t("hr.documents.upload.submit")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|