189 lines
8.4 KiB
TypeScript
189 lines
8.4 KiB
TypeScript
// AK-P17b Interim-UI (AD-28) — survey participation + item editor composed from
|
|
// ~/ui (D-HR "Umfrage Teilnahme", archetype A3/A4). Pure presentational: answers
|
|
// held in local state and submitted via the injected action (route maps them to
|
|
// item:<id> form fields); managers add/remove items. Final Subframe optics =
|
|
// tracked design-nachzug.
|
|
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherPlus } from "@subframe/core";
|
|
import type { HrSurveyItem, HrSurveyItemType, HrSurveyParticipationVM } from "@hivegrid/view-models";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Checkbox } from "~/ui/components/Checkbox";
|
|
import { Dialog } from "~/ui/components/Dialog";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
|
|
const ITEM_TYPES: readonly HrSurveyItemType[] = ["text", "rating_5", "nps", "choice", "boolean", "date"];
|
|
|
|
export type HrSurveyParticipationActions = {
|
|
onSubmit: (answers: Record<string, string>) => void;
|
|
onAddItem: (input: { itemType: string; prompt: string; options: string; required: boolean }) => void;
|
|
onRemoveItem: (itemId: string) => void;
|
|
};
|
|
|
|
export type HrSurveyParticipationPageProps = {
|
|
vm: HrSurveyParticipationVM;
|
|
canManage: boolean;
|
|
status: string;
|
|
submitting: boolean;
|
|
actions: HrSurveyParticipationActions;
|
|
};
|
|
|
|
// Controlled answer field per item type. Date uses a native input styled with the
|
|
// ~/ui design tokens (same sanctioned exception as the tasks DateField).
|
|
function AnswerField({ item, value, onChange }: { item: HrSurveyItem; value: string; onChange: (v: string) => void }) {
|
|
if (item.itemType === "boolean") {
|
|
return <Checkbox label="" checked={value === "true"} onCheckedChange={(c: boolean) => onChange(c ? "true" : "")} />;
|
|
}
|
|
if (item.itemType === "choice") {
|
|
return (
|
|
<Select value={value} onValueChange={onChange}>
|
|
{item.options.map((o) => (
|
|
<Select.Item key={o} value={o}>
|
|
{o}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
);
|
|
}
|
|
if (item.itemType === "date") {
|
|
return (
|
|
<input
|
|
type="date"
|
|
value={value}
|
|
onChange={(e) => onChange(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"
|
|
/>
|
|
);
|
|
}
|
|
const numeric = item.itemType === "rating_5" || item.itemType === "nps";
|
|
return (
|
|
<TextInput fillContainer>
|
|
<TextInput.Input type={numeric ? "number" : "text"} value={value} onChange={(e) => onChange(e.target.value)} />
|
|
</TextInput>
|
|
);
|
|
}
|
|
|
|
export function HrSurveyParticipationPage({ vm, canManage, status, submitting, actions }: HrSurveyParticipationPageProps) {
|
|
const { t } = useTranslation();
|
|
const data = vm.data;
|
|
const [answers, setAnswers] = useState<Record<string, string>>({});
|
|
const [addOpen, setAddOpen] = useState(false);
|
|
const [item, setItem] = useState({ itemType: "text", prompt: "", options: "", required: false });
|
|
|
|
if (vm.status !== "ready" || !data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.hr.survey.participate" />;
|
|
}
|
|
|
|
function addItem() {
|
|
if (!item.prompt.trim()) return;
|
|
actions.onAddItem(item);
|
|
setAddOpen(false);
|
|
setItem({ itemType: "text", prompt: "", options: "", required: false });
|
|
}
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.hr.survey.participate" data-row-id={data.surveyId}>
|
|
<div className="flex flex-col gap-0">
|
|
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{data.title}</span>
|
|
{data.description ? (
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{data.description}</span>
|
|
) : null}
|
|
</div>
|
|
|
|
{data.isAnonymous ? <Alert variant="information" title={t(data.anonymousHintKey)} /> : null}
|
|
|
|
{data.alreadySubmitted ? (
|
|
<div data-ui-id="ui.hr.survey.submitted">
|
|
<Alert variant="success" title={t(data.submittedHintKey)} />
|
|
</div>
|
|
) : status === "open" ? (
|
|
<Card>
|
|
<div className="flex w-full flex-col gap-4" data-ui-id="ui.hr.survey.form">
|
|
{data.items.map((surveyItem) => (
|
|
<div key={surveyItem.id} className="flex flex-col gap-1" data-row-id={surveyItem.id}>
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
|
|
{surveyItem.prompt}
|
|
{surveyItem.required ? " *" : ""}
|
|
</span>
|
|
<AnswerField
|
|
item={surveyItem}
|
|
value={answers[surveyItem.id] ?? ""}
|
|
onChange={(v) => setAnswers((s) => ({ ...s, [surveyItem.id]: v }))}
|
|
/>
|
|
</div>
|
|
))}
|
|
<div className="flex justify-end">
|
|
<Button loading={submitting} onClick={() => actions.onSubmit(answers)} data-ui-id="ui.hr.survey.submit">
|
|
{t("hr.surveys.action.submit")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Card>
|
|
) : (
|
|
<div data-ui-id="ui.hr.survey.closed">
|
|
<Alert variant="neutral" title={t("hr.surveys.notOpen")} />
|
|
</div>
|
|
)}
|
|
|
|
{canManage ? (
|
|
<div className="flex w-full flex-col gap-3" data-ui-id="ui.hr.survey.editor">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t("hr.surveys.editor.title")}</span>
|
|
<Button variant="secondary" size="small" icon={<FeatherPlus />} onClick={() => setAddOpen(true)}>
|
|
{t("hr.surveys.editor.add")}
|
|
</Button>
|
|
</div>
|
|
{data.items.map((surveyItem) => (
|
|
<Card key={`edit-${surveyItem.id}`}>
|
|
<div className="flex w-full items-center justify-between gap-4">
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
|
|
{t(surveyItem.typeLabelKey)} · {surveyItem.prompt}
|
|
</span>
|
|
<Button variant="tertiary" size="small" loading={submitting} onClick={() => actions.onRemoveItem(surveyItem.id)}>
|
|
{t("hr.surveys.editor.remove")}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
|
|
<Dialog open={addOpen} onOpenChange={setAddOpen}>
|
|
<Dialog.Content className="w-full max-w-[560px] p-4" data-ui-id="ui.hr.survey.addItem">
|
|
<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.surveys.editor.add")}</span>
|
|
<div className="flex w-full flex-col gap-4">
|
|
<Select label={t("hr.surveys.editor.itemType")} showLabel value={item.itemType} onValueChange={(v: string) => setItem((s) => ({ ...s, itemType: v }))}>
|
|
{ITEM_TYPES.map((type) => (
|
|
<Select.Item key={type} value={type}>
|
|
{t(`hr.surveys.itemType.${type}`)}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
<TextInput label={t("hr.surveys.editor.prompt")} showLabel fillContainer>
|
|
<TextInput.Input value={item.prompt} onChange={(e) => setItem((s) => ({ ...s, prompt: e.target.value }))} />
|
|
</TextInput>
|
|
<TextInput label={t("hr.surveys.editor.options")} showLabel fillContainer>
|
|
<TextInput.Input value={item.options} onChange={(e) => setItem((s) => ({ ...s, options: e.target.value }))} />
|
|
</TextInput>
|
|
<Checkbox label={t("hr.surveys.editor.required")} checked={item.required} onCheckedChange={(c: boolean) => setItem((s) => ({ ...s, required: c }))} />
|
|
</div>
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="neutral" onClick={() => setAddOpen(false)}>
|
|
{t("hr.action.cancel")}
|
|
</Button>
|
|
<Button loading={submitting} disabled={!item.prompt.trim()} onClick={addItem} data-ui-id="ui.hr.survey.addItem.submit">
|
|
{t("hr.surveys.editor.add")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|