Skip to content

Commit

Permalink
fix stopping of the new unread items fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Dec 5, 2023
1 parent e15c334 commit 0c736f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/shared/hooks/useCases/useUnreadInboxItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export const useUnreadInboxItems = (unread?: boolean): void => {
return;
}

dispatch(
inboxActions.refreshUnreadInboxItems.request({
newItemsAmount: notificationsAmount,
}),
);
dispatch(inboxActions.refreshUnreadInboxItems.request());
}, [notificationsAmount]);
};
2 changes: 1 addition & 1 deletion src/store/states/inbox/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const refreshUnreadInboxItems = createAsyncAction(
InboxActionType.REFRESH_UNREAD_INBOX_ITEMS_SUCCESS,
InboxActionType.REFRESH_UNREAD_INBOX_ITEMS_FAILURE,
InboxActionType.REFRESH_UNREAD_INBOX_ITEMS_CANCEL,
)<{ newItemsAmount: number }, void, void, string>();
)<void, void, void, string>();

export const addNewInboxItems = createStandardAction(
InboxActionType.ADD_NEW_INBOX_ITEMS,
Expand Down
31 changes: 17 additions & 14 deletions src/store/states/inbox/saga/refreshUnreadInboxItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ import * as actions from "../actions";
import { selectInboxItems } from "../selectors";
import { InboxItems } from "../types";

export function* refreshUnreadInboxItems(
action: ReturnType<typeof actions.refreshUnreadInboxItems.request>,
) {
const {
payload: { newItemsAmount },
} = action;
const checkCanKeepFetchingByDate = (
firstDocTimestamp: Timestamp | null,
lastDocTimestamp: Timestamp | null,
): boolean => {
if (!firstDocTimestamp) {
return true;
}
if (!lastDocTimestamp) {
return false;
}

return lastDocTimestamp.seconds >= firstDocTimestamp.seconds;
};

export function* refreshUnreadInboxItems() {
try {
const currentItems = (yield select(selectInboxItems)) as InboxItems;
const newItemsAmountToFetch =
newItemsAmount - (currentItems.data?.length || 0);

if (newItemsAmountToFetch <= 0) {
return;
}

const { firstDocTimestamp } = currentItems;
const newInboxItems: FeedLayoutItemWithFollowData[] = [];
let startAfter: Timestamp | null = null;
let keepItemsFetching = true;
Expand Down Expand Up @@ -64,7 +66,8 @@ export function* refreshUnreadInboxItems(
);
newInboxItems.push(...chatChannelItems, ...feedItemFollowItems);
keepItemsFetching =
newInboxItems.length < newItemsAmountToFetch && hasMore;
hasMore &&
checkCanKeepFetchingByDate(firstDocTimestamp, lastDocTimestamp);
startAfter = lastDocTimestamp;
}

Expand Down

0 comments on commit 0c736f4

Please sign in to comment.