Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move api-fetch to it's own hook #328

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
};