Skip to content

Commit c6520ce

Browse files
Merge pull request #41 from KeyValueSoftwareSystems/dev
Refactor: Update props, modal positioning logic change and readme update
2 parents 9fa3178 + a7afa8c commit c6520ce

File tree

15 files changed

+382
-249
lines changed

15 files changed

+382
-249
lines changed

README.md

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ The `@sirenapp/react-inbox` sdk is a comprehensive and customizable React UI kit
66

77
## 1. Installation
88

9-
You can install the react sdk from npm
9+
You can install the react sdk from npm
1010

1111
```bash
12-
npm @sirenapp/react-inbox
12+
npm install @sirenapp/react-inbox
1313
```
14+
1415
or from yarn
1516

1617
```bash
17-
yarn @sirenapp/react-inbox
18+
yarn add @sirenapp/react-inbox
1819
```
1920

2021
#### Prerequisites
2122

2223
- React v16.8+
2324

2425
## 2. Configuration
26+
2527
### 2.1 Initialization
28+
2629
Initialize the sdk with user token and recipient id. Wrap the provider around your App's root.
2730

2831
```js
@@ -37,15 +40,15 @@ const config = {
3740
```
3841

3942
### 2.2 Configure notification inbox
43+
4044
Once the provider is configured, next step is to configure the notification inbox
4145

4246
Inbox is a paginated list view for displaying notifications.
4347

4448
```js
45-
import { SirenInbox } from '@sirenapp/react-inbox';
49+
import { SirenInbox } from "@sirenapp/react-inbox";
4650

4751
<SirenInbox />
48-
4952
```
5053

5154
#### Props for the notification inbox
@@ -55,27 +58,25 @@ Below are optional props available for the inbox component:
5558
Prop | Description | Type | Default value |
5659
--- | --- | --- | --- |
5760
theme | Object for custom themes | Theme | {} |
58-
title | Title of the notification inbox | string | "Notifications" |
59-
loadMoreLabel | Text shown on the load more component | string | "Load More"
60-
hideHeader | Toggle to hide or show the header section | boolean | false |
61-
hideClearAll | Toggle to hide or show the clear all button | boolean | false |
61+
loadMoreLabel | Text shown on the load more component | string | "Load More" |
6262
hideBadge | Toggle to hide or show the badge | boolean | false |
6363
darkMode | Toggle to enable dark mode | boolean | false |
6464
itemsPerFetch | Number of notifications fetch per api request (have a max cap of 50) | number | 20 |
6565
windowViewOnly | Toggle to enable fit-to-screen window or modal view | boolean | false |
6666
notificationIcon | Option to use custom notification Icon | JSX Element | null |
67-
cardProps | Props for customizing the notification cards | { hideAvatar: boolean } | { hideAvatar: false } |
67+
inboxHeaderProps | Props for customizing the header.<br> title - Title of the notification inbox<br> hideHeader - Toggle to hide or show the header section.<br> hideClearAll - Toggle to hide or show the clear all button.<br> customHeader - Custom header component. | InboxHeaderProps| { title: 'Notifications', <br>hideHeader: false,<br> hideClearAll: false, <br>customHeader: null } |
68+
cardProps | Props for customizing the notification cards. <br>hideDelete - Toggle to hide or show delete icon<br> hideAvatar - Toggle to hide or show the avatar.<br> disableAutoMarkAsRead - Toggle to disable or enable the markAsRead functionality on card click | CardProps | { hideDelete: false,<br> hideAvatar: false,<br> disableAutoMarkAsRead: false } |
6869
customNotificationCard | Function for rendering custom notification cards | (notification)=> JSX Element | null |
6970
onNotificationCardClick | Custom click handler for notification cards | (notification)=> void | ()=>null |
7071
listEmptyComponent | Custom component for empty notification list | JSX Element | null |
71-
customHeader | Custom header component | JSX Element | null |
7272
customFooter | Custom footer component | JSX Element | null |
7373
customLoader | Custom loader component | JSX Element | null |
7474
loadMoreComponent | Custom load more component | JSX Element | null |
7575
customErrorWindow | Custom error window | JSX Element | null |
7676
onError | Callback for handling errors | (error: SirenErrorType)=> void | null |
7777

