Skip to content

Commit

Permalink
fix: state bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lauti7 committed Dec 8, 2023
1 parent 758c8e9 commit 24a22fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
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 24a22fe

Please sign in to comment.