]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20858 migrate modals to new UI
authorJeremy Davis <jeremy.davis@sonarsource.com>
Wed, 25 Oct 2023 14:15:43 +0000 (16:15 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 26 Oct 2023 20:02:58 +0000 (20:02 +0000)
server/sonar-web/src/main/js/apps/projectBranches/__tests__/ProjectBranchesApp-it.tsx
server/sonar-web/src/main/js/apps/projectBranches/components/DeleteBranchModal.tsx
server/sonar-web/src/main/js/apps/projectBranches/components/RenameBranchModal.tsx
server/sonar-web/src/main/js/apps/projectBranches/components/SetAsMainBranchModal.tsx
sonar-core/src/main/resources/org/sonar/l10n/core.properties

index c4d75bdcb911dc4e08d945ac60afb3508ee3c01e..312846c852d0c59329a054aa5eb54078b50d3fb5 100644 (file)
@@ -163,7 +163,7 @@ it('should be able to set a branch as the main branch', async () => {
   await act(async () => {
     await user.click(
       within(ui.dialog.get()).getByRole('button', {
-        name: 'project_branch_pull_request.branch.set_x_as_main.delete-branch',
+        name: 'project_branch_pull_request.branch.set_main',
       }),
     );
   });
index 7f0512c3ef9194d0ce64e4032d6418ca378d8fc6..cc306e2ea92ef890b3fcab64febd74ac1027fe07 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+import { DangerButtonPrimary, Modal } from 'design-system';
 import * as React from 'react';
-import Modal from '../../../components/controls/Modal';
-import { ResetButtonLink, SubmitButton } from '../../../components/controls/buttons';
+import { FormattedMessage } from 'react-intl';
 import { getBranchLikeDisplayName, isPullRequest } from '../../../helpers/branch-like';
-import { translate, translateWithParameters } from '../../../helpers/l10n';
 import { useDeletBranchMutation } from '../../../queries/branch';
 import { BranchLike } from '../../../types/branch-like';
 import { Component } from '../../../types/types';
@@ -32,48 +31,56 @@ interface Props {
   onClose: () => void;
 }
 
+const FORM_ID = 'confirm-branch-delete-form';
+
 export default function DeleteBranchModal(props: Props) {
-  const { branchLike, component } = props;
+  const { branchLike, component, onClose } = props;
   const { mutate: deleteBranch, isLoading } = useDeletBranchMutation();
 
-  const handleSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
-    event.preventDefault();
-    deleteBranch(
-      { component, branchLike },
-      {
-        onSuccess: props.onClose,
-      },
-    );
-  };
-
-  const header = translate(
-    isPullRequest(branchLike)
-      ? 'project_branch_pull_request.pull_request.delete'
-      : 'project_branch_pull_request.branch.delete',
+  const handleSubmit = React.useCallback(
+    (event: React.SyntheticEvent<HTMLFormElement>) => {
+      event.preventDefault();
+      deleteBranch(
+        { component, branchLike },
+        {
+          onSuccess: onClose,
+        },
+      );
+    },
+    [deleteBranch, component, branchLike, onClose],
   );
 
   return (
-    <Modal contentLabel={header} onRequestClose={props.onClose}>
-      <header className="modal-head">
-        <h2>{header}</h2>
-      </header>
-      <form onSubmit={handleSubmit}>
-        <div className="modal-body">
-          {translateWithParameters(
+    <Modal
+      headerTitle={
+        <FormattedMessage
+          id={
             isPullRequest(branchLike)
-              ? 'project_branch_pull_request.pull_request.delete.are_you_sure'
-              : 'project_branch_pull_request.branch.delete.are_you_sure',
-            getBranchLikeDisplayName(branchLike),
-          )}
-        </div>
-        <footer className="modal-foot">
-          {isLoading && <i className="spinner spacer-right" />}
-          <SubmitButton className="button-red" disabled={isLoading}>
-            {translate('delete')}
-          </SubmitButton>
-          <ResetButtonLink onClick={props.onClose}>{translate('cancel')}</ResetButtonLink>
-        </footer>
-      </form>
-    </Modal>
+              ? 'project_branch_pull_request.pull_request.delete'
+              : 'project_branch_pull_request.branch.delete'
+          }
+        />
+      }
+      body={
+        <form id={FORM_ID} onSubmit={handleSubmit}>
+          <FormattedMessage
+            id={
+              isPullRequest(branchLike)
+                ? 'project_branch_pull_request.pull_request.delete.are_you_sure'
+                : 'project_branch_pull_request.branch.delete.are_you_sure'
+            }
+            values={{ name: getBranchLikeDisplayName(branchLike) }}
+          />
+        </form>
+      }
+      loading={isLoading}
+      primaryButton={
+        <DangerButtonPrimary type="submit" form={FORM_ID}>
+          <FormattedMessage id="delete" />
+        </DangerButtonPrimary>
+      }
+      secondaryButtonLabel={<FormattedMessage id="cancel" />}
+      onClose={props.onClose}
+    />
   );
 }
index d92b0794d424538a417c219a393d15554abab654..435730dc5179bbc98643a0177411638c587d22af 100644 (file)
  * 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, FormField, InputField, Modal } from 'design-system';
 import * as React from 'react';
 import { useState } from 'react';
-import Modal from '../../../components/controls/Modal';
-import { ResetButtonLink, SubmitButton } from '../../../components/controls/buttons';
-import MandatoryFieldMarker from '../../../components/ui/MandatoryFieldMarker';
-import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
+import { FormattedMessage } from 'react-intl';
 import { translate } from '../../../helpers/l10n';
 import { useRenameMainBranchMutation } from '../../../queries/branch';
 import { MainBranch } from '../../../types/branch-like';
@@ -34,60 +32,61 @@ interface Props {
   onClose: () => void;
 }
 
+const FORM_ID = 'branch-rename-form';
+
 export default function RenameBranchModal(props: Props) {
-  const { branch, component } = props;
+  const { branch, component, onClose } = props;
   const [name, setName] = useState<string>();
 
   const { mutate: renameMainBranch, isLoading } = useRenameMainBranchMutation();
 
-  const handleSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
-    event.preventDefault();
-    if (!name) {
-      return;
-    }
+  const handleSubmit = React.useCallback(
+    (event: React.SyntheticEvent<HTMLFormElement>) => {
+      event.preventDefault();
+      if (!name) {
+        return;
+      }
 
-    renameMainBranch({ component, name }, { onSuccess: props.onClose });
-  };
+      renameMainBranch({ component, name }, { onSuccess: onClose });
+    },
+    [component, name, onClose, renameMainBranch],
+  );
 
-  const handleNameChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
+  const handleNameChange = React.useCallback((event: React.SyntheticEvent<HTMLInputElement>) => {
     setName(event.currentTarget.value);
-  };
+  }, []);
 
   const header = translate('project_branch_pull_request.branch.rename');
   const submitDisabled = isLoading || !name || name === branch.name;
 
   return (
-    <Modal contentLabel={header} onRequestClose={props.onClose} size="small">
-      <header className="modal-head">
-        <h2>{header}</h2>
-      </header>
-      <form onSubmit={handleSubmit}>
-        <div className="modal-body">
-          <MandatoryFieldsExplanation className="modal-field" />
-          <div className="modal-field">
-            <label htmlFor="rename-branch-name">
-              {translate('new_name')}
-              <MandatoryFieldMarker />
-            </label>
-            <input
+    <Modal
+      headerTitle={header}
+      body={
+        <form id={FORM_ID} onSubmit={handleSubmit}>
+          <FormField className="sw-mb-1" label={<FormattedMessage id="new_name" />}>
+            <InputField
               autoFocus
               id="rename-branch-name"
               maxLength={100}
               name="name"
               onChange={handleNameChange}
               required
-              size={50}
+              size="full"
               type="text"
               value={name ?? branch.name}
             />
-          </div>
-        </div>
-        <footer className="modal-foot">
-          {isLoading && <i className="spinner spacer-right" />}
-          <SubmitButton disabled={submitDisabled}>{translate('rename')}</SubmitButton>
-          <ResetButtonLink onClick={props.onClose}>{translate('cancel')}</ResetButtonLink>
-        </footer>
-      </form>
-    </Modal>
+          </FormField>
+        </form>
+      }
+      loading={isLoading}
+      primaryButton={
+        <ButtonPrimary disabled={submitDisabled} type="submit" form={FORM_ID}>
+          <FormattedMessage id="rename" />
+        </ButtonPrimary>
+      }
+      secondaryButtonLabel={<FormattedMessage id="cancel" />}
+      onClose={props.onClose}
+    />
   );
 }
