Skip to content

Commit e831c14

Browse files
committed
chore: add import form
1 parent 733842a commit e831c14

File tree

1 file changed

+35
-8
lines changed
  • apps/frontend/src/routes/_auth/administration/participants

1 file changed

+35
-8
lines changed

apps/frontend/src/routes/_auth/administration/participants/import.tsx

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
import { ColumnType, DSVImport, ImportInput, ImportPreview } from "@quassel/ui";
2-
import { createFileRoute } from "@tanstack/react-router";
1+
import { Button, ColumnType, DSVImport, ImportInput, ImportPreview, useForm } from "@quassel/ui";
2+
import { createFileRoute, useNavigate } from "@tanstack/react-router";
3+
import { $api } from "../../../../stores/api";
4+
import { components } from "../../../../api.gen";
35

4-
type BasicType = { childId: string; birthday: string };
6+
type ImportType = { id: string; birthday: string };
7+
type FormValues = components["schemas"]["ParticipantCreationDto"][];
58

6-
const columns: ColumnType<BasicType>[] = [
7-
{ key: "childId", label: "Child ID" },
9+
const columns: ColumnType<ImportType>[] = [
10+
{ key: "id", label: "Child ID" },
811
{ key: "birthday", label: "Birthday" },
912
];
1013

1114
function AdministrationParticipantsImport() {
15+
const n = useNavigate();
16+
const createParticipantMutation = $api.useMutation("post", "/participants", {
17+
onSuccess: () => {
18+
n({ to: "/administration/participants" });
19+
},
20+
});
21+
const f = useForm<FormValues>({
22+
mode: "uncontrolled",
23+
initialValues: [],
24+
});
25+
const handleSubmit = (values: FormValues) => {
26+
createParticipantMutation.mutate({ body: values });
27+
};
28+
const mapValues = (values: ImportType[]): FormValues => {
29+
return values.map((value) => ({
30+
id: parseInt(value.id),
31+
birthday: value.birthday,
32+
}));
33+
};
34+
1235
return (
13-
<div>
14-
<DSVImport<BasicType> columns={columns}>
36+
<form onSubmit={f.onSubmit(handleSubmit)}>
37+
<DSVImport<ImportType> columns={columns} onChange={(values) => f.setValues(mapValues(values))}>
1538
<ImportInput />
1639
<ImportPreview />
1740
</DSVImport>
18-
</div>
41+
42+
<Button type="submit" fullWidth mt="xl" loading={createParticipantMutation.isPending}>
43+
Create
44+
</Button>
45+
</form>
1946
);
2047
}
2148

0 commit comments

Comments
 (0)