From: 7PH Date: Thu, 5 Oct 2023 15:05:28 +0000 (+0200) Subject: SONAR-20366 Migrate QP restore modal to MIUI X-Git-Tag: 10.3.0.82913~255 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8744d1dd2fd0a64227799ee520fcdf87831483ea;p=sonarqube.git SONAR-20366 Migrate QP restore modal to MIUI --- diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/home/RestoreProfileForm.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/home/RestoreProfileForm.tsx index 73c6cac5b5a..ae0f70eeb2c 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/home/RestoreProfileForm.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/home/RestoreProfileForm.tsx @@ -17,135 +17,122 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { ButtonPrimary, FileInput, FlagMessage, FormField, Modal, Spinner } from 'design-system'; import * as React from 'react'; +import { useRef, useState } from 'react'; +import { useIntl } from 'react-intl'; import { restoreQualityProfile } from '../../../api/quality-profiles'; -import Modal from '../../../components/controls/Modal'; -import { ResetButtonLink, SubmitButton } from '../../../components/controls/buttons'; -import { Alert } from '../../../components/ui/Alert'; -import MandatoryFieldMarker from '../../../components/ui/MandatoryFieldMarker'; import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation'; -import { translate, translateWithParameters } from '../../../helpers/l10n'; interface Props { onClose: () => void; onRestore: () => void; } -interface State { - loading: boolean; - profile?: { name: string }; - ruleFailures?: number; - ruleSuccesses?: number; -} - -export default class RestoreProfileForm extends React.PureComponent { - mounted = false; - state: State = { loading: false }; +export default function RestoreProfileForm({ onClose, onRestore }: Readonly) { + const intl = useIntl(); - componentDidMount() { - this.mounted = true; - } + const [loading, setLoading] = useState(false); + const [profile, setProfile] = useState(); + const [ruleFailures, setRuleFailures] = useState(); + const [ruleSuccesses, setRuleSuccesses] = useState(); - componentWillUnmount() { - this.mounted = false; - } + const formRef = useRef(null); - handleFormSubmit = (event: React.SyntheticEvent) => { - event.preventDefault(); + async function handleFormSubmit() { + if (!formRef.current) { + return; + } + const data = new FormData(formRef.current); - this.setState({ loading: true }); - - const data = new FormData(event.currentTarget); - - restoreQualityProfile(data).then( - (response: any) => { - if (this.mounted) { - this.setState({ - loading: false, - profile: response.profile, - ruleFailures: response.ruleFailures, - ruleSuccesses: response.ruleSuccesses, - }); - } - this.props.onRestore(); - }, - () => { - if (this.mounted) { - this.setState({ loading: false }); - } - }, - ); - }; + try { + setLoading(true); + const { profile, ruleFailures, ruleSuccesses } = await restoreQualityProfile(data); + setProfile(profile); + setRuleFailures(ruleFailures); + setRuleSuccesses(ruleSuccesses); + onRestore(); + } finally { + setLoading(false); + } + } - renderAlert(profile: { name: string }, ruleFailures = 0, ruleSuccesses: number): React.ReactNode { + function renderAlert(profile: { name: string }, ruleFailures: number, ruleSuccesses: number) { return ruleFailures ? ( - - {translateWithParameters( - 'quality_profiles.restore_profile.warning', - profile.name, - ruleSuccesses, - ruleFailures, + + {intl.formatMessage( + { + id: `quality_profiles.restore_profile.warning`, + }, + { + profileName: profile.name, + ruleFailures, + ruleSuccesses, + }, )} - + ) : ( - - {translateWithParameters( - 'quality_profiles.restore_profile.success', - profile.name, - ruleSuccesses, + + {intl.formatMessage( + { + id: `quality_profiles.restore_profile.success`, + }, + { + profileName: profile.name, + ruleSuccesses, + }, )} - + ); } - render() { - const header = translate('quality_profiles.restore_profile'); - - const { loading, profile, ruleFailures, ruleSuccesses } = this.state; - - return ( - -
-
-

{header}

-
- -
- {profile != null && ruleSuccesses != null ? ( - this.renderAlert(profile, ruleFailures, ruleSuccesses) - ) : ( - <> - -
- - -
- - )} -
- - {ruleSuccesses == null ? ( -
- {loading && } - - {translate('restore')} - - - {translate('cancel')} - -
+ return ( + + {profile != null && ruleSuccesses != null ? ( + renderAlert(profile, ruleFailures ?? 0, ruleSuccesses) ) : ( -
- - {translate('close')} - -
+ <> + + + + + )} -
- ); - } + } + primaryButton={ + ruleSuccesses == null ? ( + <> + + + {intl.formatMessage({ id: 'restore' })} + + + ) : ( + + {intl.formatMessage({ id: 'close' })} + + ) + } + secondaryButtonLabel={intl.formatMessage({ id: ruleSuccesses == null ? 'cancel' : 'close' })} + /> + ); } diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index 3d5c5531f2b..83ccb43d34c 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -1987,8 +1987,8 @@ quality_profiles.compare_with=Compare with quality_profiles.filter_by=Filter by quality_profiles.select_lang=Select language quality_profiles.restore_profile=Restore Profile -quality_profiles.restore_profile.success={1} rule(s) restored in profile "{0}" -quality_profiles.restore_profile.warning={1} rule(s) restored, {2} rule(s) ignored in profile "{0}" +quality_profiles.restore_profile.success={ruleSuccesses} rule(s) restored in profile "{profileName}" +quality_profiles.restore_profile.warning={ruleSuccesses} rule(s) restored, {ruleFailures} rule(s) ignored in profile "{profileName}" quality_profiles.optional_configuration_file=Optional configuration file quality_profiles.new_name=New name quality_profiles.no_languages_available=There are no languages available. You cannot create a new profile.