From 49948891ce1e77d5bba6144b90b79aefbbbf78b3 Mon Sep 17 00:00:00 2001 From: Scott Bishel Date: Fri, 24 Jan 2025 10:28:19 -0700 Subject: [PATCH] MM-62550- trim value and create batch updates (#29895) * trim value and create batch updates * update test * update for review comments --------- Co-authored-by: Caleb Roseland --- .../user_settings/general/user_settings_general.test.tsx | 2 +- .../user_settings/general/user_settings_general.tsx | 3 ++- .../packages/mattermost-redux/src/actions/users.test.ts | 4 ++-- .../src/packages/mattermost-redux/src/actions/users.ts | 7 ++++--- webapp/platform/client/src/client4.ts | 7 ++----- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/webapp/channels/src/components/user_settings/general/user_settings_general.test.tsx b/webapp/channels/src/components/user_settings/general/user_settings_general.test.tsx index 109602417e..3848d382bb 100644 --- a/webapp/channels/src/components/user_settings/general/user_settings_general.test.tsx +++ b/webapp/channels/src/components/user_settings/general/user_settings_general.test.tsx @@ -246,7 +246,7 @@ describe('components/user_settings/general/UserSettingsGeneral', () => { }); test('submitAttribute() should have called saveCustomProfileAttribute', async () => { - const saveCustomProfileAttribute = jest.fn().mockResolvedValue({data: true}); + const saveCustomProfileAttribute = jest.fn().mockResolvedValue({1: 'Updated Value'}); const props = { ...requiredProps, enableCustomProfileAttributes: true, diff --git a/webapp/channels/src/components/user_settings/general/user_settings_general.tsx b/webapp/channels/src/components/user_settings/general/user_settings_general.tsx index 0db241707a..6dee9080b4 100644 --- a/webapp/channels/src/components/user_settings/general/user_settings_general.tsx +++ b/webapp/channels/src/components/user_settings/general/user_settings_general.tsx @@ -119,7 +119,7 @@ export type Props = { sendVerificationEmail: (email: string) => Promise; setDefaultProfileImage: (id: string) => void; uploadProfileImage: (id: string, file: File) => Promise; - saveCustomProfileAttribute: (userID: string, attributeID: string, attributeValue: string) => Promise; + saveCustomProfileAttribute: (userID: string, attributeID: string, attributeValue: string) => Promise>>; getCustomProfileAttributeFields: () => Promise; }; requireEmailVerification?: boolean; @@ -429,6 +429,7 @@ export class UserSettingsGeneralTab extends PureComponent { then(({data, error: err}) => { if (data) { this.updateSection(''); + this.setState({customAttributeValues: {...this.state.customAttributeValues, ...data}}); } else if (err) { const serverError = err; this.setState({serverError, emailError: '', clientError: '', sectionIsSaving: false}); diff --git a/webapp/channels/src/packages/mattermost-redux/src/actions/users.test.ts b/webapp/channels/src/packages/mattermost-redux/src/actions/users.test.ts index 2dcf7c6afe..7bd618d295 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/actions/users.test.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/actions/users.test.ts @@ -1710,9 +1710,9 @@ describe('Actions.Users', () => { 123: 'NewValue', }); - const response = await store.dispatch(Actions.saveCustomProfileAttribute(currentUser.id, '123', 'NewValue')); + const response = await store.dispatch(Actions.saveCustomProfileAttribute(currentUser.id, '123', ' NewValue ')); const data = response.data!; - expect(data).toBeTruthy(); + expect(data).toEqual({123: 'NewValue'}); }); describe('checkForModifiedUsers', () => { diff --git a/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts b/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts index f3d043b1ab..3571fa94e5 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts @@ -970,15 +970,16 @@ export function updateMe(user: Partial): ActionFuncAsync> { return async (dispatch) => { try { - await Client4.updateCustomProfileAttributeValues(attributeID, attributeValue); + const values = {[attributeID]: attributeValue.trim()}; + const data = await Client4.updateCustomProfileAttributeValues(values); + return {data}; } catch (error) { dispatch(logError(error)); return {error}; } - return {data: true}; }; } diff --git a/webapp/platform/client/src/client4.ts b/webapp/platform/client/src/client4.ts index 1d28ae8245..3d1c519dff 100644 --- a/webapp/platform/client/src/client4.ts +++ b/webapp/platform/client/src/client4.ts @@ -2103,13 +2103,10 @@ export default class Client4 { ); }; - updateCustomProfileAttributeValues = (attributeID: string, attributeValue: string) => { - const obj: { [key: string]: string } = {}; - obj[attributeID] = attributeValue; - + updateCustomProfileAttributeValues = (attributeValues: Record) => { return this.doFetch>( `${this.getCustomProfileAttributeValuesRoute()}`, - {method: 'PATCH', body: JSON.stringify(obj)}, + {method: 'PATCH', body: JSON.stringify(attributeValues)}, ); };