Skip to content

Commit acca131

Browse files
authored
feat: Added new prop to hide badge and updated component logic accordingly (#22)
1 parent e664b1a commit acca131

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ title | Title of the notification inbox | string | "Notifications" |
5959
loadMoreLabel | Text shown on the load more component | string | "Load More"
6060
hideHeader | Toggle to hide or show the header section | boolean | false |
6161
hideClearAll | Toggle to hide or show the clear all button | boolean | false |
62+
hideBadge | Toggle to hide or show the badge | boolean | false |
6263
darkMode | Toggle to enable dark mode | boolean | false |
6364
itemsPerFetch | Number of notifications fetch per api request (have a max cap of 50) | number | 20 |
6465
windowViewOnly | Toggle to enable fit-to-screen window or modal view | boolean | false |

src/components/SirenInbox.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { DEFAULT_WINDOW_TITLE } = Constants;
1919
* @param {boolean} [props.windowViewOnly=false] - Flag indicating if the window is view-only
2020
* @param {boolean} [props.hideHeader] - Flag indicating if the header should be hidden
2121
* @param {boolean} [props.hideClearAll] - Flag indicating if the clear all button should be hidden
22+
* @param {boolean} [props.hideBadge] - Flag indicating if the badge should be hidden
2223
* @param {boolean} [props.darkMode] - Flag indicating if the component is in dark mode
2324
* @param {CardProps} [props.cardProps] - Additional props for the card component
2425
* @param {ReactNode} [props.notificationIcon] - The notification icon for the window
@@ -42,6 +43,7 @@ const SirenInbox: FC<SirenProps> = ({
4243
title = DEFAULT_WINDOW_TITLE,
4344
windowViewOnly = false,
4445
hideHeader,
46+
hideBadge = false,
4547
hideClearAll = false,
4648
darkMode = false,
4749
cardProps,
@@ -133,6 +135,7 @@ const SirenInbox: FC<SirenProps> = ({
133135
onIconClick={onIconClick}
134136
badgeType={isModalOpen ? BadgeType.NONE : BadgeType.DEFAULT}
135137
darkMode={darkMode}
138+
hideBadge={hideBadge}
136139
/>
137140
</div>
138141
)}
@@ -159,6 +162,7 @@ const SirenInbox: FC<SirenProps> = ({
159162
styles={styles}
160163
noOfNotificationsPerFetch={notificationsPerPage}
161164
hideHeader={hideHeader}
165+
hideBadge={hideBadge}
162166
cardProps={cardProps}
163167
customFooter={customFooter}
164168
customHeader={customHeader}

src/components/SirenNotificationIcon.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { eventTypes, events } = Constants;
1717
* @param {BadgeType} [props.badgeType=DEFAULT] - The type of badge to display
1818
* @param {Object} [props.styles] - Custom styles for the component
1919
* @param {Function} props.onIconClick - Click event handler for the icon
20+
* @param {boolean} [props.hideBadge] - Flag indicating if the badge should be hidden
2021
* @returns {JSX.Element} - SirenNotificationIcon component JSX
2122
*/
2223

@@ -26,6 +27,7 @@ const SirenNotificationIcon: FC<SirenNotificationButtonProps> = ({
2627
styles,
2728
onIconClick,
2829
darkMode,
30+
hideBadge,
2931
}) => {
3032
const { siren } = useSirenContext();
3133

@@ -42,23 +44,28 @@ const SirenNotificationIcon: FC<SirenNotificationButtonProps> = ({
4244
};
4345

4446
useEffect(() => {
45-
PubSub.subscribe(
46-
events.NOTIFICATION_COUNT_EVENT,
47-
notificationCountSubscriber
48-
);
49-
50-
return () => {
51-
cleanUp();
52-
};
47+
if(!hideBadge) {
48+
PubSub.subscribe(
49+
events.NOTIFICATION_COUNT_EVENT,
50+
notificationCountSubscriber
51+
);
52+
53+
return () => {
54+
cleanUp();
55+
};
56+
}
5357
}, []);
5458

5559
useEffect(() => {
56-
getUnViewedCount();
57-
}, [siren]);
60+
if(!hideBadge)
61+
getUnViewedCount();
62+
63+
}, [siren, hideBadge]);
5864

5965
useEffect(() => {
60-
startRealTimeDataFetch();
61-
}, []);
66+
if(!hideBadge)
67+
startRealTimeDataFetch();
68+
}, [hideBadge]);
6269

6370
const cleanUp = () => {
6471
siren?.stopRealTimeUnviewedCountFetch();
@@ -135,7 +142,7 @@ const SirenNotificationIcon: FC<SirenNotificationButtonProps> = ({
135142
}
136143
/>
137144
)}
138-
{renderBadge()}
145+
{!hideBadge && renderBadge()}
139146
</button>
140147
);
141148
};

src/components/SirenPanel.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import useSiren from "../utils/sirenHook";
4949
* @param {string} props.title - The title of the notification panel.
5050
* @param {boolean} props.hideHeader=false] - Whether to hide the header of the notification panel.
5151
* @param {boolean} props.hideClearAll=false] - Flag indicating if the clear all button should be hidden
52+
* @param {boolean} [props.hideBadge] - Flag indicating if the badge should be hidden
5253
* @param {string} props.loadMoreLabel - Label for load more button
5354
* @param {Object} props.cardProps - Optional properties to customize the appearance of notification cards.
5455
* @param {Function} props.renderListEmpty - Function to render content when the notification list is empty.
@@ -74,6 +75,7 @@ const SirenPanel: FC<SirenPanelProps> = ({
7475
title,
7576
loadMoreLabel,
7677
hideHeader,
78+
hideBadge,
7779
darkMode,
7880
cardProps,
7981
customFooter,
@@ -117,10 +119,10 @@ const SirenPanel: FC<SirenPanelProps> = ({
117119
return () => {
118120
cleanUp();
119121
setNotifications([]);
120-
restartNotificationCountFetch();
122+
!hideBadge && restartNotificationCountFetch();
121123
handleMarkNotificationsAsViewed(new Date().toISOString());
122124
};
123-
}, []);
125+
}, [hideBadge]);
124126

