Skip to content

Commit ad7bd51

Browse files
committed
rework notifications/hydrate action creator
- split into two actions: set_channels and set_permissions - remove promise functions and use callbacks instead, for clearer flow types - split apart tags/permission requests, to allow them to work properly when tags never respond
1 parent 7852517 commit ad7bd51

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

source/redux/parts/notifications.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
// @flow
22

3-
import {} from '../../lib/storage'
4-
53
import {
64
trackChannelSubscribe,
75
trackChannelUnsubscribe,
86
trackNotificationsDisable,
97
trackNotificationsEnable,
108
} from '@frogpond/analytics'
11-
import OneSignal, {
12-
type SubscriptionState,
13-
type TagsObject,
14-
} from 'react-native-onesignal'
15-
import pify from 'pify'
16-
17-
const getOneSignalTags: () => Promise<?TagsObject> = pify(OneSignal.getTags, {
18-
errorFirst: false,
19-
})
20-
const getOneSignalPermissions: () => Promise<SubscriptionState> = pify(
21-
OneSignal.getPermissionSubscriptionState,
22-
{errorFirst: false},
23-
)
24-
const promptOneSignalPushPermission: () => Promise<boolean> = pify(
25-
OneSignal.promptForPushNotificationsWithUserResponse,
26-
{errorFirst: false},
27-
)
9+
import OneSignal from 'react-native-onesignal'
2810

2911
import {type ReduxState} from '../index'
3012

@@ -42,7 +24,8 @@ export type NotificationChannelName =
4224

4325
const CHANNEL_SUBSCRIBE = 'notifications/CHANNEL_SUBSCRIBE'
4426
const CHANNEL_UNSUBSCRIBE = 'notifications/CHANNEL_UNSUBSCRIBE'
45-
const CHANNELS_HYDRATE = 'notifications/CHANNELS_HYDRATE'
27+
const SET_CHANNELS = 'notifications/SET_CHANNELS'
28+
const SET_PERMISSIONS = 'notifications/SET_PERMISSIONS'
4629
const DISABLE = 'notifications/DISABLE'
4730
const ENABLE = 'notifications/ENABLE'
4831

@@ -87,24 +70,36 @@ export function toggleSubscription(
8770
}
8871
}
8972

90-
type HydrateChannelsAction = {|
91-
type: 'notifications/CHANNELS_HYDRATE',
92-
payload: {
93-
channels: Set<NotificationChannelName>,
94-
permissions: SubscriptionState,
95-
},
73+
type SetPermissionsAction = {|
74+
type: 'notifications/SET_PERMISSIONS',
75+
payload: {enabled: boolean, hasPrompted: boolean},
9676
|}
97-
export function hydrate(): ThunkAction<HydrateChannelsAction> {
98-
return async dispatch => {
99-
let [tags, permissions] = await Promise.all([
100-
getOneSignalTags(),
101-
getOneSignalPermissions(),
102-
])
103-
let channels = new Set()
104-
if (tags) {
105-
channels = new Set(Object.keys(tags))
106-
}
107-
dispatch({type: CHANNELS_HYDRATE, payload: {channels, permissions}})
77+
type SetChannelsAction = {|
78+
type: 'notifications/SET_CHANNELS',
79+
payload: Set<NotificationChannelName>,
80+
|}
81+
export function hydrate(): ThunkAction<SetPermissionsAction | SetChannelsAction> {
82+
return dispatch => {
83+
OneSignal.getPermissionSubscriptionState(permissions => {
84+
dispatch({type: SET_PERMISSIONS, payload: {
85+
enabled: permissions.notificationsEnabled,
86+
hasPrompted: permissions.hasPrompted,
87+
}})
88+
})
89+
90+
OneSignal.getTags(tags => {
91+
if (!tags) {
92+
return;
93+
}
94+
95+
if (tags.stack && tags.message) {
96+
// it's an error
97+
return;
98+
}
99+
100+
let channels: Set<any> = new Set(Object.keys(tags))
101+
dispatch({type: SET_CHANNELS, payload: channels})
102+
})
108103
}
109104
}
110105

@@ -129,16 +124,18 @@ function enable(): EnableNotificationsAction {
129124
export function prompt(): ThunkAction<
130125
EnableNotificationsAction | DisableNotificationsAction,
131126
> {
132-
return async dispatch => {
133-
const permissionResult = await promptOneSignalPushPermission()
134-
dispatch(permissionResult ? enable() : disable())
127+
return dispatch => {
128+
OneSignal.promptForPushNotificationsWithUserResponse(result => {
129+
dispatch(result ? enable() : disable())
130+
})
135131
}
136132
}
137133

138134
type Action =
139135
| ChannelSubscribeAction
140136
| ChannelUnsubscribeAction
141-
| HydrateChannelsAction
137+
| SetPermissionsAction
138+
| SetChannelsAction
142139
| EnableNotificationsAction
143140
| DisableNotificationsAction
144141

@@ -168,12 +165,15 @@ export function notifications(state: State = initialState, action: Action) {
168165
return {...state, channels}
169166
}
170167

171-
case CHANNELS_HYDRATE:
168+
case SET_CHANNELS: {
169+
return {...state, channels: action.payload}
170+
}
171+
172+
case SET_PERMISSIONS:
172173
return {
173174
...state,
174-
channels: action.payload.channels,
175-
enabled: action.payload.permissions.notificationsEnabled,
176-
hasPrompted: action.payload.permissions.hasPrompted,
175+
enabled: action.payload.enabled,
176+
hasPrompted: action.payload.hasPrompted,
177177
}
178178

179179
case ENABLE:

0 commit comments

Comments
 (0)