Skip to content

Commit a670af8

Browse files
committed
feat: make questionnaires editable in admin
1 parent 52c68c3 commit a670af8

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

apps/frontend/src/routes/_auth/administration/questionnaires/edit.$id.tsx

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
import { createFileRoute, useNavigate } from "@tanstack/react-router";
2-
import { components } from "../../../../api.gen";
32
import { $api } from "../../../../stores/api";
43
import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query";
5-
import { Button, TextInput, useForm } from "@quassel/ui";
4+
import { Button, DateInput, Divider, Group, Stack, Textarea, TextInput, useForm } from "@quassel/ui";
65
import { useEffect } from "react";
6+
import { QuestionnaireEntries } from "../../../../components/questionnaire/QuestionnaireEntries";
7+
import { i18n } from "../../../../stores/i18n";
8+
import { useStore } from "@nanostores/react";
79

8-
type FormValues = components["schemas"]["QuestionnaireMutationDto"];
10+
type FormValues = {
11+
startedAt: Date;
12+
endedAt: Date;
13+
title: string;
14+
remark?: string;
15+
};
16+
17+
const messages = i18n("questionnairesEdit", {
18+
labelRemarks: "Remarks",
19+
labelTitle: "Title",
20+
labelEndedAt: "End date",
21+
labelStartedAt: "Start date",
22+
});
923

1024
function AdministrationQuestionnairesEdit() {
1125
const p = Route.useParams();
26+
const n = useNavigate();
1227
const q = useQueryClient();
28+
29+
const t = useStore(messages);
30+
1331
const { data, isSuccess } = useSuspenseQuery(
1432
$api.queryOptions("get", "/questionnaires/{id}", {
1533
params: { path: { id: p.id } },
1634
})
1735
);
18-
const n = useNavigate();
36+
1937
const editQuestionnaireMutation = $api.useMutation("patch", "/questionnaires/{id}", {
2038
onSuccess: () => {
2139
q.invalidateQueries(
@@ -26,27 +44,40 @@ function AdministrationQuestionnairesEdit() {
2644
n({ to: "/administration/questionnaires" });
2745
},
2846
});
47+
2948
const f = useForm<FormValues>();
30-
const handleSubmit = (values: FormValues) => {
49+
const handleSubmit = ({ startedAt, endedAt, ...rest }: FormValues) => {
3150
editQuestionnaireMutation.mutate({
32-
body: { ...values },
51+
body: { ...rest, startedAt: startedAt.toISOString(), endedAt: endedAt.toISOString() },
3352
params: { path: { id: p.id } },
3453
});
3554
};
3655

3756
useEffect(() => {
38-
f.setValues({ title: data.title });
39-
f.resetDirty();
57+
const { title, remark, startedAt, endedAt } = data;
58+
f.initialize({ title, remark, startedAt: new Date(startedAt), endedAt: new Date(endedAt) });
4059
}, [isSuccess, data]);
4160

4261
return (
43-
<form autoComplete="off" onSubmit={f.onSubmit(handleSubmit)}>
44-
<TextInput label="Name" type="name" {...f.getInputProps("title")} />
62+
<Stack gap="xl">
63+
<form autoComplete="off" onSubmit={f.onSubmit(handleSubmit)}>
64+
<Stack>
65+
<TextInput {...f.getInputProps("title")} label={t.labelTitle} />
66+
<Group>
67+
<DateInput {...f.getInputProps("startedAt")} label={t.labelStartedAt} flex={1} />
68+
<DateInput {...f.getInputProps("endedAt")} label={t.labelEndedAt} flex={1} />
69+
</Group>
70+
71+
<Textarea {...f.getInputProps("remark")} label={t.labelRemarks} rows={8} />
4572

46-
<Button type="submit" fullWidth mt="xl" loading={editQuestionnaireMutation.isPending}>
47-
Change
48-
</Button>
49-
</form>
73+
<Button type="submit" loading={editQuestionnaireMutation.isPending}>
74+
Change
75+
</Button>
76+
</Stack>
77+
</form>
78+
<Divider />
79+
<QuestionnaireEntries questionnaire={data} />
80+
</Stack>
5081
);
5182
}
5283

libs/ui/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export {
8686
useMantineTheme,
8787
} from "@mantine/core";
8888

89-
export { TimeInput } from "@mantine/dates";
89+
export { TimeInput, DateInput } from "@mantine/dates";
9090

9191
export { useDisclosure } from "@mantine/hooks";
9292

0 commit comments

Comments
 (0)