Skip to content

Commit

Permalink
feat: add initial load logic to production list
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstark committed Apr 11, 2024
1 parent e40dca5 commit 3b4bd68
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/components/landing-page/productions-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ export const ProductionsList = () => {
const [productions, setProductions] = useState<TProduction[]>([]);
const [intervalLoad, setIntervalLoad] = useState<boolean>(false);
const [{ reloadProductionList }, dispatch] = useGlobalState();
const [doInitialLoad, setDoInitialLoad] = useState(true);

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

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

setProductions(
result
// pick laste 10 items
Expand All @@ -67,10 +69,14 @@ export const ProductionsList = () => {
};
})
);

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

setIntervalLoad(false);

setDoInitialLoad(false);
})
.catch(() => {
// TODO handle error/retry
Expand All @@ -80,9 +86,12 @@ export const ProductionsList = () => {
return () => {
aborted = true;
};
}, [dispatch, intervalLoad, reloadProductionList]);
}, [dispatch, intervalLoad, reloadProductionList, doInitialLoad]);

const showRefreshing = useRefreshAnimation({ reloadProductionList });
const showRefreshing = useRefreshAnimation({
reloadProductionList,
doInitialLoad,
});

useEffect(() => {
const interval = window.setInterval(() => {
Expand Down
8 changes: 5 additions & 3 deletions src/components/landing-page/use-refresh-animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { useEffect, useState } from "react";

type TUseRefreshAnimationOptions = {
reloadProductionList: boolean;
doInitialLoad: boolean;
};

export const useRefreshAnimation = ({
reloadProductionList,
doInitialLoad,
}: TUseRefreshAnimationOptions) => {
const [showRefreshing, setShowRefreshing] = useState(true);

useEffect(() => {
let timeout: number | null = null;

if (showRefreshing) {
if (showRefreshing || doInitialLoad) {
timeout = window.setTimeout(() => {
setShowRefreshing(false);
}, 1500);
Expand All @@ -23,13 +25,13 @@ export const useRefreshAnimation = ({
window.clearTimeout(timeout);
}
};
}, [showRefreshing]);
}, [showRefreshing, doInitialLoad]);

useEffect(() => {
if (reloadProductionList) {
setShowRefreshing(true);
}
}, [reloadProductionList]);
}, [reloadProductionList, doInitialLoad]);

return showRefreshing;
};
2 changes: 1 addition & 1 deletion src/global-state/global-state-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { uniqBy } from "../helpers";
const initialGlobalState: TGlobalState = {
production: null,
error: null,
reloadProductionList: true,
reloadProductionList: false,
devices: null,
joinProductionOptions: null,
};
Expand Down

0 comments on commit 3b4bd68

Please sign in to comment.