@@ -36,39 +36,24 @@ const useStyles = makeStyles((theme) => ({
36
36
}
37
37
} ) ) ;
38
38
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 ( ) ) ;
47
40
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 ( ) ;
61
42
62
43
const NotificationPanel = ( { isOpen, onClose, anchorRef, setNewMessages } ) => {
63
44
const classes = useStyles ( ) ;
64
45
const [ notifications , setNotifications ] = useState ( [ ] ) ;
65
46
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
+ } ) ;
72
57
} , [ setNewMessages ] ) ;
73
58
74
59
return (
0 commit comments