Skip to content

Commit

Permalink
Improved: code to handle parties adding removing action call at modal…
Browse files Browse the repository at this point in the history
… level (#12)
  • Loading branch information
amansinghbais committed Nov 22, 2023
1 parent 0745890 commit b2bca77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
40 changes: 31 additions & 9 deletions src/components/AddStaffMemberModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { hasError } from "@/adapter";
import logger from "@/logger";
import { mapGetters, useStore } from "vuex";
import { showToast } from "@/utils";
import { DateTime } from "luxon";
export default defineComponent({
name: "AddStaffMemberModal",
Expand All @@ -96,7 +97,7 @@ export default defineComponent({
IonTitle,
IonToolbar
},
props: ['selectedParties'],
props: ['facilityId', 'selectedParties'],
data () {
return {
parties: [],
Expand Down Expand Up @@ -162,7 +163,7 @@ export default defineComponent({
removeSelectedParty(partyId: string) {
this.selectedPartyValues = this.selectedPartyValues.filter((party: any) => party.partyId !== partyId)
},
saveParties() {
async saveParties() {
const partiesToAdd = this.selectedPartyValues.filter((selectedParty: any) => !this.selectedParties.some((party: any) => party.partyId === selectedParty.partyId && party.roleTypeId === selectedParty.roleTypeId))
const partiesToRemove = this.selectedParties.filter((party: any) => !this.selectedPartyValues.some((selectedParty: any) => party.partyId === selectedParty.partyId))
const partiesRoleChanged = this.selectedParties.filter((party: any) => this.selectedPartyValues.some((selectedParty: any) => selectedParty.partyId === party.partyId && selectedParty.roleTypeId !== party.roleTypeId))
Expand All @@ -173,13 +174,34 @@ export default defineComponent({
return;
}
modalController.dismiss({
dismissed: true,
value: {
partiesToAdd,
partiesToRemove
}
});
const removeResponses = await Promise.allSettled(partiesToRemove
.map(async (party: any) => await FacilityService.removePartyFromFacility({
facilityId: this.facilityId,
fromDate: party.fromDate,
thruDate: DateTime.now().toMillis(),
partyId: party.partyId,
roleTypeId: party.roleTypeId
}))
)
const addResponses = await Promise.allSettled(partiesToAdd
.map(async (party: any) => await FacilityService.addPartyToFacility({
facilityId: this.facilityId,
partyId: party.partyId,
roleTypeId: party.roleTypeId
}))
)
const hasFailedResponse = [...removeResponses, ...addResponses].some((response: any) => response.status === 'rejected')
if (hasFailedResponse) {
showToast(translate("Failed to update some role(s)."))
} else {
showToast(translate("Role(s) updated successfully."))
}
// refetching parties with updated roles
await this.store.dispatch('facility/getFacilityParties', { facilityId: this.facilityId })
modalController.dismiss()
},
updateSelectedParties(event: CustomEvent, selectedPartyId: string) {
let party = {} as any
Expand Down
36 changes: 1 addition & 35 deletions src/views/FacilityDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -522,41 +522,7 @@ export default defineComponent({
async addStaffMemberModal() {
const addStaffModal = await modalController.create({
component: AddStaffMemberModal,
componentProps: { selectedParties: this.facilityParties }
})
addStaffModal.onDidDismiss().then(async (result: any) => {
if (result.data && result.data.value) {
const partiesToAdd = result.data.value.partiesToAdd
const partiesToRemove = result.data.value.partiesToRemove
const removeResponses = await Promise.allSettled(partiesToRemove
.map(async (party: any) => await FacilityService.removePartyFromFacility({
facilityId: this.facilityId,
fromDate: party.fromDate,
thruDate: DateTime.now().toMillis(),
partyId: party.partyId,
roleTypeId: party.roleTypeId
}))
)
const addResponses = await Promise.allSettled(partiesToAdd
.map(async (party: any) => await FacilityService.addPartyToFacility({
facilityId: this.facilityId,
partyId: party.partyId,
roleTypeId: party.roleTypeId
}))
)
const hasFailedResponse = [...removeResponses, ...addResponses].some((response: any) => response.status === 'rejected')
if (hasFailedResponse) {
showToast(translate("Failed to update some role(s)."))
} else {
showToast(translate("Role(s) updated successfully."))
}
// refetching parties with updated roles
await this.store.dispatch('facility/getFacilityParties', { facilityId: this.facilityId })
}
componentProps: { facilityId: this.facilityId, selectedParties: this.facilityParties }
})
addStaffModal.present()
Expand Down

0 comments on commit b2bca77

Please sign in to comment.