|
1 |
| -import { Button, Group } from "@quassel/ui"; |
| 1 | +import { Button, Group, Stack, Textarea } from "@quassel/ui"; |
2 | 2 | import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
|
3 | 3 | import { i18n } from "../../../../../stores/i18n";
|
4 | 4 | import { useStore } from "@nanostores/react";
|
| 5 | +import { useForm } from "@mantine/form"; |
| 6 | +import { $api } from "../../../../../stores/api"; |
| 7 | +import { useEffect } from "react"; |
5 | 8 |
|
6 | 9 | export const messages = i18n("questionnaireRemarks", {
|
7 | 10 | title: "Add remarks",
|
| 11 | + remarkDescription: "Use this field to point out any exceptions to the language exposure like vacations etc...", |
8 | 12 | backAction: "Back",
|
9 | 13 | saveAction: "Save",
|
10 | 14 | formAction: "Save and complete",
|
11 | 15 | });
|
12 | 16 |
|
| 17 | +type FormValues = { |
| 18 | + remark: string; |
| 19 | +}; |
| 20 | + |
13 | 21 | function QuestionnaireRemarks() {
|
14 | 22 | const n = useNavigate();
|
15 | 23 | const p = Route.useParams();
|
16 | 24 |
|
17 | 25 | const t = useStore(messages);
|
18 | 26 |
|
19 |
| - const handleSubmit = () => { |
| 27 | + const f = useForm<FormValues>({ |
| 28 | + initialValues: { |
| 29 | + remark: "", |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + const { data: questionnaire } = $api.useSuspenseQuery("get", "/questionnaires/{id}", { params: { path: { id: p.id } } }); |
| 34 | + const updateMutation = $api.useMutation("patch", "/questionnaires/{id}"); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (questionnaire) { |
| 38 | + f.setValues(questionnaire); |
| 39 | + f.resetDirty(); |
| 40 | + } |
| 41 | + }, [questionnaire]); |
| 42 | + |
| 43 | + const onSave = (values: FormValues) => updateMutation.mutateAsync({ params: { path: p }, body: values }); |
| 44 | + |
| 45 | + const handleSubmit = async (values: FormValues) => { |
| 46 | + await onSave(values); |
20 | 47 | n({ to: "/questionnaire/$id/overview", params: p });
|
21 | 48 | };
|
22 | 49 |
|
23 | 50 | return (
|
24 |
| - <form onSubmit={handleSubmit}> |
25 |
| - <h3>{t.title}</h3> |
26 |
| - <Group> |
27 |
| - <Link to="/questionnaire/$id/entries" params={p}> |
28 |
| - <Button variant="light">{t.backAction}</Button> |
29 |
| - </Link> |
30 |
| - <Button variant="outline">{t.saveAction}</Button> |
31 |
| - <Button type="submit">{t.formAction}</Button> |
32 |
| - </Group> |
| 51 | + <form onSubmit={f.onSubmit(handleSubmit)}> |
| 52 | + <Stack> |
| 53 | + <h3>{t.title}</h3> |
| 54 | + |
| 55 | + <Textarea {...f.getInputProps("remark")} description={t.remarkDescription} rows={8} /> |
| 56 | + |
| 57 | + <Group> |
| 58 | + <Link to="/questionnaire/$id/entries" params={p}> |
| 59 | + <Button variant="light">{t.backAction}</Button> |
| 60 | + </Link> |
| 61 | + <Button variant="outline" onClick={() => onSave(f.getValues())} loading={updateMutation.isPending}> |
| 62 | + {t.saveAction} |
| 63 | + </Button> |
| 64 | + <Button type="submit" loading={updateMutation.isPending}> |
| 65 | + {t.formAction} |
| 66 | + </Button> |
| 67 | + </Group> |
| 68 | + </Stack> |
33 | 69 | </form>
|
34 | 70 | );
|
35 | 71 | }
|
36 | 72 |
|
37 | 73 | export const Route = createFileRoute("/_auth/questionnaire/_questionnaire/$id/remarks")({
|
| 74 | + loader: ({ params, context: { queryClient } }) => |
| 75 | + queryClient.ensureQueryData( |
| 76 | + $api.queryOptions("get", "/questionnaires/{id}", { |
| 77 | + params: { path: { id: params.id } }, |
| 78 | + }) |
| 79 | + ), |
38 | 80 | component: QuestionnaireRemarks,
|
39 | 81 | });
|
0 commit comments