From 0b388cd3c0eced2a9e9478eda4e8e3d037ddea1a Mon Sep 17 00:00:00 2001 From: dallascrichmond <113049138+dallascrichmond@users.noreply.github.com> Date: Mon, 24 Jul 2023 19:25:10 -0700 Subject: [PATCH] Added appropriate functions --- src/frontend-pwa/src/constants/Constants.ts | 1 + .../src/services/app/useAppService.ts | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/frontend-pwa/src/constants/Constants.ts b/src/frontend-pwa/src/constants/Constants.ts index 25bd6dc..b9289e9 100644 --- a/src/frontend-pwa/src/constants/Constants.ts +++ b/src/frontend-pwa/src/constants/Constants.ts @@ -9,6 +9,7 @@ const constants = { REPORTS_KEY: 'reports', UNSENT_REPORTS_KEY: 'unsentReports', APP_INSTALL_KEY: 'appInstalled', + UPDATE_ARRAY_KEY: 'updateArr', }; export default constants; diff --git a/src/frontend-pwa/src/services/app/useAppService.ts b/src/frontend-pwa/src/services/app/useAppService.ts index e9e5810..fef11d1 100644 --- a/src/frontend-pwa/src/services/app/useAppService.ts +++ b/src/frontend-pwa/src/services/app/useAppService.ts @@ -38,10 +38,24 @@ const useAppService = () => { const setAppData = async (isOnline: boolean) => { if (isOnline) { try { - const data = await axios.get(`${constants.BACKEND_URL}/api/locations`); - console.log('Data: ', data); - saveDataToLocalStorage(constants.APP_DATA_KEY, data); - dispatch({ type: SET_APP_DATA, payload: data }); + if (localStorageKeyExists(constants.UPDATE_ARRAY_KEY)) { + const data = getDataFromLocalStorage(constants.APP_DATA_KEY); + const updateData = await axios.post(`${constants.BACKEND_URL}/api/locations`, { updateArr: getDataFromLocalStorage(constants.UPDATE_ARRAY_KEY) }); + Object.entries(updateData.data).forEach(([key, value]) => { + if (key in data) { + data[key] = value; + } + }); + saveDataToLocalStorage(constants.UPDATE_ARRAY_KEY, data.data.updateArr); + saveDataToLocalStorage(constants.APP_DATA_KEY, data); + dispatch({ type: SET_APP_DATA, payload: data }); + } else { + const data = await axios.get(`${constants.BACKEND_URL}/api/locations`); + console.log('data: ', data); + saveDataToLocalStorage(constants.UPDATE_ARRAY_KEY, data.data.updateArr); + saveDataToLocalStorage(constants.APP_DATA_KEY, data); + dispatch({ type: SET_APP_DATA, payload: data }); + } } catch (e) { console.error(e); }