Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: changing the state of notification preference toggle only if api success (#405) #406

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ const actions: ActionTree<UserState, RootState> = {
}
},

async updateNotificationPreferences({ commit }, payload) {
commit(types.USER_NOTIFICATIONS_PREFERENCES_UPDATED, payload)
},

async storeClientRegistrationToken({ commit }, registrationToken) {
const firebaseDeviceId = generateDeviceId()
commit(types.USER_FIREBASE_DEVICEID_UPDATED, firebaseDeviceId)
Expand Down
36 changes: 16 additions & 20 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
</ion-card-content>
<ion-list>
<ion-item :key="pref.enumId" v-for="pref in notificationPrefs" lines="none">
<ion-toggle label-placement="start" @click="confirmNotificationPrefUpdate(pref.enumId, $event)" :checked="pref.isEnabled">{{ pref.description }}</ion-toggle>
<ion-toggle label-placement="start" @click.prevent="confirmNotificationPrefUpdate(pref.enumId, $event)" :checked="pref.isEnabled">{{ pref.description }}</ion-toggle>
</ion-item>
</ion-list>
</ion-card>
Expand Down Expand Up @@ -416,48 +416,44 @@ export default defineComponent({
}
await this.store.dispatch('user/updatePartialOrderRejectionConfig', params)
},
async updateNotificationPref(enumId: string, event: any) {
async updateNotificationPref(enumId: string) {
try {
emitter.emit('presentLoader', { backdropDismiss: false })
const facilityId = (this.currentFacility as any).facilityId
const topicName = generateTopicName(facilityId, enumId)
// event.target.checked returns the initial value (the value that was there before clicking
// and updating the toggle). But it returns the updated value on further references (if passed
// as a parameter in other function, here in our case, passed from confirmNotificationPrefUpdate)
// Hence, event.target.checked here holds the updated value (value after the toggle action)
event.target.checked
ymaheshwari1 marked this conversation as resolved.
Show resolved Hide resolved
? await subscribeTopic(topicName, process.env.VUE_APP_NOTIF_APP_ID)
: await unsubscribeTopic(topicName, process.env.VUE_APP_NOTIF_APP_ID)

const notificationPref = this.notificationPrefs.find((pref: any) => pref.enumId === enumId)
notificationPref.isEnabled
? await unsubscribeTopic(topicName, process.env.VUE_APP_NOTIF_APP_ID)
: await subscribeTopic(topicName, process.env.VUE_APP_NOTIF_APP_ID)

notificationPref.isEnabled = !notificationPref.isEnabled
await this.store.dispatch('user/updateNotificationPreferences', this.notificationPrefs)
showToast(translate('Notification preferences updated.'))
} catch (error) {
// reverting the value of toggle as event.target.checked is
// updated on click event, and revert is needed on API fail
event.target.checked = !event.target.checked;
showToast(translate('Notification preferences not updated. Please try again.'))
} finally {
emitter.emit("dismissLoader")
}
},
async confirmNotificationPrefUpdate(enumId: string, event: any) {
async confirmNotificationPrefUpdate(enumId: string, event: CustomEvent) {
event.stopImmediatePropagation();

const message = translate("Are you sure you want to update the notification preferences?");
const alert = await alertController.create({
header: translate("Update notification preferences"),
message,
buttons: [
{
text: translate("Cancel"),
handler: () => {
// reverting the value of toggle as event.target.checked is
// updated on click event and revert is needed on "Cancel"
event.target.checked = !event.target.checked
}
role: "cancel"
},
{
text: translate("Confirm"),
handler: async () => {
// passing event reference for updation in case the API fails
// passing event reference for updation in case the API success
alertController.dismiss()
await this.updateNotificationPref(enumId, event)
await this.updateNotificationPref(enumId)
}
}
],
Expand Down
Loading