index 99407a7bdcbbaaa04ec0c93d8bcaecfb5fe8a330..00e81ae43181249f454aa3d5cabac8b6ac1766dd 100644 (file)
@@ -20,8 +20,8 @@
 import { ButtonPrimary, FlagMessage, Modal } from 'design-system';
 import * as React from 'react';
 import { FormattedMessage } from 'react-intl';
-import DocLink from '../../../components/common/DocLink';
-import { translate, translateWithParameters } from '../../../helpers/l10n';
+import DocumentationLink from '../../../components/common/DocumentationLink';
+import { translate } from '../../../helpers/l10n';
 import { useSetMainBranchMutation } from '../../../queries/branch';
 import { Branch } from '../../../types/branch-like';
 import { Component } from '../../../types/types';
@@ -33,42 +33,46 @@ interface SetAsMainBranchModalProps {
   onSetAsMain: () => void;
 }
 
+const FORM_ID = 'branch-setasmain-form';
+
 export default function SetAsMainBranchModal(props: SetAsMainBranchModalProps) {
   const { branch, component, onClose, onSetAsMain } = props;
   const { mutate: setMainBranch, isLoading } = useSetMainBranchMutation();
 
-  const handleClick = () => {
-    setMainBranch({ component, branchName: branch.name }, { onSuccess: onSetAsMain });
-  };
+  const handleSubmit = React.useCallback(
+    (e: React.SyntheticEvent<HTMLFormElement>) => {
+      e.preventDefault();
+      setMainBranch({ component, branchName: branch.name }, { onSuccess: onSetAsMain });
+    },
+    [branch, component, onSetAsMain, setMainBranch],
+  );
 
   return (
     <Modal
       headerTitle={
-        <span className="sw-break-all">
-          {translateWithParameters('project_branch_pull_request.branch.set_x_as_main', branch.name)}
-        </span>
+        <FormattedMessage
+          id="project_branch_pull_request.branch.set_x_as_main"
+          values={{ branch: <span className="sw-break-all">{branch.name}</span> }}
+        />
       }
       loading={isLoading}
       onClose={onClose}
       body={
-        <>
+        <form id={FORM_ID} onSubmit={handleSubmit}>
           <p className="sw-mb-4">
-            {translateWithParameters(
-              'project_branch_pull_request.branch.main_branch.are_you_sure',
-              branch.name,
-            )}
+            <FormattedMessage
+              id="project_branch_pull_request.branch.main_branch.are_you_sure"
+              values={{ branch: <span className="sw-break-all">{branch.name}</span> }}
+            />
           </p>
           <p className="sw-mb-4">
             <FormattedMessage
               id="project_branch_pull_request.branch.main_branch.learn_more"
-              defaultMessage={translate(
-                'project_branch_pull_request.branch.main_branch.learn_more',
-              )}
               values={{
                 documentation: (
-                  <DocLink to="/analyzing-source-code/branches/branch-analysis/#main-branch">
+                  <DocumentationLink to="/analyzing-source-code/branches/branch-analysis/#main-branch">
                     {translate('documentation')}
-                  </DocLink>
+                  </DocumentationLink>
                 ),
               }}
             />
@@ -76,14 +80,14 @@ export default function SetAsMainBranchModal(props: SetAsMainBranchModalProps) {
           <FlagMessage variant="warning">
             {translate('project_branch_pull_request.branch.main_branch.requires_reindex')}
           </FlagMessage>
-        </>
+        </form>
       }
       primaryButton={
-        <ButtonPrimary disabled={isLoading} onClick={handleClick}>
-          {translateWithParameters('project_branch_pull_request.branch.set_x_as_main', branch.name)}
+        <ButtonPrimary disabled={isLoading} type="submit" form={FORM_ID}>
+          <FormattedMessage id="project_branch_pull_request.branch.set_main" />
         </ButtonPrimary>
       }
-      secondaryButtonLabel={translate('cancel')}
+      secondaryButtonLabel={<FormattedMessage id="cancel" />}
     />
   );
 }
index 9ee9818a20ae981828d99cb761cd80c464e7cc7e..7259cf5d64cdd8aff260ecc468b09373e37c5a57 100644 (file)
@@ -636,18 +636,18 @@ project_branch_pull_request.lifetime_information=Branches and Pull Requests are
 project_branch_pull_request.lifetime_information.admin=You can adjust this value globally in {settings}.
 project_branch_pull_request.branch.rename=Rename branch
 project_branch_pull_request.branch.set_main=Set as main branch
-project_branch_pull_request.branch.set_x_as_main=Set "{0}" as the main branch
+project_branch_pull_request.branch.set_x_as_main=Set "{branch}" as the main branch
 project_branch_pull_request.branch.delete=Delete branch
 project_branch_pull_request.branch.actions_label=Update {0}
-project_branch_pull_request.branch.delete.are_you_sure=Are you sure you want to delete branch "{0}"?
-project_branch_pull_request.branch.main_branch.are_you_sure=Are you sure you want to set branch "{0}" as the main branch of this project?
+project_branch_pull_request.branch.delete.are_you_sure=Are you sure you want to delete branch "{name}"?
+project_branch_pull_request.branch.main_branch.are_you_sure=Are you sure you want to set branch "{branch}" as the main branch of this project?
 project_branch_pull_request.branch.main_branch.requires_reindex=Changing the main branch of your project will trigger a project re-indexation and may impact the level of information that is available until re-indexing is complete.
 project_branch_pull_request.branch.main_branch.learn_more=Please refer to the {documentation} to understand the impacts of changing the main branch.
 project_branch_pull_request.branch.auto_deletion.keep_when_inactive=Keep when inactive
 project_branch_pull_request.branch.auto_deletion.keep_when_inactive.tooltip=When turned on, the branch will not be automatically deleted when inactive.
 project_branch_pull_request.branch.auto_deletion.main_branch_tooltip=The main branch is always excluded from automatic deletion.
 project_branch_pull_request.pull_request.delete=Delete Pull Request
-project_branch_pull_request.pull_request.delete.are_you_sure=Are you sure you want to delete Pull Request "{0}"?
+project_branch_pull_request.pull_request.delete.are_you_sure=Are you sure you want to delete Pull Request "{name}"?
 project_branch_pull_request.tabs.branches=Branches
 project_branch_pull_request.tabs.pull_requests=Pull Requests
 project_branch_pull_request.table.branch=Branch