From: Revanshu Paliwal Date: Wed, 26 Oct 2022 14:03:19 +0000 (+0200) Subject: SONAR-17515 Create new setting for updating login message X-Git-Tag: 9.8.0.63668~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=333225cff584b912230851be227dad3c5aec165b;p=sonarqube.git SONAR-17515 Create new setting for updating login message --- diff --git a/server/sonar-web/src/main/js/apps/settings/__tests__/utils-test.ts b/server/sonar-web/src/main/js/apps/settings/__tests__/utils-test.ts index f71d457f4cc..d6e164a01f4 100644 --- a/server/sonar-web/src/main/js/apps/settings/__tests__/utils-test.ts +++ b/server/sonar-web/src/main/js/apps/settings/__tests__/utils-test.ts @@ -18,14 +18,14 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { mockComponent } from '../../../helpers/mocks/component'; -import { mockDefinition } from '../../../helpers/mocks/settings'; +import { mockDefinition, mockSettingValue } from '../../../helpers/mocks/settings'; import { ExtendedSettingDefinition, Setting, SettingFieldDefinition, SettingType } from '../../../types/settings'; -import { buildSettingLink, getDefaultValue, getEmptyValue } from '../utils'; +import { buildSettingLink, getDefaultValue, getEmptyValue, getSettingValue } from '../utils'; const fields = [ { key: 'foo', type: 'STRING' } as SettingFieldDefinition, @@ -69,6 +69,49 @@ describe('#getEmptyValue()', () => { }); }); +describe('#getSettingValue()', () => { + it('should work for property sets', () => { + const setting: ExtendedSettingDefinition = { + ...settingDefinition, + type: SettingType.PROPERTY_SET, + fields + }; + const settingValue = mockSettingValue({ fieldValues: [{ foo: '' }] }); + expect(getSettingValue(setting, settingValue)).toEqual([{ foo: '' }]); + }); + + it('should work for category definitions', () => { + const setting: ExtendedSettingDefinition = { + ...settingDefinition, + type: SettingType.FORMATTED_TEXT, + fields, + multiValues: true + }; + const settingValue = mockSettingValue({ values: ['*text*', 'text'] }); + expect(getSettingValue(setting, settingValue)).toEqual(['*text*', 'text']); + }); + + it('should work for formatted text', () => { + const setting: ExtendedSettingDefinition = { + ...settingDefinition, + type: SettingType.FORMATTED_TEXT, + fields + }; + const settingValue = mockSettingValue({ values: ['*text*', 'text'] }); + expect(getSettingValue(setting, settingValue)).toEqual('*text*'); + }); + + it('should work for formatted text when values is undefined', () => { + const setting: ExtendedSettingDefinition = { + ...settingDefinition, + type: SettingType.FORMATTED_TEXT, + fields + }; + const settingValue = mockSettingValue({ values: undefined }); + expect(getSettingValue(setting, settingValue)).toBeUndefined(); + }); +}); + describe('#getDefaultValue()', () => { it.each([ ['true', 'settings.boolean.true'], diff --git a/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForFormattedText.tsx b/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForFormattedText.tsx new file mode 100644 index 00000000000..d2309911317 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForFormattedText.tsx @@ -0,0 +1,102 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import FormattingTipsWithLink from '../../../../components/common/FormattingTipsWithLink'; +import { Button } from '../../../../components/controls/buttons'; +import EditIcon from '../../../../components/icons/EditIcon'; +import { translate } from '../../../../helpers/l10n'; +import { sanitizeString } from '../../../../helpers/sanitize'; +import { DefaultSpecializedInputProps } from '../../utils'; + +interface State { + editMessage: boolean; +} + +export default class InputForFormattedText extends React.PureComponent< + DefaultSpecializedInputProps, + State +> { + constructor(props: DefaultSpecializedInputProps) { + super(props); + this.state = { + editMessage: !this.props.setting.hasValue + }; + } + + componentDidUpdate(prevProps: DefaultSpecializedInputProps) { + /* + * Reset `editMessage` if: + * - the value is reset (valueChanged -> !valueChanged) + * or + * - the value changes from outside the input (i.e. store update/reset/cancel) + */ + if ( + (prevProps.hasValueChanged || this.props.setting.value !== prevProps.setting.value) && + !this.props.hasValueChanged + ) { + this.setState({ editMessage: !this.props.setting.hasValue }); + } + } + + handleInputChange = (event: React.ChangeEvent) => { + this.props.onChange(event.target.value); + }; + + toggleEditMessage = () => { + const { editMessage } = this.state; + this.setState({ editMessage: !editMessage }); + }; + + render() { + const { editMessage } = this.state; + const { values } = this.props.setting; + // 0th value of the values array is markdown and 1st is the formatted text + const formattedValue = values ? values[1] : undefined; + + return ( +
+ {editMessage ? ( +
+