Skip to content

Commit

Permalink
fix: prevent double UI update on message edit
Browse files Browse the repository at this point in the history
  • Loading branch information
domw30 committed Oct 17, 2023
1 parent 36a8644 commit 1754869
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/store/messages/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,38 @@ describe('messages saga', () => {
});

describe(receiveUpdateMessage, () => {
it('updates the messages', async () => {
it('does not update the message if already updated', async () => {
const originalMessage = {
id: 8667728016,
message: 'edited message',
parentMessageText: null,
createdAt: 1678861267433,
updatedAt: 1678861290000,
};

const messages = { 8667728016: originalMessage };

const { storeState } = await expectSaga(receiveUpdateMessage, {
payload: { channelId: 'channel-1', message: originalMessage },
})
.provide([
...successResponses(),
])
.withReducer(rootReducer, { normalized: { messages } as any } as RootState)
.run();

expect(storeState.normalized.messages).toEqual({ 8667728016: originalMessage });
});

it('updates the message if not already updated', async () => {
const message = {
id: 8667728016,
message: 'original message',
parentMessageText: null,
createdAt: 1678861267433,
updatedAt: 0,
};

const editedMessage = {
id: 8667728016,
message: 'edited message',
Expand Down
6 changes: 6 additions & 0 deletions src/store/messages/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ export function* replaceOptimisticMessage(currentMessages, message) {
export function* receiveUpdateMessage(action) {
let { message } = action.payload;

// Check if the message already exists in the state
const existingMessage = yield select(messageSelector(message.id));
if (existingMessage && existingMessage.message === message.message) {
return;
}

const preview = yield call(getPreview, message.message);
message.preview = preview;

Expand Down

0 comments on commit 1754869

Please sign in to comment.