97 lines
2.3 KiB
TypeScript
97 lines
2.3 KiB
TypeScript
import {
|
|
createCorrelationId,
|
|
fail,
|
|
type HousekeepingCapabilityContext,
|
|
type HousekeepingResult,
|
|
} from "../../../foundation/contracts";
|
|
import type { HousekeepingPageInput } from "../../../route-handlers";
|
|
import { type PeopleUsersQueryData, peopleUsersQuery } from "../queries/users";
|
|
import { PeoplePageFrame } from "./page-state";
|
|
import {
|
|
type PeopleCommandField,
|
|
PeopleCommandForm,
|
|
} from "./people-command-form";
|
|
|
|
interface PeopleUserEditPageProps {
|
|
readonly context: HousekeepingCapabilityContext;
|
|
readonly result?: HousekeepingResult<PeopleUsersQueryData>;
|
|
}
|
|
|
|
export function PeopleUserEditPage({ result }: PeopleUserEditPageProps) {
|
|
return (
|
|
<PeoplePageFrame
|
|
title="Edit user"
|
|
description="Update bounded account fields through an audited command."
|
|
result={result}
|
|
isEmpty={() => false}
|
|
>
|
|
{(data) => {
|
|
if (data.kind !== "user") return null;
|
|
const fields: PeopleCommandField[] = [
|
|
{
|
|
name: "username",
|
|
label: "Username",
|
|
type: "text",
|
|
defaultValue: data.user.username,
|
|
maxLength: 20,
|
|
required: true,
|
|
},
|
|
...(data.user.mail !== null
|
|
? [
|
|
{
|
|
name: "mail",
|
|
label: "Email",
|
|
type: "text",
|
|
defaultValue: data.user.mail,
|
|
maxLength: 255,
|
|
} as const,
|
|
]
|
|
: []),
|
|
{
|
|
name: "motto",
|
|
label: "Motto",
|
|
type: "text",
|
|
defaultValue: data.user.motto,
|
|
maxLength: 127,
|
|
},
|
|
{
|
|
name: "rank",
|
|
label: "Rank",
|
|
type: "select",
|
|
defaultValue: data.user.rank,
|
|
options: data.user.permission.ranks.map((rank) => ({
|
|
value: rank.id,
|
|
label: rank.name,
|
|
})),
|
|
},
|
|
];
|
|
return (
|
|
<PeopleCommandForm
|
|
commandId="people.user.update"
|
|
buttonLabel="Save user"
|
|
input={{ userId: data.user.id }}
|
|
fields={fields}
|
|
nestFields
|
|
/>
|
|
);
|
|
}}
|
|
</PeoplePageFrame>
|
|
);
|
|
}
|
|
|
|
export async function renderPeopleUserEditPage(input: HousekeepingPageInput) {
|
|
const id = Number(input.match.params.id);
|
|
const result =
|
|
Number.isSafeInteger(id) && id > 0
|
|
? await peopleUsersQuery.run(input.context, {
|
|
routeId: "people.users.edit",
|
|
id,
|
|
})
|
|
: fail(
|
|
"VALIDATION",
|
|
"errors.housekeeping.validation",
|
|
createCorrelationId(),
|
|
);
|
|
return <PeopleUserEditPage context={input.context} result={result} />;
|
|
}
|