Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/users/InviteMemberDialog.tsx
T

140 lines
5.3 KiB
TypeScript

// "Mitglied hinzufügen" as a popup on the Mitarbeiter tab, replacing the
// separate sub-page. The sub-page existed because the hub's members tab is an
// index route and can carry no children, so the tab's Dialog outlet never
// received the form — posting the create contract with a fetcher sidesteps the
// routing constraint entirely and keeps settings/team/members/new as the action.
//
// Presentational: the route owns the fetcher. Field contract unchanged
// (email / first-name / last-name / send-invitation-email + csrf).
//
// Role-at-invite from David's wireframe is still NOT built: the invitation
// backend stores no role — roles are assigned in the list (D01) after acceptance.
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherPlus } from "@subframe/core";
import type { PlanFeatureUsageDto } from "~/application/dtos/subscriptions/PlanFeatureUsageDto";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { Checkbox } from "~/ui/components/Checkbox";
import { TextInput } from "~/ui/components/TextInput";
import { FormDialog } from "../shared/FormDialog";
export type InviteMemberInput = {
email: string;
firstName: string;
lastName: string;
sendInvitationEmail: boolean;
};
export function InviteMemberDialog({
open,
onOpenChange,
featurePlanUsage,
submitting,
errorMessage,
onInvite,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
/** Seat limit of the plan; when exhausted the form is replaced by the upgrade hint. */
featurePlanUsage: PlanFeatureUsageDto | undefined;
submitting: boolean;
errorMessage: string | null;
onInvite: (input: InviteMemberInput) => void;
}): React.JSX.Element {
const { t } = useTranslation();
const [email, setEmail] = useState("");
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [sendEmail, setSendEmail] = useState(false);
// Fresh fields on every open; the dialog stays mounted between invites.
useEffect(() => {
if (!open) return;
setEmail("");
setFirstName("");
setLastName("");
setSendEmail(false);
}, [open]);
const seatsExhausted = Boolean(featurePlanUsage && !featurePlanUsage.enabled);
const canSubmit = !seatsExhausted && email.trim().length > 0 && firstName.trim().length > 0;
return (
<FormDialog
open={open}
onOpenChange={onOpenChange}
uiId="ui.settings.members.new.dialog"
title={t("settings.members.form.newTitle")}
subtitle={t("settings.members.form.newDescription")}
footer={
<>
<Button className="mobile:w-full" type="button" variant="tertiary" onClick={() => onOpenChange(false)} data-ui-id="ui.settings.members.new.cancel">
{t("shared.cancel")}
</Button>
<Button
className="mobile:w-full"
type="button"
icon={<FeatherPlus />}
loading={submitting}
disabled={!canSubmit}
onClick={() =>
onInvite({
email: email.trim(),
firstName: firstName.trim(),
lastName: lastName.trim(),
sendInvitationEmail: sendEmail,
})
}
data-ui-id="ui.settings.members.new.submit"
>
{t("settings.members.actions.new")}
</Button>
</>
}
>
{seatsExhausted ? (
<Alert
variant="warning"
notDismissible
// Same label the sub-page's CheckPlanFeatureLimit banner showed.
title={(featurePlanUsage?.entity ? t(featurePlanUsage.entity.titlePlural) : (featurePlanUsage?.name ?? "")).toUpperCase()}
description={featurePlanUsage?.message ? t(featurePlanUsage.message) : ""}
/>
) : null}
{errorMessage ? <Alert variant="error" notDismissible title={errorMessage} /> : null}
<TextInput className="w-full max-w-full" label={t("models.user.email")} showLabel>
<TextInput.Input
type="email"
value={email}
onChange={(event) => setEmail(event.target.value)}
required
autoFocus
autoComplete="off"
disabled={seatsExhausted}
/>
</TextInput>
<div className="mobile:flex-col flex w-full gap-4">
<TextInput className="w-full max-w-full" label={t("models.user.firstName")} showLabel>
<TextInput.Input value={firstName} onChange={(event) => setFirstName(event.target.value)} required autoComplete="off" disabled={seatsExhausted} />
</TextInput>
<TextInput className="w-full max-w-full" label={t("models.user.lastName")} showLabel>
<TextInput.Input value={lastName} onChange={(event) => setLastName(event.target.value)} autoComplete="off" disabled={seatsExhausted} />
</TextInput>
</div>
{/* w-fit, because the kit checkbox is justify-between and would otherwise
stretch across the dialog and strand its label at the far edge. */}
<Checkbox
className="w-fit"
label={t("settings.members.sendInvitationEmail.title")}
description={t("settings.members.sendInvitationEmail.description")}
checked={sendEmail}
onCheckedChange={setSendEmail}
disabled={seatsExhausted}
/>
</FormDialog>
);
}