Skip to content

Commit

Permalink
fix: state and timestamp bugs (#528)
Browse files Browse the repository at this point in the history
* fix: state bugs

* chore: libs
  • Loading branch information
lauti7 authored Dec 8, 2023
1 parent 758c8e9 commit fb4152d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
25 changes: 7 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
"decentraland-connect": "^5.1.0",
"decentraland-crypto-fetch": "^2.0.1",
"decentraland-transactions": "^1.47.0",
"decentraland-ui": "^4.30.2",
"decentraland-ui": "^4.30.3",
"ethers": "^5.6.8",
"events": "^3.3.0",
"flat": "^5.0.2",
"pusher-js": "^8.0.1",
"radash": "7.1.0",
"react-intl": "^5.20.7",
"redux-persistence": "^1.2.0",
"redux-persistence-engine-localstorage": "^1.0.0",
Expand Down
23 changes: 5 additions & 18 deletions src/containers/UserInformation/UserInformation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { diff } from 'radash/dist/array'
import {
MenuItemType,
UserInformationContainer as UserMenuComponent
Expand All @@ -16,7 +15,7 @@ import {
DROPDOWN_MENU_SIGN_IN_EVENT,
DROPDOWN_MENU_SIGN_OUT_EVENT
} from './constants'
import { NotificationsAPI, checkIsOnboarding, parseNotification} from '../../modules/notifications'
import { NotificationsAPI, checkIsOnboarding} from '../../modules/notifications'

const NOTIFICATIONS_QUERY_INTERVAL = 60000;

Expand Down Expand Up @@ -130,17 +129,10 @@ export const UserInformation = (props: UserInformationProps) => {
}, [analytics, onSignIn])

const fetchNotificationsState = () => {
setUserNotifications({ notifications, isLoading: true })
setUserNotifications({ notifications: [], isLoading: true })
client.getNotifications()
.then((response) => {
const parsed = response.notifications.map(parseNotification)

// check if needed to update notifications state
if(diff(notifications, parsed).length) {
setUserNotifications({ isLoading: false, notifications: parsed })
} else {
setUserNotifications({ notifications, isLoading: false })
}
.then((retrievedNotifications) => {
setUserNotifications({ isLoading: false, notifications: retrievedNotifications })
})
}

Expand All @@ -160,12 +152,7 @@ export const UserInformation = (props: UserInformationProps) => {
useEffect(() => {
if (identity && withNotifications) {
fetchNotificationsState()
}
}, [identity])


useEffect(() => {
if (identity && withNotifications) {
const interval = setInterval(() => {
fetchNotificationsState()
}, NOTIFICATIONS_QUERY_INTERVAL)
Expand All @@ -176,7 +163,7 @@ export const UserInformation = (props: UserInformationProps) => {
} else {
return () => {}
}
}, [identity, notifications])
}, [identity])

return (
<UserMenuComponent
Expand Down
9 changes: 5 additions & 4 deletions src/modules/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export class NotificationsAPI extends BaseClient {
params.append("from", `${from}`)
}

const notificationsResponse = await this.fetch<{notifications: Array<DCLNotification>}>(`/notifications${params.toString().length ? `?${params.toString()}` : ''}`)
const { notifications } = await this
.fetch<{notifications: Array<DCLNotification>}>(`/notifications${params.toString().length ? `?${params.toString()}` : ''}`)

return notificationsResponse
return notifications.map(parseNotification)
}

async markNotificationsAsRead(ids: string[]) {
Expand All @@ -48,9 +49,9 @@ export const checkIsOnboarding = () => {
}
}

export const parseNotification = (notification: DCLNotification): DCLNotification => {
const parseNotification = (notification: DCLNotification): DCLNotification => {
return ({
...notification,
timestamp: Number(notification.timestamp) * 1000
timestamp: Number(notification.timestamp)
})
}

0 comments on commit fb4152d

Please sign in to comment.