Skip to content

Commit

Permalink
console: Use useEffect to update notification status
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Feb 5, 2024
1 parent b5be1f7 commit dd66648
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
20 changes: 20 additions & 0 deletions pkg/webui/console/containers/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
selectArchivedNotificationsTotalCount,
selectInboxNotifications,
selectInboxNotificationsTotalCount,
selectTotalUnseenCount,
} from '@console/store/selectors/notifications'

import NotificationList from './notification-list'
Expand Down Expand Up @@ -66,6 +67,7 @@ const Notifications = React.memo(() => {
const totalCount = useSelector(
showArchived ? selectArchivedNotificationsTotalCount : selectInboxNotificationsTotalCount,
)
const totalUnseenCount = useSelector(selectTotalUnseenCount)
const hasNextPage = items.length < totalCount
const [fetching, setFetching] = useState(false)
const [selectionCache, setSelectionCache] = useState({ archived: undefined, inbox: undefined })
Expand Down Expand Up @@ -164,6 +166,24 @@ const Notifications = React.memo(() => {
}
}, [category, isSmallScreen, items, navigate, notificationId, selectionCache])

// Update the status of the selected notification to seen.
useEffect(() => {
const clickedNotification = items.find(notification => notification.id === notificationId)
const index = items.findIndex(notification => notification.id === notificationId)
const timer = setTimeout(() => {
if (category === 'inbox' && !('status' in clickedNotification) && totalUnseenCount > 0) {
dispatch(
attachPromise(
updateNotificationStatus([clickedNotification.id], 'NOTIFICATION_STATUS_SEEN'),
),
)
loadNextPage(index, index + 1)
}
}, 500)

return () => clearTimeout(timer)
}, [category, items, dispatch, loadNextPage, totalUnseenCount, notificationId])

const selectedNotification = items?.find(item => item.id === notificationId)

if (!items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import attachPromise from '@ttn-lw/lib/store/actions/attach-promise'
import PropTypes from '@ttn-lw/lib/prop-types'
import sharedMessages from '@ttn-lw/lib/shared-messages'

import { markAllAsSeen, updateNotificationStatus } from '@console/store/actions/notifications'
import { markAllAsSeen } from '@console/store/actions/notifications'

import { selectTotalUnseenCount } from '@console/store/selectors/notifications'

Expand Down Expand Up @@ -64,18 +64,6 @@ const NotificationList = ({
// Otherwise, if totalCount is 0, it means the list is empty and we should not have a total count.
const itemCount = totalCount >= 0 ? totalCount : 100

const handleClick = useCallback(
async (_, id) => {
const clickedNotification = items.find(notification => notification.id === id)
const index = items.findIndex(notification => notification.id === id)
if (!isArchive && !('status' in clickedNotification) && totalUnseenCount > 0) {
await dispatch(attachPromise(updateNotificationStatus([id], 'NOTIFICATION_STATUS_SEEN')))
loadNextPage(index, index + 1)
}
},
[items, dispatch, isArchive, totalUnseenCount, loadNextPage],
)

const handleMarkAllAsSeen = useCallback(async () => {
if (totalUnseenCount > 0) {
const result = await dispatch(attachPromise(markAllAsSeen()))
Expand Down Expand Up @@ -103,7 +91,6 @@ const NotificationList = ({
notification={items[index]}
isSelected={isSelected(items[index])}
isNextSelected={isNextSelected(items[index])}
onClick={handleClick}
/>
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { selectTotalUnseenCount } from '@console/store/selectors/notifications'

import style from '../notifications.styl'

export const NotificationListItem = ({ notification, isSelected, isNextSelected, onClick }) => {
export const NotificationListItem = ({ notification, isSelected, isNextSelected }) => {
const { category } = useParams()
const totalUnseenCount = useSelector(selectTotalUnseenCount)
const showUnseenStatus = !notification.status && totalUnseenCount > 0
Expand All @@ -47,7 +47,6 @@ export const NotificationListItem = ({ notification, isSelected, isNextSelected,
to={`/notifications/${category}/${notification.id}`}
className={classes}
data-test-id="notification-list-item"
onClick={onClick}
value={notification.id}
>
{showUnseenStatus && <Status pulse={false} status="good" className={style.unseenMark} />}
Expand Down Expand Up @@ -92,7 +91,6 @@ NotificationListItem.propTypes = {
notification_type: PropTypes.string,
status: PropTypes.string,
}).isRequired,
onClick: PropTypes.func.isRequired,
}

NotificationListItem.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const GeneralSideNavigation = () => {
)}
<SideNavigation.Item
title={sharedMessages.notifications}
path="/notifications/inbox"
path="/notifications"
icon="inbox"
/>
{mayHandleApiKeys && (
Expand Down
2 changes: 1 addition & 1 deletion pkg/webui/console/store/middleware/logics/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const m = defineMessage({

const updateThroughPagination = async (totalCount, userId) => {
let page = 1
const limit = 100
const limit = 1000
let result = []

while ((page - 1) * limit < totalCount) {
Expand Down

0 comments on commit dd66648

Please sign in to comment.