Skip to content

Commit

Permalink
Handle deletion of all notifications in the last page (#5866)
Browse files Browse the repository at this point in the history
  • Loading branch information
HelNershingThapa committed Jun 2, 2023
1 parent 22df4e3 commit 16f829b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
23 changes: 20 additions & 3 deletions frontend/src/components/notifications/actionButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const ActionButtons = ({
unreadCountInSelected,
isAllSelected,
inboxQuery,
setInboxQuery,
updateUnreadCount,
pageOfCards,
totalPages,
}) => {
const dispatch = useDispatch();
const token = useSelector((state) => state.auth.token);
Expand All @@ -31,7 +34,21 @@ export const ActionButtons = ({
});
} else {
pushToLocalJSONAPI(`/api/v2/notifications/delete-multiple/`, payload, token, 'DELETE')
.then(() => handleSuccess())
.then(() => {
// Decrement the page query if the user deleted all notifications in the last page
if (inboxQuery.page === totalPages && selected.length === pageOfCards.length) {
setInboxQuery(
{
...inboxQuery,
page: inboxQuery.page - 1 || undefined,
},
'pushIn',
);
handleSuccess(false);
return;
}
handleSuccess();
})
.catch((e) => {
console.log(e.message);
});
Expand All @@ -54,9 +71,9 @@ export const ActionButtons = ({
}
};

function handleSuccess() {
function handleSuccess(shouldRetry = true) {
setSelected([]);
retryFn();
shouldRetry && retryFn();
isAllSelected
? updateUnreadCount()
: // The decrement count is readily available; deducting count from selected
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/components/notifications/notificationResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const NotificationResults = ({
liveUnreadCount,
setPopoutFocus,
inboxQuery,
setInboxQuery,
}) => {
const stateNotifications = notifications.userMessages;

Expand Down Expand Up @@ -82,7 +83,9 @@ export const NotificationResults = ({
retryFn={retryFn}
setPopoutFocus={setPopoutFocus}
totalNotifications={notifications.pagination?.total}
totalPages={notifications.pagination?.pages}
inboxQuery={inboxQuery}
setInboxQuery={setInboxQuery}
/>
</ReactPlaceholder>
</div>
Expand All @@ -103,7 +106,9 @@ const NotificationCards = ({
retryFn,
setPopoutFocus,
totalNotifications,
totalPages,
inboxQuery,
setInboxQuery,
}) => {
const dispatch = useDispatch();
const token = useSelector((state) => state.auth.token);
Expand Down Expand Up @@ -159,7 +164,10 @@ const NotificationCards = ({
unreadCountInSelected={unreadCountInSelected}
isAllSelected={isAllSelected}
inboxQuery={inboxQuery}
setInboxQuery={setInboxQuery}
updateUnreadCount={updateUnreadCount}
pageOfCards={pageOfCards}
totalPages={totalPages}
/>
</div>
{selected.length === 10 && totalNotifications > 10 && !inboxQuery.project && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Action Buttons', () => {
});

it('should decrement unread count in redux store if all notifications are not selected upon deleting notifications', async () => {
act(() => {});
render(
<ReduxIntlProviders>
<ActionButtons
Expand All @@ -89,6 +88,7 @@ describe('Action Buttons', () => {
setSelected={setSelectedMock}
updateUnreadCount={updateUnreadCountMock}
unreadCountInSelected={1}
pageOfCards={6}
/>
</ReduxIntlProviders>,
);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const NotificationsPage = () => {
notifications={notifications}
inboxQuery={inboxQuery}
retryFn={refetch}
setInboxQuery={setInboxQuery}
/>
<div className="flex justify-end mw8">
<Paginator
Expand Down

0 comments on commit 16f829b

Please sign in to comment.