Skip to content

Commit

Permalink
Merge pull request #186 from bcgov/WAYF-233-Reduce-load
Browse files Browse the repository at this point in the history
[WAYF-223] FEATURE** reduce load
  • Loading branch information
dallascrichmond authored Jul 25, 2023
2 parents e99c0f6 + 1e75b3c commit 08dd36a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/api/controllers/location-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const updatedInfo = async (req: Request, res: Response) => {
const responseObject: ReturnObject = {};
if (req.body.updateArr && typeof req.body.updateArr === typeof []) {
const datesArr: Array<UpdateDate> = await dateModel.find({});
responseObject.datesArr = datesArr;
responseObject.updateArr = datesArr;
const promises = req.body.updateArr.map(async (item: UpdateDate) => {
if (servicesOffered.includes(item.serviceType)) {
const serviceDate: any = datesArr.find((service: UpdateDate) => (
Expand Down
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;
20 changes: 17 additions & 3 deletions src/frontend-pwa/src/services/app/useAppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,23 @@ const useAppService = () => {
const setAppData = async (isOnline: boolean) => {
if (isOnline) {
try {
const data = await axios.get(`${constants.BACKEND_URL}/api/locations`);
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`);
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 08dd36a

Please sign in to comment.