From 84937305af779813fc214718e82b89ccfab90a60 Mon Sep 17 00:00:00 2001 From: reshmarevi17 Date: Wed, 5 Jun 2024 11:46:23 +0530 Subject: [PATCH 1/3] feat: Add category filter --- package-lock.json | 24 ++++++++++++------------ package.json | 2 +- src/components/SirenInbox.tsx | 2 ++ src/components/SirenPanel.tsx | 11 +++++++---- src/components/SirenProvider.tsx | 6 +++--- src/types.ts | 4 +++- src/utils/commonUtils.ts | 10 ++++++---- src/utils/sirenHook.ts | 17 ++++++++++------- tests/components/sirenProvider.spec.tsx | 4 ++-- tests/utils/sirenHook.spec.tsx | 12 +++++++----- 10 files changed, 53 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 20f6d4b..3eaa1e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,8 @@ "version": "1.1.0", "license": "MIT", "dependencies": { - "@sirenapp/js-sdk": "^1.1.0", - "pubsub-js": "^1.9.4" + "pubsub-js": "^1.9.4", + "test_notification": "^1.1.5" }, "devDependencies": { "@babel/cli": "^7.23.9", @@ -4956,16 +4956,6 @@ "@sinonjs/commons": "^3.0.0" } }, - "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==", - "dependencies": { - "promise-polyfill": "^8.3.0", - "tslib": "^2.6.2", - "whatwg-fetch": "^3.6.20" - } - }, "node_modules/@storybook/addon-actions": { "version": "7.6.17", "resolved": "https://registry.npmjs.org/@storybook/addon-actions/-/addon-actions-7.6.17.tgz", @@ -21263,6 +21253,16 @@ "source-map": "^0.6.0" } }, + "node_modules/test_notification": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/test_notification/-/test_notification-1.1.5.tgz", + "integrity": "sha512-z16D1EkbcFf50oJ7SFaqVVt9pwIP8EOUwFi9rIUOA04lRg00zuFv94p54HysrEd9/b56LezRjRYsoiTgefyyFg==", + "dependencies": { + "promise-polyfill": "^8.3.0", + "tslib": "^2.6.2", + "whatwg-fetch": "^3.6.20" + } + }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", diff --git a/package.json b/package.json index dc758ac..17bf777 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "typescript": "^5.3.3" }, "dependencies": { - "@sirenapp/js-sdk": "^1.1.0", + "test_notification": "^1.1.5", "pubsub-js": "^1.9.4" } } diff --git a/src/components/SirenInbox.tsx b/src/components/SirenInbox.tsx index eb2be48..76c5f8b 100644 --- a/src/components/SirenInbox.tsx +++ b/src/components/SirenInbox.tsx @@ -60,6 +60,7 @@ const SirenInbox: FC = ({ onCardClick, onError, itemsPerFetch = 20, + category, }) => { const { siren } = useSirenContext(); @@ -205,6 +206,7 @@ const SirenInbox: FC = ({ darkMode={darkMode} customErrorWindow={customErrorWindow} modalWidth={updatedModalWidth} + category={category} /> )} diff --git a/src/components/SirenPanel.tsx b/src/components/SirenPanel.tsx index fa9db12..0f3a20c 100644 --- a/src/components/SirenPanel.tsx +++ b/src/components/SirenPanel.tsx @@ -6,7 +6,7 @@ import type { NotificationDataType, NotificationsApiResponse, SirenErrorType, -} from "@sirenapp/js-sdk/dist/esm/types"; +} from "test_notification/dist/esm/types"; import "../styles/sirenPanel.css"; import NotificationCard from "./Card"; @@ -87,6 +87,7 @@ const SirenPanel: FC = ({ onCardClick, onError, modalWidth, + category, }) => { const { markAllAsViewed, @@ -187,7 +188,8 @@ const SirenPanel: FC = ({ try { if (!isEmptyArray(notifications)) { const response = await deleteByDate( - notifications[0].createdAt + notifications[0].createdAt, + category ); response && triggerOnError(response); @@ -227,7 +229,8 @@ const SirenPanel: FC = ({ generateFilterParams( isRefresh ? [] : notifications, false, - noOfNotificationsPerFetch + noOfNotificationsPerFetch, + category, ) ); @@ -270,7 +273,7 @@ const SirenPanel: FC = ({ try { siren?.startRealTimeFetch( - {eventType: EventType.NOTIFICATION, params: generateFilterParams(newList ?? [], true, noOfNotificationsPerFetch)} + {eventType: EventType.NOTIFICATION, params: generateFilterParams(newList ?? [], true, noOfNotificationsPerFetch, category)} ); } catch (er) { // handle error if needed diff --git a/src/components/SirenProvider.tsx b/src/components/SirenProvider.tsx index e69b6b5..6750ecf 100644 --- a/src/components/SirenProvider.tsx +++ b/src/components/SirenProvider.tsx @@ -1,13 +1,13 @@ import React, { createContext, useContext, useEffect, useMemo, useState } from "react"; -import { Siren } from "@sirenapp/js-sdk"; +import PubSub from "pubsub-js"; +import { Siren } from "test_notification"; import type { InitConfigType, NotificationsApiResponse, SirenErrorType, UnviewedCountApiResponse, -} from "@sirenapp/js-sdk/dist/esm/types"; -import PubSub from "pubsub-js"; +} from "test_notification/dist/esm/types"; import type { SirenProviderConfigProps } from "../types"; import { generateUniqueId, logger } from "../utils/commonUtils"; diff --git a/src/types.ts b/src/types.ts index d55cd59..9a8a771 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,7 @@ import type { CSSProperties } from "react"; import type { NotificationDataType, SirenErrorType, -} from "@sirenapp/js-sdk/dist/esm/types"; +} from "test_notification/dist/esm/types"; export type SirenInboxProps = { theme?: Theme; @@ -27,6 +27,7 @@ export type SirenInboxProps = { customCard?: (notification: NotificationDataType) => JSX.Element; onCardClick?: (notification: NotificationDataType) => void; onError?: (error: SirenErrorType) => void; + category?: string; }; export type SirenNotificationIconProps = { @@ -89,6 +90,7 @@ export type SirenPanelProps = Pick< | "loadMoreComponent" | "loadMoreLabel" | "customErrorWindow" + | "category" > & { styles: SirenStyleProps; onError?: (error: SirenErrorType) => void; diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index fdfafc0..da87c79 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -4,7 +4,7 @@ import type { ActionResponse, NotificationDataType, NotificationsApiResponse, -} from "@sirenapp/js-sdk/dist/esm/types"; +} from "test_notification/dist/esm/types"; import { defaultBadgeStyle, @@ -26,6 +26,7 @@ type FetchParams = { start?: string; end?: string; sort?: "createdAt" | "updatedAt"; + category?: string; }; export const generateElapsedTimeText = (timeString: string) => { @@ -362,14 +363,15 @@ export const filterDataProperty = ( export const generateFilterParams = ( data: NotificationDataType[], fromStart: boolean, - itemsPerPage: number + itemsPerPage: number, + category?: string, ): FetchParams => { - let params: FetchParams = { size: itemsPerPage, sort: "createdAt" }; + let params: FetchParams = { size: itemsPerPage, sort: "createdAt", category}; if (data.length > 0) if (fromStart) params = { ...params, start: data[0].createdAt }; else params = { ...params, end: data[data.length - 1].createdAt }; - + return params; }; diff --git a/src/utils/sirenHook.ts b/src/utils/sirenHook.ts index 92d031f..662a2db 100644 --- a/src/utils/sirenHook.ts +++ b/src/utils/sirenHook.ts @@ -11,7 +11,7 @@ const useSiren = () => { if (id?.length > 0) { const response = await siren?.markAsReadById(id); - if (response && response.data) { + if (response?.data) { const payload = { id, action: eventTypes.MARK_ITEM_AS_READ }; PubSub.publish( @@ -28,11 +28,12 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; - const markAsReadByDate = async (untilDate: string) => { + const markAsReadByDate = async (untilDate: string, category?: string) => { if (siren && untilDate) { - const response = await siren?.markAsReadByDate(untilDate); + const params = { startDate: untilDate, category }; + const response = await siren?.markAsReadByDate(params); - if (response && response.data) { + if (response?.data) { const payload = { action: eventTypes.MARK_ALL_AS_READ }; PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerInstanceId}`, JSON.stringify(payload)); @@ -43,6 +44,7 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; + const deleteById = async (id: string, shouldUpdateList: boolean = true) => { if (siren) if (id?.length > 0) { @@ -65,11 +67,12 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; - const deleteByDate = async (untilDate: string) => { + const deleteByDate = async (untilDate: string, category?: string) => { if (siren && untilDate) { - const response = await siren.deleteByDate(untilDate); + const params = { startDate: untilDate, category }; + const response = await siren?.deleteByDate(params); - if (response && response.data) { + if (response?.data) { const payload = { action: eventTypes.DELETE_ALL_ITEM }; PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerInstanceId}`, JSON.stringify(payload)); diff --git a/tests/components/sirenProvider.spec.tsx b/tests/components/sirenProvider.spec.tsx index 9efc6e7..6d1ff70 100644 --- a/tests/components/sirenProvider.spec.tsx +++ b/tests/components/sirenProvider.spec.tsx @@ -1,11 +1,11 @@ import React from "react"; -import { Siren } from "@sirenapp/js-sdk"; +import { Siren } from "test_notification"; import { render } from "@testing-library/react"; import SirenProvider from "../../src/components/SirenProvider"; // Replace with your path -jest.mock("@sirenapp/js-sdk"); +jest.mock("test_notification"); describe("SirenProvider", () => { it("should render children", () => { diff --git a/tests/utils/sirenHook.spec.tsx b/tests/utils/sirenHook.spec.tsx index a27cf78..078d06e 100644 --- a/tests/utils/sirenHook.spec.tsx +++ b/tests/utils/sirenHook.spec.tsx @@ -1,4 +1,4 @@ -import type { Siren } from "@sirenapp/js-sdk"; +import type { Siren } from "test_notification"; import { useSiren } from "../../src"; import * as sirenProvider from "../../src/components/SirenProvider"; @@ -170,8 +170,9 @@ describe("useSiren hook", () => { const untilDate = "2024-02-28T00:00:00Z"; const response = await markAsReadByDate(untilDate); - expect(mockSirenCore.markAsReadByDate).toHaveBeenCalledWith( - untilDate + expect(mockSirenCore.markAsReadByDate).toHaveBeenCalledWith({ + startDate: untilDate, + } ); expect(response).toEqual(ActionResponse); @@ -240,8 +241,9 @@ describe("useSiren hook", () => { const untilDate = "2024-02-28T00:00:00Z"; const response = await deleteByDate(untilDate); - expect(mockSirenCore.deleteByDate).toHaveBeenCalledWith( - untilDate + expect(mockSirenCore.deleteByDate).toHaveBeenCalledWith({ + startDate: untilDate, + } ); expect(response).toEqual(ActionResponse); From db988c45f4ef43c58b91f40b8a82494b9eb74dd0 Mon Sep 17 00:00:00 2001 From: reshmarevi17 Date: Wed, 5 Jun 2024 12:38:21 +0530 Subject: [PATCH 2/3] fix: Hooks input param format change --- src/components/SirenPanel.tsx | 6 +++--- src/utils/sirenHook.ts | 22 +++++++++++----------- tests/utils/sirenHook.spec.tsx | 10 +++++----- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/components/SirenPanel.tsx b/src/components/SirenPanel.tsx index 0f3a20c..bf064f6 100644 --- a/src/components/SirenPanel.tsx +++ b/src/components/SirenPanel.tsx @@ -187,9 +187,9 @@ const SirenPanel: FC = ({ const handleClearAllNotification = async (): Promise => { try { if (!isEmptyArray(notifications)) { - const response = await deleteByDate( - notifications[0].createdAt, - category + const response = await deleteByDate({ + startDate: notifications[0].createdAt, + category} ); response && triggerOnError(response); diff --git a/src/utils/sirenHook.ts b/src/utils/sirenHook.ts index 662a2db..bfdea86 100644 --- a/src/utils/sirenHook.ts +++ b/src/utils/sirenHook.ts @@ -28,9 +28,9 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; - const markAsReadByDate = async (untilDate: string, category?: string) => { - if (siren && untilDate) { - const params = { startDate: untilDate, category }; + const markAsReadByDate = async (params: { startDate: string, category?: string }) => { + + if (siren && params?.startDate) { const response = await siren?.markAsReadByDate(params); if (response?.data) { @@ -44,7 +44,6 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; - const deleteById = async (id: string, shouldUpdateList: boolean = true) => { if (siren) if (id?.length > 0) { @@ -67,28 +66,29 @@ const useSiren = () => { return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; - const deleteByDate = async (untilDate: string, category?: string) => { - if (siren && untilDate) { - const params = { startDate: untilDate, category }; + const deleteByDate = async (params: { startDate: string, category?: string }) => { + + if (siren && params?.startDate) { const response = await siren?.deleteByDate(params); - + if (response?.data) { const payload = { action: eventTypes.DELETE_ALL_ITEM }; PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerInstanceId}`, JSON.stringify(payload)); } - + return response; } - + return { error: errorMap.SIREN_OBJECT_NOT_FOUND }; }; + const markAllAsViewed = async (untilDate: string) => { if (siren && untilDate) { const response = await siren?.markAllAsViewed(untilDate); - if (response && response.data) { + if (response?.data) { const payload = { notificationsCount: 0, action: eventTypes.UPDATE_NOTIFICATIONS_COUNT, diff --git a/tests/utils/sirenHook.spec.tsx b/tests/utils/sirenHook.spec.tsx index 078d06e..2201e9d 100644 --- a/tests/utils/sirenHook.spec.tsx +++ b/tests/utils/sirenHook.spec.tsx @@ -168,7 +168,7 @@ describe("useSiren hook", () => { const { markAsReadByDate } = useSiren(); const untilDate = "2024-02-28T00:00:00Z"; - const response = await markAsReadByDate(untilDate); + const response = await markAsReadByDate({startDate: untilDate}); expect(mockSirenCore.markAsReadByDate).toHaveBeenCalledWith({ startDate: untilDate, @@ -188,9 +188,9 @@ describe("useSiren hook", () => { }); const { markAsReadByDate } = useSiren(); - const response = await markAsReadByDate( - undefined as unknown as string - ); + const response = await markAsReadByDate({ + startDate: undefined as unknown as string, + }); expect(response).toEqual({ error: errorMap.SIREN_OBJECT_NOT_FOUND, @@ -239,7 +239,7 @@ describe("useSiren hook", () => { const { deleteByDate } = useSiren(); const untilDate = "2024-02-28T00:00:00Z"; - const response = await deleteByDate(untilDate); + const response = await deleteByDate({startDate: untilDate}); expect(mockSirenCore.deleteByDate).toHaveBeenCalledWith({ startDate: untilDate, From fcafe6a792f40bbf113d4e13a25d09d888b03198 Mon Sep 17 00:00:00 2001 From: reshmarevi17 Date: Tue, 11 Jun 2024 11:20:40 +0530 Subject: [PATCH 3/3] fix: Example app changes --- example/src/App.tsx | 7 +++++-- package-lock.json | 8 ++++---- package.json | 2 +- src/utils/commonUtils.ts | 7 ++++++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 269e7ff..fc0ef53 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -83,8 +83,11 @@ const App: React.FC = () => {
Siren
-
-
markAsReadByDate(String(new Date().getTime()))}>Mark allAsRead
+
markAsReadByDate({ startDate: String(new Date().getTime()) })} + > + Mark allAsRead
); diff --git a/package-lock.json b/package-lock.json index 3eaa1e6..380dbae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "pubsub-js": "^1.9.4", - "test_notification": "^1.1.5" + "test_notification": "^1.1.6" }, "devDependencies": { "@babel/cli": "^7.23.9", @@ -21254,9 +21254,9 @@ } }, "node_modules/test_notification": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/test_notification/-/test_notification-1.1.5.tgz", - "integrity": "sha512-z16D1EkbcFf50oJ7SFaqVVt9pwIP8EOUwFi9rIUOA04lRg00zuFv94p54HysrEd9/b56LezRjRYsoiTgefyyFg==", + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/test_notification/-/test_notification-1.1.6.tgz", + "integrity": "sha512-BVXM4oreAD5sqMYxqnIDZjtXTGqkEPvDEYe9hJiY7XJV3ZVC1h0Ngoe0/e2VkkMAW07h2S2uQe43ObdvAb3FwQ==", "dependencies": { "promise-polyfill": "^8.3.0", "tslib": "^2.6.2", diff --git a/package.json b/package.json index 17bf777..d37ddfe 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "typescript": "^5.3.3" }, "dependencies": { - "test_notification": "^1.1.5", + "test_notification": "^1.1.6", "pubsub-js": "^1.9.4" } } diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index da87c79..6fc03af 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -366,7 +366,12 @@ export const generateFilterParams = ( itemsPerPage: number, category?: string, ): FetchParams => { - let params: FetchParams = { size: itemsPerPage, sort: "createdAt", category}; + let params: FetchParams = { size: itemsPerPage, sort: "createdAt"}; + + if (category) + params.category = category; + + if (data.length > 0) if (fromStart) params = { ...params, start: data[0].createdAt };