Skip to content

Commit

Permalink
use typeguard for action matchers (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
atn4z7 authored Jan 9, 2025
1 parent d163a38 commit 026b399
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions packages/core-mobile/app/store/notifications/listeners/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,68 +192,84 @@ export const addNotificationsListeners = (
}

const onNotificationsTurnedOnForBalanceChange = {
match: (action: Action<unknown>): action is PayloadAction => {
return (
action.type === turnOnNotificationsFor.type &&
(action as PayloadAction<{ channelId: ChannelId }>).payload.channelId ===
ChannelId.BALANCE_CHANGES
)
match: (
action: Action<unknown>
): action is PayloadAction<{ channelId: ChannelId }> => {
if (!turnOnNotificationsFor.match(action)) {
return false
}

return action.payload.channelId === ChannelId.BALANCE_CHANGES
}
}

const onNotificationsTurnedOnForNews = {
match: (action: Action<unknown>): action is PayloadAction => {
const channelId = (action as PayloadAction<{ channelId: ChannelId }>)
.payload.channelId
match: (
action: Action<unknown>
): action is PayloadAction<{ channelId: ChannelId }> => {
if (!turnOnNotificationsFor.match(action)) {
return false
}

const channelId = action.payload.channelId

return (
action.type === turnOnNotificationsFor.type &&
(channelId === ChannelId.MARKET_NEWS ||
channelId === ChannelId.OFFERS_AND_PROMOTIONS ||
channelId === ChannelId.PRODUCT_ANNOUNCEMENTS ||
channelId === ChannelId.PRICE_ALERTS)
channelId === ChannelId.MARKET_NEWS ||
channelId === ChannelId.OFFERS_AND_PROMOTIONS ||
channelId === ChannelId.PRODUCT_ANNOUNCEMENTS ||
channelId === ChannelId.PRICE_ALERTS
)
}
}

const onNotificationsTurnedOffForBalanceChange = {
match: (action: Action<unknown>): action is PayloadAction => {
return (
action.type === turnOffNotificationsFor.type &&
(action as PayloadAction<{ channelId: ChannelId }>).payload.channelId ===
ChannelId.BALANCE_CHANGES
)
match: (
action: Action<unknown>
): action is PayloadAction<{ channelId: ChannelId }> => {
if (!turnOffNotificationsFor.match(action)) {
return false
}

return action.payload.channelId === ChannelId.BALANCE_CHANGES
}
}

const onNotificationsTurnedOffForNews = {
match: (action: Action<unknown>): action is PayloadAction => {
const channelId = (action as PayloadAction<{ channelId: ChannelId }>)
.payload.channelId
match: (
action: Action<unknown>
): action is PayloadAction<{ channelId: ChannelId }> => {
if (!turnOffNotificationsFor.match(action)) {
return false
}

const channelId = action.payload.channelId

return (
action.type === turnOffNotificationsFor.type &&
(channelId === ChannelId.MARKET_NEWS ||
channelId === ChannelId.OFFERS_AND_PROMOTIONS ||
channelId === ChannelId.PRODUCT_ANNOUNCEMENTS ||
channelId === ChannelId.PRICE_ALERTS)
channelId === ChannelId.MARKET_NEWS ||
channelId === ChannelId.OFFERS_AND_PROMOTIONS ||
channelId === ChannelId.PRODUCT_ANNOUNCEMENTS ||
channelId === ChannelId.PRICE_ALERTS
)
}
}

const onNotificationsEnabled = {
match: (action: Action<unknown>): action is PayloadAction => {
if (action.type === setFeatureFlags.type) {
const setFeatureFlagsAction = action as PayloadAction<FeatureFlags>
match: (action: Action<unknown>): action is PayloadAction<FeatureFlags> => {
if (setFeatureFlags.match(action)) {
const setFeatureFlagsAction = action
return !!setFeatureFlagsAction.payload[FeatureGates.ALL_NOTIFICATIONS]
}

return false
}
}
const onNotificationsDisabled = {
match: (action: Action<unknown>): action is PayloadAction => {
if (action.type === setFeatureFlags.type) {
const setFeatureFlagsAction = action as PayloadAction<FeatureFlags>
match: (action: Action<unknown>): action is PayloadAction<FeatureFlags> => {
if (setFeatureFlags.match(action)) {
const setFeatureFlagsAction = action
return !setFeatureFlagsAction.payload[FeatureGates.ALL_NOTIFICATIONS]
}

return false
}
}

0 comments on commit 026b399

Please sign in to comment.