Files

145 lines
5.2 KiB
TypeScript

// "Gruppe erstellen" as a popup on the Gruppen tab, replacing the slide-over
// sub-page that still wore the old boilerplate form controls (FormGroup,
// InputText, InputSelector). Same chrome as every other form popup (FormDialog)
// and the same ~/ui controls the rest of the settings screens use.
//
// Presentational: the route owns the fetcher and posts the unchanged create
// contract of settings/team/groups/new (action=create, name, description,
// color, users[]).
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherPlus } from "@subframe/core";
import ColorBadge from "~/components/ui/badges/ColorBadge";
import { Colors } from "~/application/enums/shared/Colors";
import { getColors } from "~/utils/shared/ColorUtils";
import { Button } from "~/ui/components/Button";
import { Checkbox } from "~/ui/components/Checkbox";
import { Select } from "~/ui/components/Select";
import { TextArea } from "~/ui/components/TextArea";
import { TextInput } from "~/ui/components/TextInput";
import { FormDialog } from "../shared/FormDialog";
export type GroupCandidateUser = {
userId: string;
name: string;
email: string;
};
export type GroupNewInput = {
name: string;
description: string;
color: number;
userIds: string[];
};
/** Colour is a required column on the group; the slide-over let it post NaN. */
const DEFAULT_COLOR = Colors.BLUE;
export function GroupNewDialog({
open,
onOpenChange,
candidates,
submitting,
onCreate,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
candidates: readonly GroupCandidateUser[];
submitting: boolean;
onCreate: (input: GroupNewInput) => void;
}): React.JSX.Element {
const { t } = useTranslation();
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [color, setColor] = useState<number>(DEFAULT_COLOR);
const [userIds, setUserIds] = useState<ReadonlySet<string>>(new Set());
function toggleUser(userId: string, checked: boolean) {
setUserIds((prev) => {
const next = new Set(prev);
if (checked) next.add(userId);
else next.delete(userId);
return next;
});
}
return (
<FormDialog
open={open}
onOpenChange={onOpenChange}
uiId="ui.settings.groups.newDialog"
title={t("settings.members.groups.createTitle")}
subtitle={t("settings.members.groups.subtitle")}
footer={
<>
<Button className="mobile:w-full" type="button" variant="tertiary" onClick={() => onOpenChange(false)}>
{t("shared.cancel")}
</Button>
<Button
className="mobile:w-full"
type="button"
icon={<FeatherPlus />}
loading={submitting}
disabled={name.trim().length === 0}
onClick={() => onCreate({ name: name.trim(), description: description.trim(), color, userIds: [...userIds] })}
data-ui-id="ui.settings.groups.newDialog.submit"
>
{t("shared.create")}
</Button>
</>
}
>
<TextInput className="w-full max-w-full" label={t("models.group.name")} showLabel>
<TextInput.Input value={name} onChange={(event) => setName(event.target.value)} required autoFocus autoComplete="off" />
</TextInput>
<TextArea className="w-full max-w-full" label={t("models.group.description")} showLabel>
<TextArea.Input value={description} onChange={(event) => setDescription(event.target.value)} />
</TextArea>
<Select
className="w-full max-w-full"
label={t("models.group.color")}
showLabel
value={String(color)}
onValueChange={(value: string) => setColor(Number(value))}
>
{getColors().map((option) => (
<Select.Item key={option} value={String(option)}>
<span className="flex items-center gap-2">
<ColorBadge color={option} />
{t(`app.shared.colors.${Colors[option]}`)}
</span>
</Select.Item>
))}
</Select>
<div className="flex w-full flex-col gap-2" role="group" aria-labelledby="ui-settings-groups-new-members">
<span
id="ui-settings-groups-new-members"
className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary"
>
{t("models.user.plural")}
</span>
{candidates.length === 0 ? (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("settings.members.groups.noCandidates")}
</span>
) : (
candidates.map((candidate) => (
<Checkbox
key={candidate.userId}
// The kit checkbox is justify-between and would otherwise strand
// each label at the far edge of the dialog.
className="w-fit"
label={candidate.name || candidate.email}
description={candidate.name ? candidate.email : undefined}
checked={userIds.has(candidate.userId)}
onCheckedChange={(checked: boolean) => toggleUser(candidate.userId, checked)}
/>
))
)}
</div>
</FormDialog>
);
}