Skip to content

Commit

Permalink
fix: set the api-fetch into it's own hook (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 authored and martinstark committed Jan 29, 2025
1 parent 6ded2a6 commit ddda91a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 34 deletions.
42 changes: 8 additions & 34 deletions src/components/create-production/create-production-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
PrimaryButton,
SecondaryButton,
} from "../landing-page/form-elements.tsx";
import { API } from "../../api/api.ts";
import { useGlobalState } from "../../global-state/context-provider.tsx";
import { Spinner } from "../loader/loader.tsx";
import { FlexContainer } from "../generic-components.ts";
Expand All @@ -27,13 +26,7 @@ import { NavigateToRootButton } from "../navigate-to-root-button/navigate-to-roo
import { ResponsiveFormContainer } from "../user-settings/user-settings.tsx";
import { isMobile } from "../../bowser.ts";
import { Checkbox } from "../checkbox/checkbox.tsx";

type FormValues = {
productionName: string;
defaultLine: string;
defaultLineProgramOutput: boolean;
lines: { name: string; programOutputLine?: boolean }[];
};
import { FormValues, useCreateProduction } from "./use-create-production.tsx";

const HeaderWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -88,10 +81,8 @@ const CheckboxWrapper = styled.div`

export const CreateProductionPage = () => {
const [, dispatch] = useGlobalState();
const [createdProductionId, setCreatedProductionId] = useState<string | null>(
null
);
const [loading, setLoading] = useState<boolean>(false);
const [createNewProduction, setCreateNewProduction] =
useState<FormValues | null>(null);
const [copiedUrl, setCopiedUrl] = useState<boolean>(false);
const {
formState: { errors },
Expand All @@ -116,33 +107,16 @@ export const CreateProductionPage = () => {
},
});

const { createdProductionId, loading } = useCreateProduction({
createNewProduction,
});

const { error: productionFetchError, production } = useFetchProduction(
createdProductionId ? parseInt(createdProductionId, 10) : null
);

const onSubmit: SubmitHandler<FormValues> = (value) => {
setLoading(true);
API.createProduction({
name: value.productionName,
lines: [
{
name: value.defaultLine,
programOutputLine: value.defaultLineProgramOutput,
},
...value.lines,
],
})
.then((v) => {
setCreatedProductionId(v.productionId);
setLoading(false);
})
.catch((error) => {
dispatch({
type: "ERROR",
payload: error,
});
setLoading(false);
});
setCreateNewProduction(value);
};

// Reset form values when created production id changes
Expand Down
62 changes: 62 additions & 0 deletions src/components/create-production/use-create-production.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useEffect, useState } from "react";
import { useGlobalState } from "../../global-state/context-provider";
import { API } from "../../api/api";

type TUseCreateProduction = {
createNewProduction: FormValues | null;
};

export type FormValues = {
productionName: string;
defaultLine: string;
defaultLineProgramOutput: boolean;
lines: { name: string; programOutputLine?: boolean }[];
};

export const useCreateProduction = ({
createNewProduction,
}: TUseCreateProduction) => {
const [newProduction, setNewProduction] = useState<string | null>(null);
const [loading, setLoading] = useState<boolean>(false);
const [, dispatch] = useGlobalState();

useEffect(() => {
let aborted = false;

if (createNewProduction) {
setLoading(true);
API.createProduction({
name: createNewProduction.productionName,
lines: [
{
name: createNewProduction.defaultLine,
programOutputLine: createNewProduction.defaultLineProgramOutput,
},
...createNewProduction.lines,
],
})
.then((v) => {
if (aborted) return;

setNewProduction(v.productionId);
setLoading(false);
})
.catch((error) => {
dispatch({
type: "ERROR",
payload: error,
});
setLoading(false);
});
}

return () => {
aborted = true;
};
}, [createNewProduction, dispatch]);

return {
createdProductionId: newProduction,
loading,
};
};

0 comments on commit ddda91a

Please sign in to comment.