Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(refetching): refetch messages when last message is archived, save… #574

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading