Skip to content

Commit

Permalink
Update group settings and subscriptions function
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Nov 29, 2023
1 parent 6cc5892 commit c9cf8b5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
25 changes: 11 additions & 14 deletions src/notifications/settings/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,23 @@ export const useWalletGroupNotificationSettings = () => {
const options: GroupSettings = {
[type]: enabled,
};

const newSettings: GroupSettings = {
...existingGroupSettings,
...options,
};
const newOwnerEnabled = newSettings[NotificationRelationship.OWNER];
const newWatcherEnabled = newSettings[NotificationRelationship.WATCHER];

const updateStore = () => {
updateGroupSettings(newSettings);
};
const walletsToUpdate =
type === NotificationRelationship.OWNER ? ownedWallets : watchedWallets;

if (newOwnerEnabled !== ownerEnabled) {
return toggleGroupNotifications(ownedWallets, newOwnerEnabled).then(
updateStore
);
} else if (newWatcherEnabled !== watcherEnabled) {
return toggleGroupNotifications(watchedWallets, newWatcherEnabled).then(
updateStore
);
const isSuccess = await toggleGroupNotifications(
walletsToUpdate,
enabled
);
if (isSuccess) {
updateGroupSettings(newSettings);
}
return Promise.resolve();
return isSuccess;
},
[
existingGroupSettings,
Expand All @@ -145,6 +141,7 @@ export const useWalletGroupNotificationSettings = () => {
() => ownerEnabled && !allOwnedWalletsDisabled,
[allOwnedWalletsDisabled, ownerEnabled]
);

const isWatcherEnabled = useMemo(
() => watcherEnabled && !allWatchedWalletsDisabled,
[allWatchedWalletsDisabled, watcherEnabled]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,42 +260,40 @@ const NotificationsSection = () => {
const neverGranted = permissionStatus === RESULTS.DENIED;
const disabledInSystem = permissionStatus === RESULTS.BLOCKED;

const toggleAllOwnedNotifications = useCallback(() => {
const toggleAllOwnedNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setOwnedState(prev => ({ status: !prev.status, loading: true }));
updateGroupSettingsAndSubscriptions(
const isSuccess = await updateGroupSettingsAndSubscriptions(
NotificationRelationship.OWNER,
!storedOwnerEnabled
)
.then(() => {
setOwnedState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setOwnedState(prev => ({ status: !prev.status, loading: false }));
});
);
if (isSuccess) {
setOwnedState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setOwnedState(prev => ({ status: !prev.status, loading: false }));
}
}, [storedOwnerEnabled, updateGroupSettingsAndSubscriptions, isConnected]);

const toggleAllWatchedNotifications = useCallback(() => {
const toggleAllWatchedNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setWatchedState(prev => ({ status: !prev.status, loading: true }));
updateGroupSettingsAndSubscriptions(
const isSuccess = await updateGroupSettingsAndSubscriptions(
NotificationRelationship.WATCHER,
!storedWatcherEnabled
)
.then(() => {
setWatchedState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setWatchedState(prev => ({ status: !prev.status, loading: false }));
});
);
if (isSuccess) {
setWatchedState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setWatchedState(prev => ({ status: !prev.status, loading: false }));
}
}, [updateGroupSettingsAndSubscriptions, storedWatcherEnabled, isConnected]);

const openSystemSettings = Linking.openSettings;
Expand Down
38 changes: 18 additions & 20 deletions src/screens/SettingsSheet/components/NotificationsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,42 +259,40 @@ const NotificationsSection = () => {
const neverGranted = permissionStatus === RESULTS.DENIED;
const disabledInSystem = permissionStatus === RESULTS.BLOCKED;

const toggleAllOwnedNotifications = useCallback(() => {
const toggleAllOwnedNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setOwnedState(prev => ({ status: !prev.status, loading: true }));
updateGroupSettingsAndSubscriptions(
const isSuccess = await updateGroupSettingsAndSubscriptions(
NotificationRelationship.OWNER,
!storedOwnerEnabled
)
.then(() => {
setOwnedState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setOwnedState(prev => ({ status: !prev.status, loading: false }));
});
);
if (isSuccess) {
setOwnedState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setOwnedState(prev => ({ status: !prev.status, loading: false }));
}
}, [storedOwnerEnabled, updateGroupSettingsAndSubscriptions, isConnected]);

const toggleAllWatchedNotifications = useCallback(() => {
const toggleAllWatchedNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setWatchedState(prev => ({ status: !prev.status, loading: true }));
updateGroupSettingsAndSubscriptions(
const isSuccess = await updateGroupSettingsAndSubscriptions(
NotificationRelationship.WATCHER,
!storedWatcherEnabled
)
.then(() => {
setWatchedState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setWatchedState(prev => ({ status: !prev.status, loading: false }));
});
);
if (isSuccess) {
setWatchedState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setWatchedState(prev => ({ status: !prev.status, loading: false }));
}
}, [updateGroupSettingsAndSubscriptions, storedWatcherEnabled, isConnected]);

const openSystemSettings = Linking.openSettings;
Expand Down

0 comments on commit c9cf8b5

Please sign in to comment.