From c85535e520fe94303da9970618c7f1d575225356 Mon Sep 17 00:00:00 2001 From: Alex Ruzenhack Date: Fri, 2 Feb 2024 01:04:03 +0000 Subject: [PATCH] fix: opt-in modal render time (#424) * fix: push notification opt-in modal render time * chore: improve code readability * lint: comply with rules and improve comment * fix: typo * fix: add delay --- src/components/AskForPushNotification.js | 19 +++++++++++----- src/sagas/pushNotification.js | 28 +++++++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/components/AskForPushNotification.js b/src/components/AskForPushNotification.js index 1b88e7982..f1a468d89 100644 --- a/src/components/AskForPushNotification.js +++ b/src/components/AskForPushNotification.js @@ -13,16 +13,23 @@ export default function AskForPushNotification(props) { dispatch(pushDismissOptInQuestion()); }; - if (!showOptIn) { - return null; - } - return ( + const putDismissOptInQuestion = () => { + dispatch(pushDismissOptInQuestion()); + }; + + const pushNotificationOptInModal = () => ( onEnablePushNotifications()} - onDismiss={() => dispatch(pushDismissOptInQuestion())} + onAction={onEnablePushNotifications} + onDismiss={putDismissOptInQuestion} /> ); + + if (showOptIn) { + return pushNotificationOptInModal(); + } + + return null; } diff --git a/src/sagas/pushNotification.js b/src/sagas/pushNotification.js index 98f6a8518..4c5e9b478 100644 --- a/src/sagas/pushNotification.js +++ b/src/sagas/pushNotification.js @@ -15,6 +15,7 @@ import { take, takeLatest, debounce, + spawn, delay, } from 'redux-saga/effects'; import messaging from '@react-native-firebase/messaging'; @@ -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()); + } + } + }); } /**