Skip to content

Commit

Permalink
fix issue on fetch data first render
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Jan 18, 2024
1 parent ef5c07d commit 8d9e76f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/components/pages/pageIndex/HeatmapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const HeatmapChart = () => {
const [heatmapChartOptions, setHeatmapChartOptions] = useState(
defaultHeatmapChartOptions
);
const [platformFetched, setPlatformFetched] = useState<boolean>(false);

const defaultEndDate = moment().subtract(1, 'day');
const defaultStartDate = moment(defaultEndDate).subtract(7, 'days');
Expand Down Expand Up @@ -93,8 +94,11 @@ const HeatmapChart = () => {
};

useEffect(() => {
fetchPlatformChannels();
fetchData();
const initializeSelectedChannels = async () => {
await fetchPlatformChannels();
};

initializeSelectedChannels();
}, []);

const handleSelectedZone = (zone: string) => {
Expand Down Expand Up @@ -144,7 +148,9 @@ const HeatmapChart = () => {
};

const handleFetchHeatmapByChannels = () => {
fetchData();
if (platformFetched) {
fetchData();
}
};

const fetchPlatformChannels = async () => {
Expand All @@ -158,6 +164,7 @@ const HeatmapChart = () => {
} else {
await refreshData(platformId);
}
setPlatformFetched(true);
}
} catch (error) {
} finally {
Expand All @@ -169,7 +176,7 @@ const HeatmapChart = () => {
return;
}
fetchData();
}, [dateRange, selectedZone, platformId]);
}, [dateRange, selectedZone, platformId, platformFetched]);

return (
<div className="bg-white shadow-box rounded-lg p-5 min-h-[400px]">
Expand Down

0 comments on commit 8d9e76f

Please sign in to comment.