Skip to content

Commit

Permalink
add sorting to update list of inbox items
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Oct 23, 2023
1 parent f36f262 commit 53a3e6c
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/store/states/inbox/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
checkIsFeedItemFollowLayoutItem,
FeedLayoutItemWithFollowData,
} from "@/shared/interfaces";
import { ChatChannel, CommonFeed } from "@/shared/models";
import { ChatChannel, CommonFeed, Timestamp } from "@/shared/models";
import * as actions from "./actions";
import { InboxItems, InboxState } from "./types";
import { getFeedLayoutItemDateForSorting } from "./utils";
Expand All @@ -30,6 +30,26 @@ const initialState: InboxState = {
nextChatChannelItemId: null,
};

const sortInboxItems = (data: FeedLayoutItemWithFollowData[]): void => {
data.sort(
(prevItem, nextItem) =>
getFeedLayoutItemDateForSorting(nextItem).toMillis() -
getFeedLayoutItemDateForSorting(prevItem).toMillis(),
);
};

const getDocTimestamps = (
data: FeedLayoutItemWithFollowData[],
): {
firstDocTimestamp: Timestamp | null;
lastDocTimestamp: Timestamp | null;
} => ({
firstDocTimestamp: data[0] ? getFeedLayoutItemDateForSorting(data[0]) : null,
lastDocTimestamp: data[data.length - 1]
? getFeedLayoutItemDateForSorting(data[data.length - 1])
: null,
});

const updateInboxItemInList = (
state: WritableDraft<InboxState>,
payload: {
Expand Down Expand Up @@ -64,10 +84,14 @@ const updateInboxItemInList = (
...nextData[itemIndex],
...updatedItem,
};
sortInboxItems(nextData);
}
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -162,9 +186,13 @@ const updateFeedItemInInboxItem = (
feedItem: { ...newFeedItem },
},
};
sortInboxItems(nextData);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -285,9 +313,13 @@ const updateChatChannelItemInInboxItem = (
lastMessage: updatedChatChannelItem.lastMessage || undefined,
},
};
sortInboxItems(nextData);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -421,8 +453,6 @@ export const reducer = createReducer<InboxState, Action>(initialState)
(chatChannelItem) => chatChannelItem.itemId === item.item.itemId,
),
);
let firstDocTimestamp = nextState.items.firstDocTimestamp;

const data = payload.reduceRight((acc, { item, statuses }) => {
const nextData = [...acc];
const itemIndex = nextData.findIndex(
Expand All @@ -443,7 +473,6 @@ export const reducer = createReducer<InboxState, Action>(initialState)
}

const finalItem: FeedLayoutItemWithFollowData = { ...item };
firstDocTimestamp = getFeedLayoutItemDateForSorting(item);

if (itemIndex < 0) {
return [finalItem, ...nextData];
Expand All @@ -453,11 +482,14 @@ export const reducer = createReducer<InboxState, Action>(initialState)

return nextData;
}, nextState.items.data || []);
sortInboxItems(data);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(data);

nextState.items = {
...nextState.items,
data,
firstDocTimestamp,
lastDocTimestamp,
};
}),
)
Expand Down

0 comments on commit 53a3e6c

Please sign in to comment.