|
1 |
| -import { Button } from "@quassel/ui"; |
| 1 | +import { Button, Group, Radio, Select, Stack, useForm } from "@quassel/ui"; |
2 | 2 | import { createFileRoute } from "@tanstack/react-router";
|
3 | 3 | import { $api } from "../../../../stores/api";
|
| 4 | +import { i18n } from "../../../../stores/i18n"; |
| 5 | +import { useStore } from "@nanostores/react"; |
| 6 | +import { useSuspenseQuery } from "@tanstack/react-query"; |
| 7 | + |
| 8 | +const messages = i18n("AdministrationExportIndexRoute", { |
| 9 | + title: "Carers", |
| 10 | + studyLabel: "Study", |
| 11 | + studyPlaceholder: "Select a study", |
| 12 | + formatLabel: "File format", |
| 13 | + csvLabel: "Comma-separated values (CSV)", |
| 14 | + sqlLabel: "Database export (SQL)", |
| 15 | + formAction: "Download", |
| 16 | +}); |
| 17 | + |
| 18 | +type FormValues = { |
| 19 | + fileType: "csv" | "sql"; |
| 20 | + studyId?: string; |
| 21 | +}; |
4 | 22 |
|
5 | 23 | function AdministrationExportIndex() {
|
| 24 | + const t = useStore(messages); |
| 25 | + const f = useForm<FormValues>({ |
| 26 | + mode: "uncontrolled", |
| 27 | + initialValues: { |
| 28 | + fileType: "csv", |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + const studies = useSuspenseQuery($api.queryOptions("get", "/studies")); |
6 | 33 | const { isDownloading, downloadFile } = $api.useDownload("/export", "dump.sql");
|
7 | 34 | return (
|
8 |
| - <div> |
9 |
| - <Button loading={isDownloading} onClick={() => downloadFile()}> |
10 |
| - Download |
11 |
| - </Button> |
12 |
| - </div> |
| 35 | + <form onSubmit={f.onSubmit(() => downloadFile())}> |
| 36 | + <Stack> |
| 37 | + <Select |
| 38 | + label={t.studyLabel} |
| 39 | + placeholder={t.studyPlaceholder} |
| 40 | + data={studies.data.map((s) => ({ label: s.title, value: s.id.toString() }))} |
| 41 | + /> |
| 42 | + <Radio.Group label={t.formatLabel} withAsterisk {...f.getInputProps("fileType")}> |
| 43 | + <Group> |
| 44 | + <Radio checked value="csv" label={t.csvLabel} /> |
| 45 | + <Radio value="sql" label={t.sqlLabel} /> |
| 46 | + </Group> |
| 47 | + </Radio.Group> |
| 48 | + <Button type="submit" loading={isDownloading}> |
| 49 | + {t.formAction} |
| 50 | + </Button> |
| 51 | + </Stack> |
| 52 | + </form> |
13 | 53 | );
|
14 | 54 | }
|
15 | 55 |
|
16 | 56 | export const Route = createFileRoute("/_auth/administration/export/")({
|
| 57 | + loader: ({ context: { queryClient } }) => { |
| 58 | + queryClient.ensureQueryData($api.queryOptions("get", "/studies")); |
| 59 | + }, |
17 | 60 | component: AdministrationExportIndex,
|
18 | 61 | });
|
0 commit comments