Skip to content

Commit

Permalink
fix(refetching): refetch messages when last message is archived, save…
Browse files Browse the repository at this point in the history
… search params
  • Loading branch information
rileylnapier committed May 22, 2024
1 parent a8c48f3 commit 7c5c547
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/react-hooks/src/inbox/__tests__/reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,13 @@ describe("inbox reducer", () => {

expect(state).toEqual({
...initialState,
pinned: [],
isLoading: false,
lastMessagesFetched: mockDate,
messages: [mockGraphMessage],
isLoading: false,
pinned: [],
searchParams: {
filters: {},
},
});
});

Expand Down Expand Up @@ -217,6 +220,9 @@ describe("inbox reducer", () => {
lastMessagesFetched: mockDate,
messages: [mockGraphMessage, mockGraphMessage2],
isLoading: false,
searchParams: {
filters: {},
},
});
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/react-hooks/src/inbox/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => {
return {
...state,
isLoading: false,
searchParams: action.meta.searchParams,
lastMessagesFetched: new Date().getTime(),
messages: newMessages as IInboxMessagePreview[],
pinned: action.payload?.appendMessages
Expand Down Expand Up @@ -355,6 +356,11 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => {
}

case INBOX_NEW_MESSAGE: {
if (state?.searchParams?.archived && !action.payload.archived) {
// don't add new message if we are on an archived list
return state;
}

const newMessage = {
...action.payload,
created: new Date().toISOString(),
Expand Down
2 changes: 2 additions & 0 deletions packages/react-hooks/src/inbox/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IGetInboxMessagesParams } from "@trycourier/client-graphql";
import {
Brand,
IInboxMessagePreview,
Expand All @@ -13,6 +14,7 @@ export interface IInbox<T = IInboxMessagePreview> {
lastMessagesFetched?: number;
messages?: Array<T>;
onEvent?: OnEvent;
searchParams?: IGetInboxMessagesParams;
pinned?: Array<T>;
startCursor?: string;
unreadMessageCount?: number;
Expand Down
13 changes: 12 additions & 1 deletion packages/react-hooks/src/inbox/use-inbox-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export interface IInboxActions {

const useInboxActions = (): IInboxActions => {
const {
tenantId,
apiUrl,
authorization,
clientKey,
clientSourceId,
dispatch,
inbox,
inboxApiUrl,
tenantId,
userId,
userSignature,
} =
Expand Down Expand Up @@ -244,6 +244,17 @@ const useInboxActions = (): IInboxActions => {
event: "archive",
});
}

const messageLength = inbox?.messages?.length ?? 0;
if (messageLength <= 1 && inbox.searchParams) {
dispatch({
meta: {
searchParams: inbox.searchParams,
},
payload: () => inboxClient.getMessages(inbox.searchParams),
type: "inbox/FETCH_MESSAGES",
});
}
},
addTag: async (messageId, tag, fromWS) => {
dispatch(addTag(messageId, tag));
Expand Down

0 comments on commit 7c5c547

Please sign in to comment.