From d832799d826d7de42118fd763b2b70015518244b Mon Sep 17 00:00:00 2001 From: Abhay Date: Thu, 20 Mar 2025 15:13:58 +0530 Subject: [PATCH 1/2] Fix: Remove verification status step during initialization --- package-lock.json | 14 ++++---- package.json | 2 +- src/components/sirenInbox.tsx | 24 +++++-------- src/components/sirenInboxIcon.tsx | 21 +++-------- src/components/sirenProvider.tsx | 36 +++++-------------- src/utils/sirenHook.ts | 4 +-- .../components/sirenNotificationIcon.test.tsx | 4 +-- tests/utils/sirenHook.test.ts | 12 +++---- 8 files changed, 38 insertions(+), 79 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7672959..a5fd5ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.1.0", "license": "MIT", "dependencies": { - "@sirenapp/js-sdk": "^1.1.0", + "@sirenapp/js-sdk": "^1.2.3", "pubsub-js": "^1.9.4" }, "devDependencies": { @@ -4827,9 +4827,9 @@ } }, "node_modules/@sirenapp/js-sdk": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@sirenapp/js-sdk/-/js-sdk-1.1.0.tgz", - "integrity": "sha512-4V6nltHLhPLOsPMQO7OqllUeFEELvgeb8RSPb/uuwZ1qr05pClQEgoCy5/AlX0glsfHCdoQUfAyWt3akJEenrw==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@sirenapp/js-sdk/-/js-sdk-1.2.3.tgz", + "integrity": "sha512-tqVEscSTRcNZ1c06n9i28NfQGZbLQm443z852cpZ8ybwh1uv8uFTN1dFXzP7QXB/EHi1qB4O1ogBsTvCucYwNQ==", "dependencies": { "promise-polyfill": "^8.3.0", "tslib": "^2.6.2", @@ -19030,9 +19030,9 @@ } }, "@sirenapp/js-sdk": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@sirenapp/js-sdk/-/js-sdk-1.1.0.tgz", - "integrity": "sha512-4V6nltHLhPLOsPMQO7OqllUeFEELvgeb8RSPb/uuwZ1qr05pClQEgoCy5/AlX0glsfHCdoQUfAyWt3akJEenrw==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@sirenapp/js-sdk/-/js-sdk-1.2.3.tgz", + "integrity": "sha512-tqVEscSTRcNZ1c06n9i28NfQGZbLQm443z852cpZ8ybwh1uv8uFTN1dFXzP7QXB/EHi1qB4O1ogBsTvCucYwNQ==", "requires": { "promise-polyfill": "^8.3.0", "tslib": "^2.6.2", diff --git a/package.json b/package.json index 2f62dee..835fc0c 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,6 @@ ], "dependencies": { "pubsub-js": "^1.9.4", - "@sirenapp/js-sdk": "^1.1.0" + "@sirenapp/js-sdk": "^1.2.3" } } diff --git a/src/components/sirenInbox.tsx b/src/components/sirenInbox.tsx index ecea810..e46c108 100644 --- a/src/components/sirenInbox.tsx +++ b/src/components/sirenInbox.tsx @@ -21,9 +21,7 @@ const { events, TOKEN_VERIFICATION_PENDING, MAXIMUM_ITEMS_PER_FETCH, - VerificationStatus, - EventType, - errorMap + EventType } = Constants; const { applyTheme, isNonEmptyArray, updateNotifications } = CommonUtils; @@ -105,7 +103,7 @@ const SirenInbox = (props: SirenInboxProps): ReactElement => { itemsPerFetch > MAXIMUM_ITEMS_PER_FETCH ? MAXIMUM_ITEMS_PER_FETCH : itemsPerFetch ); - const { siren, verificationStatus, id } = useSirenContext(); + const { siren, id } = useSirenContext(); const { deleteById, deleteByDate, markAllAsViewed } = useSiren(); @@ -130,15 +128,10 @@ const SirenInbox = (props: SirenInboxProps): ReactElement => { useEffect(() => { // Initialize Siren SDK and start polling notifications - if (verificationStatus === VerificationStatus.SUCCESS && siren) { + if (siren) initialize(); - } else if(verificationStatus === VerificationStatus.FAILED) { - setIsError(true); - setIsLoading(false); - setNotifications([]); - if (onError) onError(errorMap.INVALID_CREDENTIALS); - } - }, [siren, verificationStatus]); + + }, [siren]); useEffect(() => { if (eventListenerData) { @@ -193,8 +186,7 @@ const SirenInbox = (props: SirenInboxProps): ReactElement => { if (isNonEmptyArray(allNotifications)) notificationParams.start = allNotifications[0].createdAt; - if (verificationStatus === VerificationStatus.SUCCESS) - siren?.startRealTimeFetch({eventType: EventType.NOTIFICATION, params: notificationParams}); + siren?.startRealTimeFetch({eventType: EventType.NOTIFICATION, params: notificationParams}); } }; @@ -283,8 +275,8 @@ const SirenInbox = (props: SirenInboxProps): ReactElement => { if (isNonEmptyArray(allNotifications)) notificationParams.start = allNotifications[0].createdAt; - if (verificationStatus === VerificationStatus.SUCCESS) - siren?.startRealTimeFetch({eventType: EventType.NOTIFICATION, params:notificationParams}); + + siren?.startRealTimeFetch({eventType: EventType.NOTIFICATION, params:notificationParams}); } catch (err) { setIsLoading(false); setIsError(true); diff --git a/src/components/sirenInboxIcon.tsx b/src/components/sirenInboxIcon.tsx index 355914c..bc9fbf6 100644 --- a/src/components/sirenInboxIcon.tsx +++ b/src/components/sirenInboxIcon.tsx @@ -8,16 +8,7 @@ import { useSirenContext } from './sirenProvider'; import type { SirenInboxIconProps } from '../types'; import { CommonUtils, Constants } from '../utils'; -const { - ThemeMode, - defaultBadgeStyle, - eventTypes, - events, - defaultStyles, - EventType, - VerificationStatus, - errorMap -} = Constants; +const { ThemeMode, defaultBadgeStyle, eventTypes, events, defaultStyles, EventType } = Constants; const { logger } = CommonUtils; /** @@ -51,7 +42,7 @@ const SirenInboxIcon = (props: SirenInboxIconProps) => { onError = () => null } = props; - const { siren, verificationStatus, id } = useSirenContext(); + const { siren, id } = useSirenContext(); const [unviewedCount, seUnviewedCount] = useState(0); @@ -88,10 +79,8 @@ const SirenInboxIcon = (props: SirenInboxIconProps) => { }, []); useEffect(() => { - if (verificationStatus !== VerificationStatus.PENDING && siren) initialize(); - else if (verificationStatus === VerificationStatus.FAILED && onError) - onError(errorMap.MISSING_PARAMETER); - }, [siren, verificationStatus]); + if (siren) initialize(); + }, [siren]); useEffect(() => { if (unviewedCount > 0) logger.info(`unviewed notification count : ${unviewedCount}`); @@ -103,7 +92,7 @@ const SirenInboxIcon = (props: SirenInboxIconProps) => { const unViewed: UnviewedCountReturnResponse | null = await siren.fetchUnviewedNotificationsCount(); - siren.startRealTimeFetch({eventType: EventType.UNVIEWED_COUNT}); + siren.startRealTimeFetch({ eventType: EventType.UNVIEWED_COUNT }); if (unViewed?.data) seUnviewedCount(unViewed.data?.unviewedCount || 0); if (unViewed?.error) onError(unViewed?.error); } diff --git a/src/components/sirenProvider.tsx b/src/components/sirenProvider.tsx index c9f8999..1df4a8b 100644 --- a/src/components/sirenProvider.tsx +++ b/src/components/sirenProvider.tsx @@ -17,14 +17,12 @@ import { eventTypes, IN_APP_RECIPIENT_UNAUTHENTICATED, MAXIMUM_RETRY_COUNT, - VerificationStatus, EventType } from '../utils/constants'; import { useSiren } from '../utils'; type SirenContextProp = { siren: Siren | null; - verificationStatus: VerificationStatus; id: string; }; @@ -35,7 +33,6 @@ interface SirenProvider { export const SirenContext = createContext({ siren: null, - verificationStatus: VerificationStatus.PENDING, id: '' }); @@ -45,7 +42,6 @@ export const SirenContext = createContext({ * @example * const { * siren, - * verificationStatus * } = useSirenContext(); * * @returns {SirenContextProp} The Siren notifications context. @@ -56,7 +52,7 @@ export const useSirenContext = (): SirenContextProp => useContext(SirenContext); * Provides a React context for Siren notifications, making Siren SDK functionality * available throughout your React application. * - * `SirenProvider` initializes the Siren SDK with given configuration and manages the state for siren and verificationStatus. + * `SirenProvider` initializes the Siren SDK with given configuration and manages the state for siren. * * @component * @example @@ -80,17 +76,12 @@ const SirenProvider: React.FC = ({ config, children }) => { const [id] = useState(generateUniqueId()); const [siren, setSiren] = useState(null); - const [verificationStatus, setVerificationStatus] = useState( - VerificationStatus.PENDING - ); useEffect(() => { if (config?.recipientId && config?.userToken) { stopRealTimeFetch(); sendResetDataEvents(); initialize(); - } else { - setVerificationStatus(VerificationStatus.FAILED); } }, [config]); @@ -108,7 +99,10 @@ const SirenProvider: React.FC = ({ config, children }) => { }; PubSub.publish(`${events.NOTIFICATION_COUNT_EVENT}${id}`, JSON.stringify(updateCountPayload)); - PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${id}`, JSON.stringify(updateNotificationPayload)); + PubSub.publish( + `${events.NOTIFICATION_LIST_EVENT}${id}`, + JSON.stringify(updateNotificationPayload) + ); }; const onNewNotificationEvent = (responseData: NotificationDataType[]) => { @@ -135,14 +129,11 @@ const SirenProvider: React.FC = ({ config, children }) => { if (Array.isArray(responseData) && isNonEmptyArray(responseData)) onNewNotificationEvent(responseData); - }; const handleUnviewedCountEvent = (response: UnviewedCountApiResponse) => { const responseData = response?.data; - if (responseData && 'totalUnviewed' in responseData) - onTotalUnviewedCountEvent(response); - + if (responseData && 'totalUnviewed' in responseData) onTotalUnviewedCountEvent(response); }; const onEventReceive = ( response: NotificationsApiResponse | UnviewedCountApiResponse = {}, @@ -157,11 +148,8 @@ const SirenProvider: React.FC = ({ config, children }) => { break; } }; - const onStatusChange = (status: VerificationStatus) => { - setVerificationStatus(status); - }; - const actionCallbacks = { onEventReceive, onStatusChange }; + const actionCallbacks = { onEventReceive }; const getDataParams = () => { return { @@ -173,11 +161,7 @@ const SirenProvider: React.FC = ({ config, children }) => { }; const retryVerification = (error: SirenErrorType) => { - if ( - error.Code === IN_APP_RECIPIENT_UNAUTHENTICATED && - retryCount < MAXIMUM_RETRY_COUNT && - verificationStatus === VerificationStatus.FAILED - ) + if (error.Code === IN_APP_RECIPIENT_UNAUTHENTICATED && retryCount < MAXIMUM_RETRY_COUNT) setTimeout(() => { initialize(); retryCount++; @@ -188,7 +172,6 @@ const SirenProvider: React.FC = ({ config, children }) => { // Function to initialize the Siren SDK and fetch notifications const initialize = (): void => { - setVerificationStatus(VerificationStatus.PENDING); const dataParams: InitConfigType = getDataParams(); const siren = new Siren(dataParams); @@ -199,8 +182,7 @@ const SirenProvider: React.FC = ({ config, children }) => { {children} diff --git a/src/utils/sirenHook.ts b/src/utils/sirenHook.ts index f863b17..230eff7 100644 --- a/src/utils/sirenHook.ts +++ b/src/utils/sirenHook.ts @@ -27,7 +27,7 @@ const useSiren = () => { const markAsReadByDate = async (untilDate: string) => { if (siren && untilDate) { - const response = await siren?.markAsReadByDate(untilDate); + const response = await siren?.markAsReadByDate({startDate: untilDate}); if (response?.data) { const payload = { action: eventTypes.MARK_ALL_AS_READ }; @@ -62,7 +62,7 @@ const useSiren = () => { const deleteByDate = async (untilDate: string) => { if (siren && untilDate) { - const response = await siren.deleteByDate(untilDate); + const response = await siren.deleteByDate({startDate: untilDate}); if (response?.data) { const payload = { action: eventTypes.DELETE_ALL_ITEM }; diff --git a/tests/components/sirenNotificationIcon.test.tsx b/tests/components/sirenNotificationIcon.test.tsx index 0e29724..08fb5fa 100644 --- a/tests/components/sirenNotificationIcon.test.tsx +++ b/tests/components/sirenNotificationIcon.test.tsx @@ -6,7 +6,6 @@ import type { Siren } from '@sirenapp/js-sdk'; import { SirenInboxIcon } from '../../src'; import type { Theme } from '../../src/types'; import * as sirenProvider from '../../src/components/sirenProvider'; -import {VerificationStatus} from '../../src/utils/constants' const UnviewedCountReturnResponse = { data: { @@ -39,7 +38,6 @@ describe('SirenInboxIcon', () => { deleteById: jest.fn(), deleteByDate: jest.fn(), markAllAsViewed: jest.fn(), - verifyToken: jest.fn(), fetchUnviewedNotificationsCount: jest.fn(async () => UnviewedCountReturnResponse), fetchAllNotifications: jest.fn(), startRealTimeFetch: jest.fn(), @@ -48,7 +46,7 @@ describe('SirenInboxIcon', () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.PENDING + id: '' }); it('renders without crashing', () => { diff --git a/tests/utils/sirenHook.test.ts b/tests/utils/sirenHook.test.ts index 979b2e7..3ee00b1 100644 --- a/tests/utils/sirenHook.test.ts +++ b/tests/utils/sirenHook.test.ts @@ -2,7 +2,6 @@ import type { Siren } from '@sirenapp/js-sdk'; import { useSiren } from '../../src'; import * as sirenProvider from '../../src/components/sirenProvider'; -import {VerificationStatus} from '../../src/utils/constants' const Response = { data: { @@ -78,7 +77,6 @@ describe('useSiren hook', () => { deleteById: jest.fn(async () => ActionResponse), deleteByDate: jest.fn(async () => ActionResponse), markAllAsViewed: jest.fn(async () => MarkAsViewedResponse), - verifyToken: jest.fn(), fetchUnviewedNotificationsCount: jest.fn(), fetchAllNotifications: jest.fn(), startRealTimeFetch: jest.fn(), @@ -90,7 +88,7 @@ describe('useSiren hook', () => { // Mock useSirenContext jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.SUCCESS + id: '' }); const { markAsReadById } = useSiren(); @@ -103,7 +101,7 @@ describe('useSiren hook', () => { it('should call siren.markAsReadByDate and update notifications list when siren exists and untilDate is provided', async () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.SUCCESS + id: '' }); const { markAsReadByDate } = useSiren(); @@ -118,7 +116,7 @@ describe('useSiren hook', () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.SUCCESS + id: '' }); const { deleteById } = useSiren(); @@ -132,7 +130,7 @@ describe('useSiren hook', () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.SUCCESS + id: '' }); const { deleteByDate } = useSiren(); @@ -147,7 +145,7 @@ describe('useSiren hook', () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, - verificationStatus: VerificationStatus.SUCCESS + id: '' }); const { markAllAsViewed } = useSiren(); From b1fb06c3978e01d9dca5f0d4db9f8b810bb1b0c9 Mon Sep 17 00:00:00 2001 From: Abhay Date: Thu, 20 Mar 2025 17:10:09 +0530 Subject: [PATCH 2/2] Fix: Update test cases --- src/components/sirenProvider.tsx | 15 --------------- tests/components/sirenProvider.test.tsx | 2 -- tests/utils/sirenHook.test.ts | 6 +++--- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/components/sirenProvider.tsx b/src/components/sirenProvider.tsx index 1df4a8b..5aa2f1e 100644 --- a/src/components/sirenProvider.tsx +++ b/src/components/sirenProvider.tsx @@ -6,7 +6,6 @@ import type { InitConfigType, NotificationDataType, NotificationsApiResponse, - SirenErrorType, UnviewedCountApiResponse } from '@sirenapp/js-sdk/dist/esm/types'; @@ -15,8 +14,6 @@ import { generateUniqueId, isNonEmptyArray, logger } from '../utils/commonUtils' import { events, eventTypes, - IN_APP_RECIPIENT_UNAUTHENTICATED, - MAXIMUM_RETRY_COUNT, EventType } from '../utils/constants'; import { useSiren } from '../utils'; @@ -70,7 +67,6 @@ export const useSirenContext = (): SirenContextProp => useContext(SirenContext); * @param {React.ReactNode} props.children - Child components that will have access to the Siren context. */ const SirenProvider: React.FC = ({ config, children }) => { - let retryCount = 0; const { markAllAsViewed } = useSiren(); @@ -155,21 +151,10 @@ const SirenProvider: React.FC = ({ config, children }) => { return { token: config.userToken, recipientId: config.recipientId, - onError: retryVerification, actionCallbacks: actionCallbacks }; }; - const retryVerification = (error: SirenErrorType) => { - if (error.Code === IN_APP_RECIPIENT_UNAUTHENTICATED && retryCount < MAXIMUM_RETRY_COUNT) - setTimeout(() => { - initialize(); - retryCount++; - }, 5000); - - if (retryCount === MAXIMUM_RETRY_COUNT) stopRealTimeFetch(); - }; - // Function to initialize the Siren SDK and fetch notifications const initialize = (): void => { const dataParams: InitConfigType = getDataParams(); diff --git a/tests/components/sirenProvider.test.tsx b/tests/components/sirenProvider.test.tsx index 0c5b0c2..e288894 100644 --- a/tests/components/sirenProvider.test.tsx +++ b/tests/components/sirenProvider.test.tsx @@ -23,13 +23,11 @@ describe('SirenProvider', () => { Child ); - const mocErrorFn = jest.fn(); const mockEventHandler = jest.fn(); const sirenObject = new Siren({ token: 'user-token', recipientId: 'recipient-id', - onError: mocErrorFn, actionCallbacks: { onEventReceive: mockEventHandler, } diff --git a/tests/utils/sirenHook.test.ts b/tests/utils/sirenHook.test.ts index 3ee00b1..f730d5e 100644 --- a/tests/utils/sirenHook.test.ts +++ b/tests/utils/sirenHook.test.ts @@ -108,7 +108,7 @@ describe('useSiren hook', () => { const untilDate = '2024-02-28T00:00:00Z'; const response = await markAsReadByDate(untilDate); - expect(mockSiren.markAsReadByDate).toHaveBeenCalledWith(untilDate); + expect(mockSiren.markAsReadByDate).toHaveBeenCalledWith({startDate: untilDate}); expect(response).toEqual(ActionResponse); }); @@ -126,7 +126,7 @@ describe('useSiren hook', () => { expect(response).toEqual(ActionResponse); }); - it('should call siren.deleteByDate and update notifications list when siren exists and untilDate is provided', async () => {; + it('should call siren.deleteByDate and update notifications list when siren exists and untilDate is provided', async () => { jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({ siren: mockSiren as Siren, @@ -137,7 +137,7 @@ describe('useSiren hook', () => { const untilDate = '2024-02-28T00:00:00Z'; const response = await deleteByDate(untilDate); - expect(mockSiren.deleteByDate).toHaveBeenCalledWith(untilDate); + expect(mockSiren.deleteByDate).toHaveBeenCalledWith({startDate: untilDate}); expect(response).toEqual(ActionResponse); });