Skip to content

Commit

Permalink
chore: moved fetch-production-list into a separate hook
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 authored and martinstark committed May 3, 2024
1 parent 48fcd4e commit 387e0bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
53 changes: 6 additions & 47 deletions src/components/landing-page/productions-list.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import styled from "@emotion/styled";
import { useEffect, useState } from "react";
import { API } from "../../api/api.ts";
import { TBasicProduction } from "../production-line/types.ts";
import { useEffect } from "react";
import { useGlobalState } from "../../global-state/context-provider.tsx";
import { LoaderDots } from "../loader/loader.tsx";
import { useRefreshAnimation } from "./use-refresh-animation.ts";
import { DisplayContainerHeader } from "./display-container-header.tsx";
import { DisplayContainer } from "../generic-components.ts";
import { ManageProductionButton } from "./manage-production-button.tsx";
import { LocalError } from "../error.tsx";
import { useFetchProductionList } from "./use-fetch-production-list.ts";

const ProductionListContainer = styled.div`
display: flex;
Expand Down Expand Up @@ -42,50 +41,10 @@ const ProductionId = styled.div`
`;

export const ProductionsList = () => {
const [productions, setProductions] = useState<TBasicProduction[]>([]);
const [intervalLoad, setIntervalLoad] = useState<boolean>(false);
const [{ reloadProductionList }, dispatch] = useGlobalState();
const [doInitialLoad, setDoInitialLoad] = useState(true);
const [error, setError] = useState<Error | null>(null);
const [{ reloadProductionList }] = useGlobalState();

// TODO extract to separate hook file
useEffect(() => {
let aborted = false;

if (reloadProductionList || intervalLoad || doInitialLoad) {
API.listProductions()
.then((result) => {
if (aborted) return;

setProductions(
result
// pick laste 10 items
.slice(-10)
);

dispatch({
type: "PRODUCTION_LIST_FETCHED",
});

setIntervalLoad(false);

setDoInitialLoad(false);

setError(null);
})
.catch((e) => {
setError(
e instanceof Error
? e
: new Error("Failed to fetch production list.")
);
});
}

return () => {
aborted = true;
};
}, [dispatch, intervalLoad, reloadProductionList, doInitialLoad]);
const { productions, doInitialLoad, error, setIntervalLoad } =
useFetchProductionList();

const showRefreshing = useRefreshAnimation({
reloadProductionList,
Expand All @@ -100,7 +59,7 @@ export const ProductionsList = () => {
return () => {
window.clearInterval(interval);
};
}, []);
}, [setIntervalLoad]);

return (
<>
Expand Down
56 changes: 56 additions & 0 deletions src/components/landing-page/use-fetch-production-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useEffect, useState } from "react";
import { useGlobalState } from "../../global-state/context-provider";
import { API } from "../../api/api.ts";
import { TBasicProduction } from "../production-line/types.ts";

export const useFetchProductionList = () => {
const [productions, setProductions] = useState<TBasicProduction[]>([]);
const [doInitialLoad, setDoInitialLoad] = useState(true);
const [intervalLoad, setIntervalLoad] = useState<boolean>(false);
const [error, setError] = useState<Error | null>(null);

const [{ reloadProductionList }, dispatch] = useGlobalState();

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

if (reloadProductionList || intervalLoad || doInitialLoad) {
API.listProductions()
.then((result) => {
if (aborted) return;

setProductions(
result
// pick laste 10 items
.slice(-10)
);

dispatch({
type: "PRODUCTION_LIST_FETCHED",
});

setIntervalLoad(false);
setDoInitialLoad(false);
setError(null);
})
.catch((e) => {
setError(
e instanceof Error
? e
: new Error("Failed to fetch production list.")
);
});
}

return () => {
aborted = true;
};
}, [dispatch, intervalLoad, reloadProductionList, doInitialLoad]);

return {
productions,
doInitialLoad,
error,
setIntervalLoad,
};
};

0 comments on commit 387e0bf

Please sign in to comment.