Skip to content

Commit

Permalink
Update WalletNotificationsSettings to use new toggle logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Nov 29, 2023
1 parent c9cf8b5 commit 93de206
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
showOfflineAlert,
} from '@/screens/SettingsSheet/components/notificationAlerts';
import { useNetInfo } from '@react-native-community/netinfo';
import { updateSettingsForWalletWithAddress } from '@/notifications/settings/storage';
import { BackgroundProvider, Box, Inline, Inset, Text } from '@/design-system';
import { SimpleSheet } from '@/components/sheet/SimpleSheet';
import { useWallets } from '@/hooks';
Expand Down Expand Up @@ -116,18 +115,16 @@ const WalletNotificationsSettings = () => {
notifications,
setNotificationSettings,
] = useState<WalletNotificationSettings>(notificationSettings);

const updateSettings = useCallback(
(options: Partial<WalletNotificationSettings>) => {
const newSettingsForWallet = updateSettingsForWalletWithAddress(
address,
options
);

if (newSettingsForWallet) {
setNotificationSettings(newSettingsForWallet);
}
const newSettingsForWallet = {
...notifications,
...options,
};
setNotificationSettings(newSettingsForWallet);
},
[address]
[address, notifications]
);

const {
Expand Down Expand Up @@ -181,31 +178,33 @@ const WalletNotificationsSettings = () => {
setTopicSubscriptionInProgress,
] = useState<NotificationTopicType | null>(null);

const toggleAllowNotifications = useCallback(() => {
const toggleAllowNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setAllState(prev => ({ status: !prev.status, loading: true }));
toggleGroupNotifications([notifications], !notificationsEnabled)
.then(() => {
if (
!notificationsSectionEnabled ||
(notificationsSectionEnabled && lastWalletEnabled)
) {
updateGroupSettings({
[notifications.type]: !notificationsEnabled,
});
}
updateSettings({
enabled: !notificationsEnabled,
const success = await toggleGroupNotifications(
[notifications],
!notificationsEnabled
);
if (success) {
if (
!notificationsSectionEnabled ||
(notificationsSectionEnabled && lastWalletEnabled)
) {
updateGroupSettings({
[notifications.type]: !notificationsEnabled,
});
setAllState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setAllState(prev => ({ status: !prev.status, loading: false }));
}
updateSettings({
enabled: !notificationsEnabled,
});
setAllState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setAllState(prev => ({ status: !prev.status, loading: false }));
}
}, [
notificationsSectionEnabled,
lastWalletEnabled,
Expand All @@ -216,33 +215,30 @@ const WalletNotificationsSettings = () => {
]);

const toggleTopic = useCallback(
(topic: NotificationTopicType) => {
async (topic: NotificationTopicType) => {
if (!isConnected) {
showOfflineAlert();
return;
}
toggleStateForTopic(topic);
setTopicSubscriptionInProgress(topic);
toggleTopicForWallet(
const success = await toggleTopicForWallet(
notifications.address,
topic,
!notifications?.topics[topic]
)
.then(() => {
updateSettings({
topics: {
...notifications.topics,
[topic]: !notifications?.topics[topic],
},
});
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
toggleStateForTopic(topic);
})
.finally(() => {
setTopicSubscriptionInProgress(null);
);
if (success) {
updateSettings({
topics: {
...notifications.topics,
[topic]: !notifications?.topics[topic],
},
});
} else {
showNotificationSubscriptionErrorAlert();
toggleStateForTopic(topic);
}
setTopicSubscriptionInProgress(null);
},
[notifications, updateSettings, isConnected]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
showOfflineAlert,
} from '@/screens/SettingsSheet/components/notificationAlerts';
import { useNetInfo } from '@react-native-community/netinfo';
import { updateSettingsForWalletWithAddress } from '@/notifications/settings/storage';

const makeTopicRowsData = (colors: ThemeContextProps['colors']) => [
{
Expand Down Expand Up @@ -103,18 +102,16 @@ const WalletNotificationsSettings = () => {
notifications,
setNotificationSettings,
] = useState<WalletNotificationSettings>(notificationSettings);

const updateSettings = useCallback(
(options: Partial<WalletNotificationSettings>) => {
const newSettingsForWallet = updateSettingsForWalletWithAddress(
address,
options
);

if (newSettingsForWallet) {
setNotificationSettings(newSettingsForWallet);
}
const newSettingsForWallet = {
...notifications,
...options,
};
setNotificationSettings(newSettingsForWallet);
},
[address]
[address, notifications]
);

const {
Expand Down Expand Up @@ -168,31 +165,33 @@ const WalletNotificationsSettings = () => {
setTopicSubscriptionInProgress,
] = useState<NotificationTopicType | null>(null);

const toggleAllowNotifications = useCallback(() => {
const toggleAllowNotifications = useCallback(async () => {
if (!isConnected) {
showOfflineAlert();
return;
}
setAllState(prev => ({ status: !prev.status, loading: true }));
toggleGroupNotifications([notifications], !notificationsEnabled)
.then(() => {
if (
!notificationsSectionEnabled ||
(notificationsSectionEnabled && lastWalletEnabled)
) {
updateGroupSettings({
[notifications.type]: !notificationsEnabled,
});
}
updateSettings({
enabled: !notificationsEnabled,
const success = await toggleGroupNotifications(
[notifications],
!notificationsEnabled
);
if (success) {
if (
!notificationsSectionEnabled ||
(notificationsSectionEnabled && lastWalletEnabled)
) {
updateGroupSettings({
[notifications.type]: !notificationsEnabled,
});
setAllState(prev => ({ ...prev, loading: false }));
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
setAllState(prev => ({ status: !prev.status, loading: false }));
}
updateSettings({
enabled: !notificationsEnabled,
});
setAllState(prev => ({ ...prev, loading: false }));
} else {
showNotificationSubscriptionErrorAlert();
setAllState(prev => ({ status: !prev.status, loading: false }));
}
}, [
notificationsSectionEnabled,
lastWalletEnabled,
Expand All @@ -203,33 +202,30 @@ const WalletNotificationsSettings = () => {
]);

const toggleTopic = useCallback(
(topic: NotificationTopicType) => {
async (topic: NotificationTopicType) => {
if (!isConnected) {
showOfflineAlert();
return;
}
toggleStateForTopic(topic);
setTopicSubscriptionInProgress(topic);
toggleTopicForWallet(
const success = await toggleTopicForWallet(
notifications.address,
topic,
!notifications?.topics[topic]
)
.then(() => {
updateSettings({
topics: {
...notifications.topics,
[topic]: !notifications?.topics[topic],
},
});
})
.catch(() => {
showNotificationSubscriptionErrorAlert();
toggleStateForTopic(topic);
})
.finally(() => {
setTopicSubscriptionInProgress(null);
);
if (success) {
updateSettings({
topics: {
...notifications.topics,
[topic]: !notifications?.topics[topic],
},
});
} else {
showNotificationSubscriptionErrorAlert();
toggleStateForTopic(topic);
}
setTopicSubscriptionInProgress(null);
},
[notifications, updateSettings, isConnected]
);
Expand Down

0 comments on commit 93de206

Please sign in to comment.