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 28, 2024
1 parent 618f257 commit 46b0306
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 @@ -184,10 +184,13 @@ describe("inbox reducer", () => {

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

Expand Down Expand Up @@ -216,6 +219,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 } from "@trycourier/core";
import { OnEvent } from "@trycourier/react-provider";
export interface IInbox<T = IInboxMessagePreview> {
Expand All @@ -10,6 +11,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 @@ -264,6 +264,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 46b0306

Please sign in to comment.