7878
## 3. Customization
79+
7980
### 3.1 Themes
8081

8182
Here are the available theme options:
@@ -101,7 +102,7 @@ type ThemeProps = {
101102
timerIcon?: string,
102103
clearAllIcon?: string,
103104
infiniteLoader?: string,
104-
windowShadowColor?: string
105+
windowShadowColor?: string,
105106
},
106107
badgeStyle?: {
107108
color?: string,
@@ -128,9 +129,10 @@ type ThemeProps = {
128129
loadMoreButton: {
129130
color?: string,
130131
background?: string,
131-
}
132+
},
132133
};
133134
```
135+
134136
### 3.2 Style options
135137

136138
Here are the custom style options for the notification inbox
@@ -187,6 +189,27 @@ Please note that the badgeStyle, window shadow and border props are only applica
187189
}
188190
```
189191

192+
#### CardProps
193+
194+
```js
195+
type CardProps = {
196+
hideDelete?: boolean;
197+
hideAvatar?: boolean,
198+
disableAutoMarkAsRead?: boolean,
199+
};
200+
```
201+
202+
#### InboxHeaderProps
203+
204+
```js
205+
type InboxHeaderProps = {
206+
title?: string;
207+
hideHeader?: boolean,
208+
hideClearAll?: boolean,
209+
customHeader?: JSX.Element | null,
210+
};
211+
```
212+
190213
## 4. Hooks
191214

192215
`useSiren` is a hook that provides utility functions for modifying notifications.
@@ -229,43 +252,41 @@ function MyComponent() {
229252
}
230253
```
231254

232-
#### useSiren functions
255+
### useSiren functions
233256

234-
| Functions | Parameters | Type | Description |
235-
| ----------------------------- | ----------------- |---------| ----------------------------------------------------------- |
236-
| markNotificationsAsReadByDate | startDate | ISO date string | Sets the read status of notifications to true until the given date. |
237-
| markAsRead | id | string | Set read status of a notification to true |
238-
| deleteNotification | id | string | Delete a notification by id |
239-
| deleteNotificationsByDate | startDate | ISO date string| Delete all notifications until given date |
240-
| markNotificationsAsViewed | startDate | ISO date string | Sets the viewed status of notifications to true until the given date |
257+
Functions | Parameters | Type | Description |
258+
----------|------------|-------|------------|
259+
markNotificationsAsReadByDate | startDate | ISO date string | Sets the read status of notifications to true until the given date |
260+
markAsRead | id | string | Set read status of a notification to true |
261+
deleteNotification | id | string | Delete a notification by id |
262+
deleteNotificationsByDate | startDate | ISO date string | Delete all notifications until given date |
263+
markNotificationsAsViewed | startDate | ISO date string |Sets the viewed status of notifications to true until the given date |
241264

242265
## 5. Error codes
243266

244267
Given below are all possible error codes thrown by sdk.
245268

246-
| Error code | Description |
247-
| ------------------------- | ------------------------------------------------------------------|
248-
| INVALID_TOKEN | The token passed in the provider is invalid |
249-
| INVALID_RECIPIENT_ID | The recipient id passed in the provider is invalid |
250-
| TOKEN_VERIFICATION_FAILED | Verification of the given tokens has failed |
251-
| GENERIC_API_ERROR | Occurrence of an unexpected api error |
252-
| OUTSIDE_SIREN_CONTEXT | Attempting to invoke the functions outside the siren inbox context|
253-
| MISSING_PARAMETER | The required parameter is missing |
269+
Error code | Description |
270+
------------------------- | ------------------------------------------------------------------|
271+
INVALID_TOKEN | The token passed in the provider is invalid |
272+
INVALID_RECIPIENT_ID | The recipient id passed in the provider is invalid |
273+
TOKEN_VERIFICATION_FAILED | Verification of the given tokens has failed |
274+
GENERIC_API_ERROR | Occurrence of an unexpected api error |
275+
OUTSIDE_SIREN_CONTEXT | Attempting to invoke the functions outside the siren inbox context|
276+
MISSING_PARAMETER | The required parameter is missing |
254277

255278
## Example
256279

257280
Here's a basic example to help you get started
258281

259282
```js
260-
261-
import React from 'react';
262-
import {SirenInbox,SirenProvider} from '@sirenapp/react-inbox';
283+
import React from "react";
284+
import { SirenInbox, SirenProvider } from "@sirenapp/react-inbox";
263285

