Skip to content

Commit

Permalink
feat: added loading-boolean to fetch-production hook
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 committed Apr 23, 2024
1 parent ca0f0b3 commit 5a678cf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/components/landing-page/use-fetch-production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@ import { TProduction } from "../production-line/types";
type TUseFetchProduction = (id: number | null) => {
production: TProduction | null;
error: Error | null;
loading: boolean;
};

export const useFetchProduction: TUseFetchProduction = (id) => {
const [production, setProduction] = useState<TProduction | null>(null);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | null>(null);

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

if (id) {
API.fetchProduction(id)
.then((p) => {
if (aborted) return;

setError(null);

setLoading(false);
setProduction(p);
})
.catch((e) => {
setProduction(null);

setLoading(false);
setError(e);
});
} else {
setProduction(null);
setLoading(false);
}

return () => {
Expand All @@ -40,5 +44,6 @@ export const useFetchProduction: TUseFetchProduction = (id) => {
return {
error,
production,
loading,
};
};

0 comments on commit 5a678cf

Please sign in to comment.