125127
useEffect(() => {
126128
if (eventListenerData) {
@@ -137,10 +139,10 @@ const SirenPanel: FC<SirenPanelProps> = ({
137139

138140
useEffect(() => {
139141
if (siren && verificationStatus !== VerificationStatus.PENDING) {
140-
siren.stopRealTimeUnviewedCountFetch();
142+
!hideBadge && siren.stopRealTimeUnviewedCountFetch();
141143
fetchNotifications(true);
142144
}
143-
}, [siren, verificationStatus]);
145+
}, [siren, verificationStatus, hideBadge]);
144146

145147

146148
const restartNotificationCountFetch = () => {

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type SirenInboxProps = {
1212
loadMoreLabel?: string,
1313
hideHeader?: boolean;
1414
hideClearAll?: boolean;
15+
hideBadge?: boolean;
1516
darkMode?: boolean;
1617
itemsPerFetch?: number;
1718
cardProps?: CardProps;
@@ -62,12 +63,14 @@ export type SirenNotificationButtonProps = {
6263
styles: SirenStyleProps;
6364
badgeType: BadgeType;
6465
darkMode: boolean;
66+
hideBadge: boolean;
6567
notificationIcon?: JSX.Element;
6668
onIconClick: () => void;
6769
};
6870
export type SirenPanelProps = Pick<
6971
SirenInboxProps,
7072
| "hideHeader"
73+
| "hideBadge"
7174
| "cardProps"
7275
| "customFooter"
7376
| "customHeader"

tests/components/sirenNotificationIcon.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test("matches snapshot", () => {
2727
badgeType="default"
2828
onIconClick={mockClickFn}
2929
darkMode={false}
30+
hideBadge={false}
3031
/>
3132
);
3233

@@ -41,6 +42,7 @@ test("should render notification icon only", () => {
4142
notificationIcon={<div />}
4243
badgeType="none"
4344
darkMode={true}
45+
hideBadge={false}
4446
/>
4547
);
4648
const badge = component.queryByTestId("notification-default-badge");
@@ -56,6 +58,7 @@ test("should notification icon with a dot badge", () => {
5658
notificationIcon={<div />}
5759
badgeType="dot"
5860
darkMode={true}
61+
hideBadge={false}
5962
/>
6063
);
6164
const badge = component.queryByTestId("notification-dot-badge");
@@ -70,6 +73,7 @@ test("should render the passed notification icon", () => {
7073
badgeType="default"
7174
notificationIcon={<div data-testid="custom-notification-icon" />}
7275
darkMode={true}
76+
hideBadge={false}
7377
/>
7478
);
7579
const button = component.getByTestId("custom-notification-icon"); // Assuming count is displayed as button text
@@ -85,6 +89,7 @@ test("should call onClick handler on button click", () => {
8589
notificationIcon={<div />}
8690
badgeType="dot"
8791
darkMode={true}
92+
hideBadge={false}
8893
/>
8994
);
9095
const button = component.getByTestId("notification-icon");
@@ -102,6 +107,7 @@ it("does not render badge with count if unviewed count is 0", () => {
102107
onIconClick={mockClickFn}
103108
notificationIcon={<div />}
104109
darkMode={true}
110+
hideBadge={false}
105111
/>
106112
);
107113
const badge = queryByTestId("notification-default-badge");
@@ -117,6 +123,7 @@ it("renders badge without count for DOT badge type", () => {
117123
notificationIcon={<div />}
118124
badgeType="dot"
119125
darkMode={true}
126+
hideBadge={false}
120127
/>
121128
);
122129
const badge = getByTestId("notification-dot-badge");
@@ -133,6 +140,7 @@ it("calls onClick handler when icon is clicked", () => {
133140
notificationIcon={<div />}
134141
badgeType="dot"
135142
darkMode={true}
143+
hideBadge={false}
136144
/>
137145
);
138146
const icon = getByTestId("notification-icon");
@@ -149,6 +157,7 @@ test("should not render badge with count if unviewed count is 0", () => {
149157
onIconClick={mockClickFn}
150158
notificationIcon={<div />}
151159
darkMode={true}
160+
hideBadge={false}
152161
/>
153162
);
154163
const badge = queryByTestId("notification-default-badge");
@@ -164,6 +173,7 @@ test("should hide badge if unviewed count is 0 and badge type is default", () =>
164173
badgeType="default"
165174
notificationIcon={<div />}
166175
darkMode={true}
176+
hideBadge={false}
167177
/>
168178
);
169179
const badge = queryByTestId("notification-default-badge");
@@ -179,6 +189,7 @@ test("should hide badge if badge type is set to none", () => {
179189
badgeType="none"
180190
notificationIcon={<div />}
181191
darkMode={true}
192+
hideBadge={false}
182193
/>
183194
);
184195
const badge = queryByTestId("notification-default-badge");
@@ -194,6 +205,7 @@ test("calls PubSub.subscribe with correct arguments", () => {
194205
badgeType="default"
195206
onIconClick={mockClickFn}
196207
darkMode={true}
208+
hideBadge={false}
197209
/>,
198210
{
199211
wrapper: ({ children }) => (

0 commit comments

Comments
 (0)