@@ -7,7 +7,7 @@ import type {
77 NotificationDataType ,
88 NotificationsApiResponse ,
99 SirenErrorType ,
10- UnviewedCountApiResponse ,
10+ UnviewedCountApiResponse
1111} from '@sirenapp/js-sdk/dist/esm/types' ;
1212
1313import type { SirenProviderConfigProps } from '../types' ;
@@ -17,7 +17,8 @@ import {
1717 eventTypes ,
1818 IN_APP_RECIPIENT_UNAUTHENTICATED ,
1919 MAXIMUM_RETRY_COUNT ,
20- VerificationStatus
20+ VerificationStatus ,
21+ EventType
2122} from '../utils/constants' ;
2223import { useSiren } from '../utils' ;
2324
@@ -74,10 +75,11 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
7475 let retryCount = 0 ;
7576
7677 const { markAllAsViewed } = useSiren ( ) ;
77-
78+
7879 const [ siren , setSiren ] = useState < Siren | null > ( null ) ;
79- const [ verificationStatus , setVerificationStatus ] = useState < VerificationStatus > ( VerificationStatus . PENDING ) ;
80-
80+ const [ verificationStatus , setVerificationStatus ] = useState < VerificationStatus > (
81+ VerificationStatus . PENDING
82+ ) ;
8183
8284 useEffect ( ( ) => {
8385 if ( config ?. recipientId && config ?. userToken ) {
@@ -90,8 +92,8 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
9092 } , [ config ] ) ;
9193
9294 const stopRealTimeFetch = ( ) : void => {
93- siren ?. stopRealTimeNotificationFetch ( ) ;
94- siren ?. stopRealTimeUnviewedCountFetch ( ) ;
95+ siren ?. stopRealTimeFetch ( EventType . NOTIFICATION ) ;
96+ siren ?. stopRealTimeFetch ( EventType . UNVIEWED_COUNT ) ;
9597 } ;
9698
9799 const sendResetDataEvents = ( ) => {
@@ -106,9 +108,17 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
106108 PubSub . publish ( events . NOTIFICATION_LIST_EVENT , JSON . stringify ( updateNotificationPayload ) ) ;
107109 } ;
108110
109- const onUnViewedCountReceived = ( response : UnviewedCountApiResponse ) : void => {
110- const totalUnviewed = response ?. data ?. totalUnviewed ;
111+ const onNewNotificationEvent = ( responseData : NotificationDataType [ ] ) => {
112+ logger . info ( `new notifications : ${ JSON . stringify ( responseData ) } ` ) ;
113+
114+ markAllAsViewed ( responseData [ 0 ] . createdAt ) ;
115+ const payload = { newNotifications : responseData , action : eventTypes . NEW_NOTIFICATIONS } ;
116+
117+ PubSub . publish ( events . NOTIFICATION_LIST_EVENT , JSON . stringify ( payload ) ) ;
118+ } ;
111119
120+ const onTotalUnviewedCountEvent = ( response : UnviewedCountApiResponse ) => {
121+ const totalUnviewed = response . data ?. totalUnviewed ;
112122 const payload = {
113123 unviewedCount : totalUnviewed ,
114124 action : eventTypes . UPDATE_NOTIFICATIONS_COUNT
@@ -117,25 +127,38 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
117127 PubSub . publish ( events . NOTIFICATION_COUNT_EVENT , JSON . stringify ( payload ) ) ;
118128 } ;
119129
120- const onNotificationReceived = ( response : NotificationsApiResponse ) : void => {
121- const responseData : NotificationDataType [ ] = response ?. data || [ ] ;
130+ const handleNotificationEvent = ( response : NotificationsApiResponse ) => {
131+ const responseData = response ?. data ;
122132
123- if ( isNonEmptyArray ( responseData ) ) {
124- logger . info ( `new notifications : ${ JSON . stringify ( responseData ) } ` ) ;
125-
126- markAllAsViewed ( responseData [ 0 ] . createdAt ) ;
127- const payload = { newNotifications : response ?. data , action : eventTypes . NEW_NOTIFICATIONS } ;
133+ if ( Array . isArray ( responseData ) && isNonEmptyArray ( responseData ) )
134+ onNewNotificationEvent ( responseData ) ;
128135
129- PubSub . publish ( events . NOTIFICATION_LIST_EVENT , JSON . stringify ( payload ) ) ;
130- }
131136 } ;
137+ const handleUnviewedCountEvent = ( response : UnviewedCountApiResponse ) => {
138+ const responseData = response ?. data ;
132139
140+ if ( responseData && 'totalUnviewed' in responseData )
141+ onTotalUnviewedCountEvent ( response ) ;
142+
143+ } ;
144+ const onEventReceive = (
145+ response : NotificationsApiResponse | UnviewedCountApiResponse = { } ,
146+ eventType : EventType
147+ ) => {
148+ switch ( eventType ) {
149+ case EventType . NOTIFICATION :
150+ handleNotificationEvent ( response as NotificationsApiResponse ) ;
151+ break ;
152+ case EventType . UNVIEWED_COUNT :
153+ handleUnviewedCountEvent ( response as UnviewedCountApiResponse ) ;
154+ break ;
155+ }
156+ } ;
133157 const onStatusChange = ( status : VerificationStatus ) => {
134158 setVerificationStatus ( status ) ;
135159 } ;
136160
137-
138- const actionCallbacks = { onUnViewedCountReceived, onNotificationReceived, onStatusChange } ;
161+ const actionCallbacks = { onEventReceive, onStatusChange } ;
139162
140163 const getDataParams = ( ) => {
141164 return {
0 commit comments