@@ -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
6243const 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