From 609b69ec53759f105bb4dc6d511f45f41b7c7c57 Mon Sep 17 00:00:00 2001 From: Harshil Sharma Date: Thu, 26 Aug 2021 11:19:32 +0530 Subject: [PATCH] Don't update the card if the property or description didn't change (#1092) * Not updating card when property or discription is not changed * Added tests Co-authored-by: Hossein --- webapp/src/components/content/textElement.tsx | 4 ++- webapp/src/mutator.test.ts | 35 +++++++++++++++++++ webapp/src/mutator.ts | 7 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 webapp/src/mutator.test.ts diff --git a/webapp/src/components/content/textElement.tsx b/webapp/src/components/content/textElement.tsx index 9e22a8a38..73955fe51 100644 --- a/webapp/src/components/content/textElement.tsx +++ b/webapp/src/components/content/textElement.tsx @@ -25,7 +25,9 @@ const TextElement = React.memo((props: Props): JSX.Element => { text={block.title} placeholderText={intl.formatMessage({id: 'ContentBlock.editText', defaultMessage: 'Edit text...'})} onBlur={(text) => { - mutator.changeTitle(block.id, block.title, text, intl.formatMessage({id: 'ContentBlock.editCardText', defaultMessage: 'edit card text'})) + if (text !== block.title) { + mutator.changeTitle(block.id, block.title, text, intl.formatMessage({id: 'ContentBlock.editCardText', defaultMessage: 'edit card text'})) + } }} readonly={readonly} /> diff --git a/webapp/src/mutator.test.ts b/webapp/src/mutator.test.ts new file mode 100644 index 000000000..3be17e0d6 --- /dev/null +++ b/webapp/src/mutator.test.ts @@ -0,0 +1,35 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import mutator from './mutator' +import {TestBlockFactory} from './test/testBlockFactory' +import 'isomorphic-fetch' +import {FetchMock} from './test/fetchMock' +import {mockDOM} from './testUtils' + +global.fetch = FetchMock.fn + +beforeEach(() => { + FetchMock.fn.mockReset() +}) + +beforeAll(() => { + mockDOM() +}) + +describe('Mutator', () => { + test('changePropertyValue', async () => { + const card = TestBlockFactory.createCard() + card.fields.properties.property_1 = 'hello' + + await mutator.changePropertyValue(card, 'property_1', 'hello') + + // No API call should be made as property value DIDN'T CHANGE + expect(FetchMock.fn).toBeCalledTimes(0) + + await mutator.changePropertyValue(card, 'property_1', 'hello world') + + // 1 API call should be made as property value DID CHANGE + expect(FetchMock.fn).toBeCalledTimes(1) + }) +}) diff --git a/webapp/src/mutator.ts b/webapp/src/mutator.ts index eefe3cf8b..bbdb78bb1 100644 --- a/webapp/src/mutator.ts +++ b/webapp/src/mutator.ts @@ -367,6 +367,13 @@ class Mutator { } async changePropertyValue(card: Card, propertyId: string, value?: string | string[], description = 'change property') { + const oldValue = card.fields.properties[propertyId] + + // dont save anything if property value was not changed. + if (oldValue === value) { + return + } + const newCard = createCard(card) if (value) { newCard.fields.properties[propertyId] = value