Skip to content

Commit

Permalink
Merge pull request #424 from HathorNetwork/fix/opt-in-modal
Browse files Browse the repository at this point in the history
fix: opt-in modal render time
  • Loading branch information
alexruzenhack authored Feb 13, 2024
2 parents cb5655b + be8f712 commit 445164e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/components/AskForPushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ export default function AskForPushNotification(props) {
dispatch(pushDismissOptInQuestion());
};

if (!showOptIn) {
return null;
}
return (
const putDismissOptInQuestion = () => {
dispatch(pushDismissOptInQuestion());
};

const pushNotificationOptInModal = () => (
<ActionModal
title={t`Do you want to enable push notifications for this wallet?`}
text={t`You can always change this later in the settings menu`}
button={t`Yes, enable`}
onAction={() => onEnablePushNotifications()}
onDismiss={() => dispatch(pushDismissOptInQuestion())}
onAction={onEnablePushNotifications}
onDismiss={putDismissOptInQuestion}
/>
);

if (showOptIn) {
return pushNotificationOptInModal();
}

return null;
}
28 changes: 22 additions & 6 deletions src/sagas/pushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
take,
takeLatest,
debounce,
spawn,
delay,
} from 'redux-saga/effects';
import messaging from '@react-native-firebase/messaging';
Expand Down Expand Up @@ -315,12 +316,27 @@ export function* init() {
}
}

// If the user has not been asked yet, we should ask him if he wants to enable push notifications
// We should appear only once, so we should save the fact that the user has dismissed the question
const optInDismissed = STORE.getItem(pushNotificationKey.optInDismissed);
if (optInDismissed === null || !optInDismissed) {
yield put(pushAskOptInQuestion());
}
// Gives users the option to opt-in the Push Notification
// after its initialization, but only opens the opt-in
// modal after wallet become ready.
// Spwan creates a detached thread from this current thread.
yield spawn(function* handleOptIn() {
const { ready } = yield race({
ready: take(types.WALLET_STATE_READY),
// Do nothing with error, only terminates the thread.
error: take(types.WALLET_STATE_ERROR),
});

if (ready) {
// Ask user for push notification opt-in if it has not
// been asked previously. It appears only once because
// we persist the dismiss action on store.
const optInDismissed = STORE.getItem(pushNotificationKey.optInDismissed);
if (optInDismissed === null || !optInDismissed) {
yield put(pushAskOptInQuestion());
}
}
});
}

/**
Expand Down

0 comments on commit 445164e

Please sign in to comment.