Skip to content

Commit

Permalink
Added appropriate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dallascrichmond committed Jul 25, 2023
1 parent 280478b commit 0b388cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/frontend-pwa/src/constants/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const constants = {
REPORTS_KEY: 'reports',
UNSENT_REPORTS_KEY: 'unsentReports',
APP_INSTALL_KEY: 'appInstalled',
UPDATE_ARRAY_KEY: 'updateArr',
};

export default constants;
22 changes: 18 additions & 4 deletions src/frontend-pwa/src/services/app/useAppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0b388cd

Please sign in to comment.