Skip to content

Commit 173e20c

Browse files
committed
refactor: extract carer form to separate component
1 parent f56931a commit 173e20c

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Button, TextInput, useForm } from "@quassel/ui";
2+
import { useEffect } from "react";
3+
4+
type CarerFormProps = {
5+
carer?: FormValues;
6+
onSave: (carer: FormValues) => void;
7+
loading: boolean;
8+
};
9+
10+
type FormValues = {
11+
name: string;
12+
};
13+
14+
export function CarerForm({ carer, onSave, loading }: CarerFormProps) {
15+
const f = useForm<FormValues>({
16+
initialValues: {
17+
name: "",
18+
},
19+
});
20+
21+
useEffect(() => {
22+
if (carer) f.initialize(carer);
23+
}, [carer]);
24+
25+
return (
26+
<form autoComplete="off" onSubmit={f.onSubmit(onSave)}>
27+
<TextInput label="Name" type="name" {...f.getInputProps("name")} />
28+
29+
<Button type="submit" fullWidth mt="xl" loading={loading}>
30+
Change
31+
</Button>
32+
</form>
33+
);
34+
}

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

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { createFileRoute, useNavigate } from "@tanstack/react-router";
2-
import { components } from "../../../../api.gen";
32
import { $api } from "../../../../stores/api";
43
import { useQueryClient } from "@tanstack/react-query";
5-
import { Button, TextInput, useForm } from "@quassel/ui";
6-
import { useEffect } from "react";
7-
8-
type FormValues = components["schemas"]["CarerMutationDto"];
4+
import { CarerForm } from "../../../../components/CarerForm";
5+
import { components } from "../../../../api.gen";
96

107
function AdministrationCarersEdit() {
118
const p = Route.useParams();
129
const q = useQueryClient();
13-
const { data, isSuccess } = $api.useSuspenseQuery("get", "/carers/{id}", { params: { path: { id: p.id } } });
10+
const { data } = $api.useSuspenseQuery("get", "/carers/{id}", { params: { path: { id: p.id } } });
1411
const n = useNavigate();
1512
const editCarerMutation = $api.useMutation("patch", "/carers/{id}", {
1613
onSuccess: () => {
@@ -22,34 +19,15 @@ function AdministrationCarersEdit() {
2219
n({ to: "/administration/carers" });
2320
},
2421
});
25-
const f = useForm<FormValues>({
26-
initialValues: {
27-
name: "",
28-
},
29-
});
30-
const handleSubmit = (values: FormValues) => {
22+
23+
const handleSubmit = (values: components["schemas"]["CarerMutationDto"]) => {
3124
editCarerMutation.mutate({
3225
body: { ...values },
3326
params: { path: { id: p.id } },
3427
});
3528
};
3629

37-
useEffect(() => {
38-
f.setValues({ ...data, participant: data.participant?.id });
39-
f.resetDirty();
40-
}, [isSuccess, data]);
41-
42-
return (
43-
<>
44-
<form autoComplete="off" onSubmit={f.onSubmit(handleSubmit)}>
45-
<TextInput label="Name" type="name" {...f.getInputProps("name")} />
46-
47-
<Button type="submit" fullWidth mt="xl" loading={editCarerMutation.isPending}>
48-
Change
49-
</Button>
50-
</form>
51-
</>
52-
);
30+
return <CarerForm carer={data} onSave={handleSubmit} loading={editCarerMutation.isPending} />;
5331
}
5432

5533
export const Route = createFileRoute("/_auth/administration/carers/edit/$id")({

apps/frontend/src/routes/_auth/administration/carers/new.tsx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { createFileRoute, useNavigate } from "@tanstack/react-router";
2-
import { Button, TextInput, useForm } from "@quassel/ui";
32
import { $api } from "../../../../stores/api";
43
import { components } from "../../../../api.gen";
5-
6-
type FormValues = components["schemas"]["CarerCreationDto"];
4+
import { CarerForm } from "../../../../components/CarerForm";
75

86
function AdministrationCarersNew() {
97
const n = useNavigate();
@@ -12,27 +10,12 @@ function AdministrationCarersNew() {
1210
n({ to: "/administration/carers" });
1311
},
1412
});
15-
const f = useForm<FormValues>({
16-
mode: "uncontrolled",
17-
initialValues: {
18-
name: "",
19-
},
20-
});
21-
const handleSubmit = (values: FormValues) => {
13+
14+
const handleSubmit = (values: components["schemas"]["CarerCreationDto"]) => {
2215
createCarerMutation.mutate({ body: values });
2316
};
2417

25-
return (
26-
<>
27-
<form autoComplete="off" onSubmit={f.onSubmit(handleSubmit)}>
28-
<TextInput label="Name" type="text" {...f.getInputProps("name")} required />
29-
30-
<Button type="submit" fullWidth mt="xl" loading={createCarerMutation.isPending}>
31-
Create
32-
</Button>
33-
</form>
34-
</>
35-
);
18+
return <CarerForm onSave={handleSubmit} loading={createCarerMutation.isPending} />;
3619
}
3720

3821
export const Route = createFileRoute("/_auth/administration/carers/new")({

0 commit comments

Comments
 (0)