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 <hahmadia@users.noreply.github.com>
This commit is contained in:
Harshil Sharma 2021-08-26 11:19:32 +05:30 committed by GitHub
parent b58497e2ea
commit 609b69ec53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View file

@ -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}
/>

View file

@ -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)
})
})

View file

@ -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