Skip to content

Commit b0916a7

Browse files
authored
fix: set the api-fetch into it's own hook (#327)
1 parent ad99d05 commit b0916a7

File tree

2 files changed

+70
-34
lines changed

2 files changed

+70
-34
lines changed

src/components/create-production/create-production-page.tsx

+8-34
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
PrimaryButton,
1717
SecondaryButton,
1818
} from "../landing-page/form-elements.tsx";
19-
import { API } from "../../api/api.ts";
2019
import { useGlobalState } from "../../global-state/context-provider.tsx";
2120
import { Spinner } from "../loader/loader.tsx";
2221
import { FlexContainer } from "../generic-components.ts";
@@ -27,13 +26,7 @@ import { NavigateToRootButton } from "../navigate-to-root-button/navigate-to-roo
2726
import { ResponsiveFormContainer } from "../user-settings/user-settings.tsx";
2827
import { isMobile } from "../../bowser.ts";
2928
import { Checkbox } from "../checkbox/checkbox.tsx";
30-
31-
type FormValues = {
32-
productionName: string;
33-
defaultLine: string;
34-
defaultLineProgramOutput: boolean;
35-
lines: { name: string; programOutputLine?: boolean }[];
36-
};
29+
import { FormValues, useCreateProduction } from "./use-create-production.tsx";
3730

3831
const HeaderWrapper = styled.div`
3932
display: flex;
@@ -88,10 +81,8 @@ const CheckboxWrapper = styled.div`
8881

8982
export const CreateProductionPage = () => {
9083
const [, dispatch] = useGlobalState();
91-
const [createdProductionId, setCreatedProductionId] = useState<string | null>(
92-
null
93-
);
94-
const [loading, setLoading] = useState<boolean>(false);
84+
const [createNewProduction, setCreateNewProduction] =
85+
useState<FormValues | null>(null);
9586
const [copiedUrl, setCopiedUrl] = useState<boolean>(false);
9687
const {
9788
formState: { errors },
@@ -116,33 +107,16 @@ export const CreateProductionPage = () => {
116107
},
117108
});
118109

110+
const { createdProductionId, loading } = useCreateProduction({
111+
createNewProduction,
112+
});
113+
119114
const { error: productionFetchError, production } = useFetchProduction(
120115
createdProductionId ? parseInt(createdProductionId, 10) : null
121116
);
122117

123118
const onSubmit: SubmitHandler<FormValues> = (value) => {
124-
setLoading(true);
125-
API.createProduction({
126-
name: value.productionName,
127-
lines: [
128-
{
129-
name: value.defaultLine,
130-
programOutputLine: value.defaultLineProgramOutput,
131-
},
132-
...value.lines,
133-
],
134-
})
135-
.then((v) => {
136-
setCreatedProductionId(v.productionId);
137-
setLoading(false);
138-
})
139-
.catch((error) => {
140-
dispatch({
141-
type: "ERROR",
142-
payload: error,
143-
});
144-
setLoading(false);
145-
});
119+
setCreateNewProduction(value);
146120
};
147121

148122
// Reset form values when created production id changes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect, useState } from "react";
2+
import { useGlobalState } from "../../global-state/context-provider";
3+
import { API } from "../../api/api";
4+
5+
type TUseCreateProduction = {
6+
createNewProduction: FormValues | null;
7+
};
8+
9+
export type FormValues = {
10+
productionName: string;
11+
defaultLine: string;
12+
defaultLineProgramOutput: boolean;
13+
lines: { name: string; programOutputLine?: boolean }[];
14+
};
15+
16+
export const useCreateProduction = ({
17+
createNewProduction,
18+
}: TUseCreateProduction) => {
19+
const [newProduction, setNewProduction] = useState<string | null>(null);
20+
const [loading, setLoading] = useState<boolean>(false);
21+
const [, dispatch] = useGlobalState();
22+
23+
useEffect(() => {
24+
let aborted = false;
25+
26+
if (createNewProduction) {
27+
setLoading(true);
28+
API.createProduction({
29+
name: createNewProduction.productionName,
30+
lines: [
31+
{
32+
name: createNewProduction.defaultLine,
33+
programOutputLine: createNewProduction.defaultLineProgramOutput,
34+
},
35+
...createNewProduction.lines,
36+
],
37+
})
38+
.then((v) => {
39+
if (aborted) return;
40+
41+
setNewProduction(v.productionId);
42+
setLoading(false);
43+
})
44+
.catch((error) => {
45+
dispatch({
46+
type: "ERROR",
47+
payload: error,
48+
});
49+
setLoading(false);
50+
});
51+
}
52+
53+
return () => {
54+
aborted = true;
55+
};
56+
}, [createNewProduction, dispatch]);
57+
58+
return {
59+
createdProductionId: newProduction,
60+
loading,
61+
};
62+
};

0 commit comments

Comments
 (0)