Skip to content

Commit 2d88d7a

Browse files
authored
Merge pull request #604 from data-driven-forms/migrate-to-firestore
migrate docs to use firestore
2 parents de1e3bc + 0622707 commit 2d88d7a

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

packages/react-renderer-demo/firebaseFunctions.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const functions = require('firebase-functions');
2+
const admin = require('firebase-admin');
23
const next = require('next');
34
const { join } = require('path');
45
const { parse } = require('url');
@@ -13,12 +14,40 @@ const nextjsServer = next({
1314
});
1415
const nextjsHandle = nextjsServer.getRequestHandler();
1516

17+
admin.initializeApp();
18+
19+
const db = admin.firestore();
20+
1621
exports.nextjsFunc = functions.https.onRequest((req, res) => {
1722
console.log('File: ' + req.originalUrl); // eslint-disable-line no-console
1823
return nextjsServer.prepare().then(() => {
1924
const parsedUrl = parse(req.url, true);
2025
const { pathname } = parsedUrl;
2126

27+
if (pathname === '/notifications') {
28+
const endDate = Number(req.query.end);
29+
return db
30+
.collection('notifications')
31+
.doc('notifications')
32+
.get()
33+
.then((snapshot) => {
34+
const data = snapshot
35+
.data()
36+
.notifications.filter(({ active_till: activeTill }) => {
37+
const dueDate = activeTill.toDate();
38+
return dueDate.getTime() >= endDate;
39+
})
40+
.map(({ 'created-at': createdAt, active_till: activeTill, ...notification }) => ({
41+
...notification,
42+
activeTill: activeTill.toDate(),
43+
'created-at': createdAt ? createdAt.toDate : undefined // eslint-disable-line camelcase
44+
}));
45+
res.status(200).json(data);
46+
res.finished = true;
47+
return res.end();
48+
});
49+
}
50+
2251
// handle GET request to /service-worker.js
2352
if (pathname === '/service-worker.js') {
2453
const filePath = join(__dirname, '.next', pathname);

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,24 @@ const useStyles = makeStyles((theme) => ({
3636
}
3737
}));
3838

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) => {
44-
if (!data) {
45-
return [];
46-
}
39+
const getNotifications = () => fetch(`/notifications?end=${Date.now()}`).then((data) => data.json());
4740

48-
if (typeof data !== 'object') {
49-
return [];
50-
}
51-
52-
if (!Array.isArray(data)) {
53-
data = Object.values(data);
54-
}
55-
56-
return data.filter(Boolean).sort((a, b) => b['created-at'].localeCompare(a['created-at']));
57-
});
58-
};
59-
60-
const createNotificationId = (notification) => `${notification['created-at']}-${notification['expired-at']}`;
41+
const createNotificationId = (notification) => notification.activeTill.toString();
6142

6243
const NotificationPanel = ({ isOpen, onClose, anchorRef, setNewMessages }) => {
6344
const classes = useStyles();
6445
const [notifications, setNotifications] = useState([]);
6546
useEffect(() => {
66-
getNotifications().then((data = []) => {
67-
const lastSeen = JSON.parse(localStorage.getItem('data-driven-forms-last-seen') || '[]');
68-
setNewMessages(data.filter((notification) => !lastSeen.includes(createNotificationId(notification))).length);
69-
localStorage.setItem('data-driven-forms-last-seen', JSON.stringify(data.map(createNotificationId)));
70-
setNotifications(data);
71-
});
47+
getNotifications()
48+
.then((data = []) => {
49+
const lastSeen = JSON.parse(localStorage.getItem('data-driven-forms-last-seen') || '[]');
50+
setNewMessages(data.filter((notification) => !lastSeen.includes(createNotificationId(notification))).length);
51+
localStorage.setItem('data-driven-forms-last-seen', JSON.stringify(data.map(createNotificationId)));
52+
setNotifications(data);
53+
})
54+
.catch(() => {
55+
setNotifications([]);
56+
});
7257
}, [setNewMessages]);
7358

7459
return (

0 commit comments

Comments
 (0)