Skip to content

Commit

Permalink
Merge pull request #3839 from Emurgo/fix/YOEXT-1704/notifications-set…
Browse files Browse the repository at this point in the history
…ting-for-each-wallet

fix(settings): add notifications setting per wallet
  • Loading branch information
aatsindev authored Feb 4, 2025
2 parents d6a57bb + 87c3530 commit 0cdcc6e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,45 @@ import { ampli } from '../../../../../../ampli';

const NotificationsSettings = ({ intl }) => {
const strings = useStrings(intl);
const [notificationsEnabled, setNotificationsEnabled] = React.useState(true)
const [notificationsEnabled, setNotificationsEnabled] = React.useState(true);
const [selectedWalletId, setSelectedWalletId] = React.useState("");

const lsApi = new LocalStorageApi();

async function getNotificationsSetting(checkCurrentWallet: boolean = false) {
const notifSettingsStr = await lsApi.getNotificationsSetting();
const notifSettings = JSON.parse(notifSettingsStr || "{}");

if (checkCurrentWallet) {
const selectedWalletId = await lsApi.getSelectedWalletId();
setSelectedWalletId(selectedWalletId);

return notifSettings[selectedWalletId] !== undefined ? notifSettings[selectedWalletId] : true;
}

return notifSettings;
}

async function setNotificationsSetting(enabled: boolean) {
const notifSettings = await getNotificationsSetting();
lsApi.setNotificationsSetting(JSON.stringify({ ...notifSettings, [selectedWalletId]: enabled }));
}

// get initial state from localstorage
React.useEffect(() => {
async function getNotifStatus() {
const notifEnabled = await lsApi.getNotificationsSetting();
if (notifEnabled === "true" && !notificationsEnabled) {
setNotificationsEnabled(true);
}
async function initialNotifStatus() {
const notifEnabled = await getNotificationsSetting(true);
setNotificationsEnabled(notifEnabled);
}

getNotifStatus();
initialNotifStatus();
}, [])

const handleNotificationsChange = (event) => {
setNotificationsEnabled(prev => !prev);
lsApi.setNotificationsSetting(String(event.target.checked));

// handle checkbox change event
const handleNotificationsChange = async (event) => {
const enabled = event.target.checked;
setNotificationsEnabled(enabled);
setNotificationsSetting(enabled);
ampli.settingsInAppNotificationsStatusUpdated({
status: event.target.checked ? "enabled" : "disabled"
})
Expand Down
8 changes: 4 additions & 4 deletions packages/yoroi-extension/app/api/localStorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const storageKeys = {
FLAGS: networkForLocalStorage + '-FLAGS',
USER_THEME: networkForLocalStorage + '-USER-THEME',
PORTFOLIO_FIAT_PAIR: networkForLocalStorage + '-PORTFOLIO_FIAT_PAIR',
NOTIFICATIONS_SETTING: networkForLocalStorage + '-NOTIFICATIONS_SETTING',
NOTIFICATIONS_ENABLED: networkForLocalStorage + '-NOTIFICATIONS_ENABLED_PER_WALLET',
BUY_SELL_DISCLAIMER: networkForLocalStorage + '-BUY_SELL_DISCLAIMER',
// ========== CONNECTOR ========== //
DAPP_CONNECTOR_WHITELIST: 'connector_whitelist',
Expand Down Expand Up @@ -109,11 +109,11 @@ export default class LocalStorageApi {

// ========== Notifications Setting ========== //

getNotificationsSetting: void => Promise<?string> = () => getLocalItem(storageKeys.NOTIFICATIONS_SETTING);
getNotificationsSetting: void => Promise<?string> = () => getLocalItem(storageKeys.NOTIFICATIONS_ENABLED);

setNotificationsSetting: string => Promise<void> = allowed => setLocalItem(storageKeys.NOTIFICATIONS_SETTING, allowed);
setNotificationsSetting: string => Promise<void> = allowed => setLocalItem(storageKeys.NOTIFICATIONS_ENABLED, allowed);

unsetNotificationsSetting: void => Promise<void> = () => removeLocalItem(storageKeys.NOTIFICATIONS_SETTING);
unsetNotificationsSetting: void => Promise<void> = () => removeLocalItem(storageKeys.NOTIFICATIONS_ENABLED);

// ========== Buy/Sell Disclaimer ========== //
getBuySellDisclaimer: void => Promise<?string> = () => getLocalItem(storageKeys.BUY_SELL_DISCLAIMER);
Expand Down

0 comments on commit 0cdcc6e

Please sign in to comment.