Skip to content

Fix: Remove verification status step during initialization #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@
],
"dependencies": {
"pubsub-js": "^1.9.4",
"@sirenapp/js-sdk": "^1.1.0"
"@sirenapp/js-sdk": "^1.2.3"
}
}
24 changes: 8 additions & 16 deletions src/components/sirenInbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ const {
events,
TOKEN_VERIFICATION_PENDING,
MAXIMUM_ITEMS_PER_FETCH,
VerificationStatus,
EventType,
errorMap
EventType
} = Constants;
const { applyTheme, isNonEmptyArray, updateNotifications } = CommonUtils;

Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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});
}
};

Expand Down Expand Up @@ -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);
Expand Down
21 changes: 5 additions & 16 deletions src/components/sirenInboxIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -51,7 +42,7 @@ const SirenInboxIcon = (props: SirenInboxIconProps) => {
onError = () => null
} = props;

const { siren, verificationStatus, id } = useSirenContext();
const { siren, id } = useSirenContext();

const [unviewedCount, seUnviewedCount] = useState<number>(0);

Expand Down Expand Up @@ -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}`);
Expand All @@ -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);
}
Expand Down
49 changes: 8 additions & 41 deletions src/components/sirenProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
InitConfigType,
NotificationDataType,
NotificationsApiResponse,
SirenErrorType,
UnviewedCountApiResponse
} from '@sirenapp/js-sdk/dist/esm/types';

Expand All @@ -15,16 +14,12 @@ import { generateUniqueId, isNonEmptyArray, logger } from '../utils/commonUtils'
import {
events,
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;
};

Expand All @@ -35,7 +30,6 @@ interface SirenProvider {

export const SirenContext = createContext<SirenContextProp>({
siren: null,
verificationStatus: VerificationStatus.PENDING,
id: ''
});

Expand All @@ -45,7 +39,6 @@ export const SirenContext = createContext<SirenContextProp>({
* @example
* const {
* siren,
* verificationStatus
* } = useSirenContext();
*
* @returns {SirenContextProp} The Siren notifications context.
Expand All @@ -56,7 +49,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
Expand All @@ -74,23 +67,17 @@ 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<SirenProvider> = ({ config, children }) => {
let retryCount = 0;

const { markAllAsViewed } = useSiren();

const [id] = useState(generateUniqueId());
const [siren, setSiren] = useState<Siren | null>(null);
const [verificationStatus, setVerificationStatus] = useState<VerificationStatus>(
VerificationStatus.PENDING
);

useEffect(() => {
if (config?.recipientId && config?.userToken) {
stopRealTimeFetch();
sendResetDataEvents();
initialize();
} else {
setVerificationStatus(VerificationStatus.FAILED);
}
}, [config]);

Expand All @@ -108,7 +95,10 @@ const SirenProvider: React.FC<SirenProvider> = ({ 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[]) => {
Expand All @@ -135,14 +125,11 @@ const SirenProvider: React.FC<SirenProvider> = ({ 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 = {},
Expand All @@ -157,38 +144,19 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
break;
}
};
const onStatusChange = (status: VerificationStatus) => {
setVerificationStatus(status);
};

const actionCallbacks = { onEventReceive, onStatusChange };
const actionCallbacks = { onEventReceive };

const getDataParams = () => {
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 &&
verificationStatus === VerificationStatus.FAILED
)
setTimeout(() => {
initialize();
retryCount++;
}, 5000);

if (retryCount === MAXIMUM_RETRY_COUNT) stopRealTimeFetch();
};

// Function to initialize the Siren SDK and fetch notifications
const initialize = (): void => {
setVerificationStatus(VerificationStatus.PENDING);
const dataParams: InitConfigType = getDataParams();
const siren = new Siren(dataParams);

Expand All @@ -199,8 +167,7 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
<SirenContext.Provider
value={{
id,
siren,
verificationStatus
siren
}}
>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/sirenHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 };
Expand Down
4 changes: 1 addition & 3 deletions tests/components/sirenNotificationIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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(),
Expand All @@ -48,7 +46,7 @@ describe('SirenInboxIcon', () => {

jest.spyOn(sirenProvider, 'useSirenContext').mockReturnValue({
siren: mockSiren as Siren,
verificationStatus: VerificationStatus.PENDING
id: ''
});

it('renders without crashing', () => {
Expand Down
2 changes: 0 additions & 2 deletions tests/components/sirenProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ describe('SirenProvider', () => {
<Text>Child</Text>
</SirenProvider>
);
const mocErrorFn = jest.fn();
const mockEventHandler = jest.fn();

const sirenObject = new Siren({
token: 'user-token',
recipientId: 'recipient-id',
onError: mocErrorFn,
actionCallbacks: {
onEventReceive: mockEventHandler,
}
Expand Down
Loading
Loading