264286
function App(): React.JSX.Element {
265-
266287
const config = {
267-
userToken: 'your_user_token',
268-
recipientId: 'your_recipient_id',
288+
userToken: "your_user_token",
289+
recipientId: "your_recipient_id",
269290
};
270291

271292
return (
@@ -278,15 +299,21 @@ function App(): React.JSX.Element {
278299
export default App;
279300

280301
export function MyContainer(): React.JSX.Element {
281-
282302
return (
283303
<div>
284304
<SirenInbox
285-
title="Notifications"
286-
hideHeader={false}
305+
inboxHeaderProps={
306+
title: "Notifications",
307+
hideHeader: false
308+
}
287309
darkMode={false}
288-
cardProps={{hideAvatar: false}}
310+
cardProps={{
311+
hideDelete: false,
312+
hideAvatar: false,
313+
disableAutoMarkAsRead: false,
314+
}}
289315
/>
290316
</div>
291317
);
292-
}
318+
}
319+
```

example/src/App.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const App: React.FC = () => {
3737
const [showCustomNotificationCard, setShowCustomNotificationCard] =
3838
useState(false);
3939

40-
const { markNotificationsAsReadByDate, markAsRead } = useSiren();
40+
const { markNotificationsAsReadByDate } = useSiren();
4141

4242
const renderListEmpty = () => {
4343
return (
@@ -248,25 +248,26 @@ const App: React.FC = () => {
248248

249249
return (
250250
<div>
251-
<SirenInbox
252-
title="Siren Notifications"
253-
hideHeader={hideHeader}
251+
<SirenInbox
252+
inboxHeaderProps={{
253+
title:"Siren Notifications",
254+
hideHeader: hideHeader,
255+
customHeader: showCustomHeader ? renderCustomHeader() : undefined
256+
}}
254257
darkMode={sdkDarkModeEnabled}
255258
cardProps={{ hideAvatar: hideAvatar, showMedia: true }}
256259
theme={windowThemes[windowThemeIndex]}
257260
customFooter={showCustomFooter ? renderCustomFooter() : undefined}
258261
listEmptyComponent={
259262
showCustomEmptyComponent ? renderListEmpty() : undefined
260263
}
261-
customHeader={showCustomHeader ? renderCustomHeader() : undefined}
262264
customNotificationCard={
263265
showCustomNotificationCard
264266
? (notification: any) => renderCustomNotificationCard(notification)
265267
: undefined
266268
}
267-
onNotificationCardClick={(notification: { id: any; }) => {
269+
onNotificationCardClick={() => {
268270
console.log("click on notification");
269-
markAsRead(notification.id);
270271
}}
271272
onError={(error: any) => {
272273
console.log(`error: ${error}`);

src/components/Card.tsx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import defaultAvatarLight from "../assets/light/defaultAvatarLight.png";
88
import type { NotificationCardProps } from "../types";
99
import { generateElapsedTimeText } from "../utils/commonUtils";
1010
import "../styles/card.css";
11+
import useSiren from "../utils/sirenHook";
1112

1213
/**
1314
* Card component represents an individual notification card in the notification list.
@@ -51,7 +52,10 @@ const Card: FC<NotificationCardProps> = ({
5152
}) => {
5253
const { id, createdAt, message, isRead } = notification;
5354
const { avatar, header, subHeader, body } = message;
54-
55+
const { hideAvatar, hideDelete, disableAutoMarkAsRead } = cardProps ?? {};
56+
const {
57+
markAsRead
58+
} = useSiren();
5559
const onDelete = (event: React.MouseEvent) => {
5660
deleteNotificationById(id);
5761
event.stopPropagation();
@@ -69,20 +73,23 @@ const Card: FC<NotificationCardProps> = ({
6973
backgroundColor: styles.activeCardMarker.backgroundColor,
7074
};
7175

76+
const handleNotificationCardClick = () => {
77+
onNotificationCardClick && onNotificationCardClick(notification);
78+
!disableAutoMarkAsRead && markAsRead(notification.id);
79+
}
80+
7281
return (
7382
<div
7483
style={cardContainerStyle}
7584
className={`${
76-
cardProps?.hideAvatar
85+
hideAvatar
7786
? "siren-sdk-hide-avatar-card-container"
7887
: "siren-sdk-card-container"
7988
}`}
80-
onClick={() =>
81-
onNotificationCardClick && onNotificationCardClick(notification)
82-
}
89+
onClick={handleNotificationCardClick}
8390
data-testid={`card-${notification.id}`}
8491
>
85-
{!cardProps?.hideAvatar && (
92+
{!hideAvatar && (
8693
<div
8794
style={{
8895
...styles.cardIconRound,
@@ -118,16 +125,18 @@ const Card: FC<NotificationCardProps> = ({
118125
</div>
119126
</div>
120127
</div>
121-
<div
122-
data-testid={`delete-${notification.id}`}
123-
className="siren-sdk-delete-button"
124-
onClick={onDelete}
125-
>
126-
<CloseIcon
127-
color={styles?.deleteIcon.color}
128-
size={styles.deleteIcon.size}
129-
/>
130-
</div>
128+
{!hideDelete && (
129+
<div
130+
data-testid={`delete-${notification.id}`}
131+
className="siren-sdk-delete-button"
132+
onClick={onDelete}
133+
>
134+
<CloseIcon
135+
color={styles?.deleteIcon.color}
136+
size={styles.deleteIcon.size}
137+
/>
138+
</div>
139+
)}
131140
</div>
132141
);
133142
};

src/components/Loader.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import React from "react";
1+
import React, { type FC } from "react";
22

33
import "../styles/loader.css";
4-
import type { SirenStyleProps } from "../types";
4+
import type { LoaderProps } from "../types";
55

6-
const Loader = ({styles}: {styles: SirenStyleProps}) => {
6+
const Loader : FC<LoaderProps> = ({
7+
hideAvatar,
8+
styles,
9+
10+
}) => {
711
return (
812
<div className="siren-sdk-skeleton-container">
9-
<div className="siren-sdk-skeleton-grid">
10-
<div className="siren-sdk-skeleton-avatar siren-sdk-skeleton" style={styles.loader} />
13+
<div className={`${!hideAvatar ? 'siren-sdk-skeleton-grid-with-avatar' : 'siren-sdk-skeleton-grid-without-avatar' }`}>
14+
{!hideAvatar && (<div className="siren-sdk-skeleton-avatar siren-sdk-skeleton" style={styles.loader} />)}
1115
<div className="siren-sdk-skeleton-head siren-sdk-skeleton" style={styles.loader} />
1216
<div className="siren-sdk-skeleton-subtitle siren-sdk-skeleton" style={styles.loader} />
1317
<div className="siren-sdk-skeleton-body siren-sdk-skeleton" style={styles.loader}/>

0 commit comments

Comments
 (0)