Skip to content

Commit

Permalink
console: Update state periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Feb 2, 2024
1 parent f1f1f3b commit a3950e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
16 changes: 8 additions & 8 deletions pkg/webui/console/store/actions/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ export const [
limit,
}))

export const GET_UNSEEN_NOTIFICATIONS_BASE = 'GET_UNSEEN_NOTIFICATIONS'
export const GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_BASE = 'GET_UNSEEN_NOTIFICATIONS_PERIODICALLY'
export const [
{
request: GET_UNSEEN_NOTIFICATIONS,
success: GET_UNSEEN_NOTIFICATIONS_SUCCESS,
failure: GET_UNSEEN_NOTIFICATIONS_FAILURE,
request: GET_UNSEEN_NOTIFICATIONS_PERIODICALLY,
success: GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_SUCCESS,
failure: GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_FAILURE,
},
{
request: getUnseenNotifications,
success: getUnseenNotificationsSuccess,
failure: getUnseenNotificationsFailure,
request: getUnseenNotificationsPeriodically,
success: getUnseenNotificationsPeriodicallySuccess,
failure: getUnseenNotificationsPeriodicallyFailure,
},
] = createRequestActions(GET_UNSEEN_NOTIFICATIONS_BASE, () => ({}))
] = createRequestActions(GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_BASE, () => ({}))

export const UPDATE_NOTIFICATION_STATUS_BASE = 'UPDATE_NOTIFICATION_STATUS'
export const [
Expand Down
7 changes: 5 additions & 2 deletions pkg/webui/console/store/middleware/logics/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import { TokenError } from '@ttn-lw/lib/errors/custom-errors'
import { isPermissionDeniedError, isUnauthenticatedError } from '@ttn-lw/lib/errors/utils'

import * as user from '@console/store/actions/logout'
import { getInboxNotifications, getUnseenNotifications } from '@console/store/actions/notifications'
import {
getInboxNotifications,
getUnseenNotificationsPeriodically,
} from '@console/store/actions/notifications'

const consoleAppLogic = createRequestLogic({
type: init.INITIALIZE,
Expand Down Expand Up @@ -67,7 +70,7 @@ const consoleAppLogic = createRequestLogic({
userResult.isAdmin = info.is_admin || false
dispatch(user.getUserMeSuccess(userResult))
dispatch(getInboxNotifications({ page: 1, limit: 3 }))
dispatch(getUnseenNotifications(userId))
dispatch(getUnseenNotificationsPeriodically())
} catch (error) {
dispatch(user.getUserMeFailure(error))
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/webui/console/store/middleware/logics/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,34 @@ const getArchivedNotificationsLogic = createRequestLogic({
})

const getUnseenNotificationsPeriodicallyLogic = createRequestLogic({
type: notifications.GET_UNSEEN_NOTIFICATIONS,
process: async ({ getState }) => {
type: notifications.GET_UNSEEN_NOTIFICATIONS_PERIODICALLY,
process: async ({ getState }, dispatch) => {
clearInterval()
const id = selectUserId(getState())
const result = await tts.Notifications.getAllNotifications(id, ['NOTIFICATION_STATUS_UNSEEN'])
let totalCount = result.totalCount

setInterval(async () => {
const totalCount = selectTotalUnseenCount(getState())
const newResult = await tts.Notifications.getAllNotifications(id, [
'NOTIFICATION_STATUS_UNSEEN',
])

if (newResult.totalCount > totalCount) {
dispatch(
notifications.getUnseenNotificationsPeriodicallySuccess({
totalCount: newResult.totalCount,
}),
)
toast({ message: m.newNotifications, type: toast.types.INFO })
}
totalCount = newResult.totalCount
}, [1000 * 60 * 5]) // 5 minutes

dispatch(
notifications.getUnseenNotificationsPeriodicallySuccess({
totalCount: result.totalCount,
}),
)

return { notifications: result.notifications, totalCount: result.totalCount }
},
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/webui/console/store/reducers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import {
GET_ARCHIVED_NOTIFICATIONS_SUCCESS,
GET_INBOX_NOTIFICATIONS_SUCCESS,
GET_UNSEEN_NOTIFICATIONS_SUCCESS,
GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_SUCCESS,
MARK_ALL_AS_SEEN_SUCCESS,
UPDATE_NOTIFICATION_STATUS_SUCCESS,
} from '@console/store/actions/notifications'
Expand Down Expand Up @@ -80,7 +80,7 @@ const notifications = (state = defaultState, { type, payload }) => {
},
},
}
case GET_UNSEEN_NOTIFICATIONS_SUCCESS:
case GET_UNSEEN_NOTIFICATIONS_PERIODICALLY_SUCCESS:
return {
...state,
unseenTotalCount: payload.totalCount,
Expand Down

0 comments on commit a3950e5

Please sign in to comment.