Skip to content

Commit dcdc619

Browse files
committed
Added API driven notifications panel.
1 parent 0f32950 commit dcdc619

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

packages/react-renderer-demo/src/app/src/components/layout.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import ConnectedLinks from './common/connected-links';
2121
import Footer from './footer';
2222

2323
import dynamic from 'next/dynamic';
24-
import NotificationPanel, { lastMessageId, oldestMessageId } from './notification-panel';
24+
import NotificationPanel from './notification-panel';
2525
const DocSearch = dynamic(import('./docsearch'), {
2626
ssr: false
2727
});
@@ -126,16 +126,12 @@ const Layout = ({ children }) => {
126126
const searchRef = useRef(null);
127127
const anchorRef = useRef(null);
128128
const [openNotification, setOpenNotifiation] = useState(false);
129-
const [lastCheck, setLastCheck] = useState(lastMessageId);
129+
const [newMessages, setNewMessages] = useState(0);
130130

131131
useEffect(() => {
132132
setLinks(findConnectedLinks(router.asPath, flatSchema) || {});
133133
}, [router.asPath]);
134134

135-
useEffect(() => {
136-
setLastCheck(localStorage.getItem('data-driven-forms-last-checked') || '');
137-
}, []);
138-
139135
const handleDrawerOpen = () => {
140136
setOpen(true);
141137
setTimeout(() => searchRef.current.focus(), 500);
@@ -145,9 +141,8 @@ const Layout = ({ children }) => {
145141
setOpen(false);
146142
}
147143

148-
const handleToggle = () => {
149-
localStorage.setItem('data-driven-forms-last-checked', lastMessageId);
150-
setLastCheck(lastMessageId);
144+
const handleToggle = (notificationDates) => {
145+
setNewMessages(0);
151146
setOpenNotifiation(!openNotification);
152147
};
153148

@@ -189,11 +184,16 @@ const Layout = ({ children }) => {
189184
>
190185
<DocSearch />
191186
<IconButton aria-label="show new notifications" onClick={handleToggle} color="inherit" ref={anchorRef} className={clsx(classes.menuButton)}>
192-
<Badge badgeContent={lastMessageId - Math.max(lastCheck, oldestMessageId)} color="secondary">
187+
<Badge badgeContent={newMessages} color="secondary">
193188
<NotificationsIcon className={classes.menuIcons} />
194189
</Badge>
195190
</IconButton>
196-
<NotificationPanel isOpen={openNotification} anchorRef={anchorRef} onClose={() => setOpenNotifiation(false)} />
191+
<NotificationPanel
192+
setNewMessages={setNewMessages}
193+
isOpen={openNotification}
194+
anchorRef={anchorRef}
195+
onClose={() => setOpenNotifiation(false)}
196+
/>
197197
<a href="https://github.com/data-driven-forms/react-forms" rel="noopener noreferrer" target="_blank">
198198
<IconButton color="inherit" aria-label="gh repository" edge="start" className={clsx(classes.menuButton)}>
199199
<SvgIcon>

packages/react-renderer-demo/src/app/src/components/notification-panel.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import PropTypes from 'prop-types';
33
import Popper from '@material-ui/core/Popper';
44
import Grow from '@material-ui/core/Grow';
@@ -21,22 +21,6 @@ const options = {
2121
}
2222
};
2323

24-
const messageList = [
25-
{
26-
id: '1',
27-
date: 'April 8 2020',
28-
title: 'Version 2 released',
29-
text: `Version 2 of Data Driven Forms has been released.<br /><br />
30-
Check what is new in [migration guide](/migration-guide)!<br /><br />
31-
If you need documenation for version 1, please use v1 [branch on GitHub](https://github.com/data-driven-forms/react-forms/tree/v1/packages/react-renderer-demo/src/app/pages)
32-
or visit our [backup link](https://pokus-next.firebaseapp.com/)!
33-
`
34-
}
35-
];
36-
37-
export const lastMessageId = messageList[0].id;
38-
export const oldestMessageId = messageList[messageList.length - 1].id - 1;
39-
4024
const useStyles = makeStyles((theme) => ({
4125
paper: {
4226
transformOrigin: 'top right'
@@ -52,8 +36,26 @@ const useStyles = makeStyles((theme) => ({
5236
}
5337
}));
5438

55-
const NotificationPanel = ({ isOpen, onClose, anchorRef }) => {
39+
const getNotifications = () => {
40+
const query = `?orderBy="expired-at"&startAt="${new Date().toISOString()}"&limitToFirst=10`;
41+
return fetch(`https://data-driven-forms.firebaseio.com/notifications.json${query}`)
42+
.then((data) => data.json())
43+
.then((data) => data || []);
44+
};
45+
46+
const createNotificationId = (notification) => `${notification['created-at']}-${notification['expired-at']}`;
47+
48+
const NotificationPanel = ({ isOpen, onClose, anchorRef, setNewMessages }) => {
5649
const classes = useStyles();
50+
const [notifications, setNotifications] = useState([]);
51+
useEffect(() => {
52+
getNotifications().then((data = []) => {
53+
const lastSeen = JSON.parse(localStorage.getItem('data-driven-forms-last-seen') || '[]');
54+
setNewMessages(data.filter((notification) => !lastSeen.includes(createNotificationId(notification))).length);
55+
localStorage.setItem('data-driven-forms-last-seen', JSON.stringify(data.map(createNotificationId)));
56+
setNotifications(data);
57+
});
58+
}, [setNewMessages]);
5759

5860
return (
5961
<Popper id="notifications-popup" open={isOpen} anchorEl={anchorRef.current} placement="bottom-end" transition disablePortal role={undefined}>
@@ -62,17 +64,17 @@ const NotificationPanel = ({ isOpen, onClose, anchorRef }) => {
6264
<Grow in={isOpen} {...TransitionProps}>
6365
<Paper className={classes.paper}>
6466
<List className={classes.list}>
65-
{messageList.map((message, index) => (
66-
<React.Fragment key={message.id}>
67+
{notifications.map((message, index) => (
68+
<React.Fragment key={index}>
6769
<ListItem alignItems="flex-start" className={classes.listItem}>
68-
{message.date && <ListItemText secondary={message.date} />}
70+
{message['created-at'] && <ListItemText secondary={new Date(message['created-at']).toDateString()} />}
6971
<ListItemText
7072
primary={message.title}
71-
secondary={<Markdown options={options}>{message.text}</Markdown>}
73+
secondary={<Markdown options={options}>{message.content}</Markdown>}
7274
secondaryTypographyProps={{ color: 'textPrimary', component: 'div' }}
7375
/>
7476
</ListItem>
75-
{index < messageList.length - 1 ? <Divider variant="middle" /> : null}
77+
{index < notifications.length - 1 ? <Divider variant="middle" /> : null}
7678
</React.Fragment>
7779
))}
7880
</List>
@@ -87,6 +89,7 @@ const NotificationPanel = ({ isOpen, onClose, anchorRef }) => {
8789
NotificationPanel.propTypes = {
8890
isOpen: PropTypes.bool.isRequired,
8991
onClose: PropTypes.func.isRequired,
92+
setNewMessages: PropTypes.func.isRequired,
9093
anchorRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({ current: PropTypes.any })]).isRequired
9194
};
9295

0 commit comments

Comments
 (0)