From 4851496b8a71ebc480b6152327fff417a36498fd Mon Sep 17 00:00:00 2001 From: Ridwa Date: Wed, 10 Jul 2024 19:24:57 +0530 Subject: [PATCH 1/3] Improved: made changes such that , state only gets updated if the API call is successful , if no result found user state is made empty and total count 0 , for unsuccessfl API callprevious state is set (#282) --- src/store/modules/facility/actions.ts | 32 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/store/modules/facility/actions.ts b/src/store/modules/facility/actions.ts index 6d7e7613..96316743 100644 --- a/src/store/modules/facility/actions.ts +++ b/src/store/modules/facility/actions.ts @@ -103,28 +103,36 @@ const actions: ActionTree = { ...payload } - const facilities = JSON.parse(JSON.stringify(state.facilities.list)); - let total = 0, facilityList = []; + let facilities = JSON.parse(JSON.stringify(state.facilities.list)); + let total = 0; try { const resp = await FacilityService.fetchFacilities(params) - - if(!hasError(resp) && resp.data.count) { - if(payload.viewIndex && payload.viewIndex > 0) { - facilityList = facilities.concat(resp.data.docs) - } else { - facilityList = resp.data.docs + + if(!hasError(resp)){ + if(resp.data.count > 0){ + if(payload.viewIndex && payload.viewIndex > 0) { + facilities = facilities.concat(resp.data.docs) + } else { + facilities = resp.data.docs + } + total = resp.data.count } - total = resp.data.count - } else { + }else { throw resp.data } } catch(error) { - logger.error(error) + if (payload.viewIndex === 0) { + facilities = []; + total = 0; + } else { + facilities = state.facilities.list; + total = state.facilities.total; + } } + commit(types.FACILITY_LIST_UPDATED , { facilities, total }); emitter.emit("dismissLoader"); - commit(types.FACILITY_LIST_UPDATED , { facilities: facilityList, total }); if(facilities.length) { await dispatch('fetchFacilitiesAdditionalInformation', payload) From 560730507a6d98200f3bc1c1689947f1eb437254 Mon Sep 17 00:00:00 2001 From: Ridwa Date: Thu, 11 Jul 2024 10:37:44 +0530 Subject: [PATCH 2/3] Improved: made changes such that , state only gets updated if the API call is successful , if no result found user state is made empty and total count 0 , for unsuccessfl API callprevious state is set (#282) --- src/store/modules/facility/actions.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/store/modules/facility/actions.ts b/src/store/modules/facility/actions.ts index 96316743..53cdec28 100644 --- a/src/store/modules/facility/actions.ts +++ b/src/store/modules/facility/actions.ts @@ -125,9 +125,6 @@ const actions: ActionTree = { if (payload.viewIndex === 0) { facilities = []; total = 0; - } else { - facilities = state.facilities.list; - total = state.facilities.total; } } From cf6bc57824c0444285eee9f04d157e97787366e4 Mon Sep 17 00:00:00 2001 From: Ridwa Date: Tue, 16 Jul 2024 11:08:23 +0530 Subject: [PATCH 3/3] Improved: Optimized code , removed unnecessary check (#285) --- src/store/modules/facility/actions.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/store/modules/facility/actions.ts b/src/store/modules/facility/actions.ts index 53cdec28..4797ef2e 100644 --- a/src/store/modules/facility/actions.ts +++ b/src/store/modules/facility/actions.ts @@ -109,15 +109,13 @@ const actions: ActionTree = { try { const resp = await FacilityService.fetchFacilities(params) - if(!hasError(resp)){ - if(resp.data.count > 0){ + if(!hasError(resp) && resp.data.count > 0){ if(payload.viewIndex && payload.viewIndex > 0) { facilities = facilities.concat(resp.data.docs) } else { facilities = resp.data.docs } - total = resp.data.count - } + total = resp.data.count }else { throw resp.data }