|
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"; |
3 | 5 |
|
4 |
| -type BasicType = { childId: string; birthday: string }; |
| 6 | +type ImportType = { id: string; birthday: string }; |
| 7 | +type FormValues = components["schemas"]["ParticipantCreationDto"][]; |
5 | 8 |
|
6 |
| -const columns: ColumnType<BasicType>[] = [ |
7 |
| - { key: "childId", label: "Child ID" }, |
| 9 | +const columns: ColumnType<ImportType>[] = [ |
| 10 | + { key: "id", label: "Child ID" }, |
8 | 11 | { key: "birthday", label: "Birthday" },
|
9 | 12 | ];
|
10 | 13 |
|
11 | 14 | 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 | + |
12 | 35 | 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))}> |
15 | 38 | <ImportInput />
|
16 | 39 | <ImportPreview />
|
17 | 40 | </DSVImport>
|
18 |
| - </div> |
| 41 | + |
| 42 | + <Button type="submit" fullWidth mt="xl" loading={createParticipantMutation.isPending}> |
| 43 | + Create |
| 44 | + </Button> |
| 45 | + </form> |
19 | 46 | );
|
20 | 47 | }
|
21 | 48 |
|
|
0 commit comments