From aa2a61c3c7655b4be735f1bc715bda53f77d88a8 Mon Sep 17 00:00:00 2001 From: Abhay Date: Mon, 18 Mar 2024 14:30:21 +0530 Subject: [PATCH] feat: Implement retry token verification feature --- src/components/sirenProvider.tsx | 14 ++++++++++++-- src/utils/constants.ts | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/sirenProvider.tsx b/src/components/sirenProvider.tsx index b5ee748..b6aeb2b 100644 --- a/src/components/sirenProvider.tsx +++ b/src/components/sirenProvider.tsx @@ -12,7 +12,7 @@ import type { import type { SirenProviderConfigProps } from '../types'; import { isNonEmptyArray, logger } from '../utils/commonUtils'; -import { events, eventTypes } from '../utils/constants'; +import { events, eventTypes, TOKEN_VERIFICATION_FAILED } from '../utils/constants'; type SirenContextProp = { siren: Siren | null; @@ -66,6 +66,8 @@ 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 [siren, setSiren] = useState(null); useEffect(() => { @@ -121,11 +123,19 @@ const SirenProvider: React.FC = ({ config, children }) => { return { token: config.userToken, recipientId: config.recipientId, - onError: (error: SirenErrorType): void => logger.info(`Error : ${JSON.stringify(error)}`), + onError: retryVerification, actionCallbacks: actionCallbacks }; }; + const retryVerification = (error: SirenErrorType) => { + if (error.Code === TOKEN_VERIFICATION_FAILED && retryCount < 3) + setTimeout(() => { + initialize(); + retryCount++; + }, 5000); + }; + // Function to initialize the Siren SDK and fetch notifications const initialize = (): void => { const dataParams: InitConfigType = getDataParams(); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 26db69f..1005de7 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -74,6 +74,7 @@ export const ERROR_DESCRIPTION = 'Could not load the notifications. Please refre export const DEFAULT_WINDOW_TITLE = 'Notifications'; export const RETRY_BUTTON_LABEL = 'Retry'; export const CLEAR_ALL_LABEL = 'Clear All'; +export const TOKEN_VERIFICATION_FAILED = 'TOKEN_VERIFICATION_FAILED'; export const errorMap = { SIREN_OBJECT_NOT_FOUND: {