diff --git a/pwa/app/help/fetch-service.js b/pwa/app/help/fetch-service.js index 31d910f6ac173c1acd666fea12a53b343cdab5b9..b1103e5521ebb93444060cc1ede21693c2e3f526 100644 --- a/pwa/app/help/fetch-service.js +++ b/pwa/app/help/fetch-service.js @@ -10,6 +10,7 @@ async function getDonutContent(year) { 'Content-Type': 'application/json' } }); + /* return new Promise((resolve, reject) => { resolve( [ diff --git a/pwa/app/page.jsx b/pwa/app/page.jsx index 0d7157b52895c4c1d8f1066a435a2a0a45e9bb14..e5838a94ed1d76add7c8c9c66e0514844066628e 100644 --- a/pwa/app/page.jsx +++ b/pwa/app/page.jsx @@ -19,19 +19,19 @@ export default function Page() { const [chartData, setChartData] = useState([]); const [timeseries, setTimeseries] = useState([]); - const [isLoadingBar, setIsLoadingBar] = useState(false); + const [isLoading, setIsLoading] = useState(false); const [isLoadingDonut, setIsLoadingDonut] = useState(false); - const [isLoadingTimeseries, setIsLoadingTimesseries] = useState(false); + const [isLoadingBarChart, setIsLoadingBarChart] = useState(false); const handleFormSubmit = async (formData) => { - setIsLoadingBar(true); + setIsLoadingBarChart(true); const response = await FetchService.fetchBarChartData(formData); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); setChartData(data); - setIsLoadingBar(false); + setIsLoadingBarChart(false); }; const handleYearSubmit = async (year) => { @@ -46,33 +46,40 @@ export default function Page() { } useEffect(() => { - (async () => { - // bar chart - const formData = { - startDate: `${constants.initialBarChartFormData.startDate}`, - endDate: `${constants.initialBarChartFormData.endDate}`, - granularity: `${constants.initialBarChartFormData.granularity}` - }; - await handleFormSubmit(formData); - - // time series + const fetchData = async () => { try { - setIsLoadingTimesseries(true); - const response = await FetchService.getTimesSeries(); - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); + setIsLoading(true); + + const formData = { + startDate: constants.initialBarChartFormData.startDate, + endDate: constants.initialBarChartFormData.endDate, + granularity: constants.initialBarChartFormData.granularity, } - const data = await response.json(); - setTimeseries(data); - setIsLoadingTimesseries(false); + + const year = constants.initialDonutChartYear.selectedYear; + + const [timeseriesResponse, barChartResponse, donutChartResponse] = await Promise.all([ + FetchService.getTimesSeries(), + FetchService.fetchBarChartData(formData), + FetchService.getDonutContent(year), + ]); + + + if (!timeseriesResponse.ok || !barChartResponse.ok || !donutChartResponse.ok) { + throw new Error(`HTTP error!`); + } + + setTimeseries(await timeseriesResponse.json()); + setChartData(await barChartResponse.json()); + setDonutValues(await donutChartResponse.json()); + + setIsLoading(false); } catch (error) { - console.error('Error fetching time series data:', error); + console.error('Error fetching data:', error); } + }; - // donut - await handleYearSubmit(2020); - - })(); + fetchData(); }, []); return ( @@ -80,7 +87,7 @@ export default function Page() {