From fca9924906130f85eea93f8caf64fad6a79fc317 Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Thu, 19 Nov 2020 11:21:26 +0100 Subject: [PATCH] Fixing codesmell --- .../ApplicationBranches.tsx | 1 - .../ApplicationConsoleApp.tsx | 2 +- .../application-console/BranchRowActions.tsx | 1 - .../application-console/CreateBranchForm.tsx | 26 ++++++++++++------- .../application-console/ProjectBranchRow.tsx | 9 +++---- .../ApplicationBranches-test.tsx.snap | 1 - 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/application-console/ApplicationBranches.tsx b/server/sonar-web/src/main/js/apps/application-console/ApplicationBranches.tsx index c2f53284abe..bd9a2586e83 100644 --- a/server/sonar-web/src/main/js/apps/application-console/ApplicationBranches.tsx +++ b/server/sonar-web/src/main/js/apps/application-console/ApplicationBranches.tsx @@ -111,7 +111,6 @@ export default class ApplicationBranches extends React.PureComponent p.key)} onClose={this.handleCreateFormClose} onCreate={this.handleCreate} - onUpdate={() => {}} /> )} diff --git a/server/sonar-web/src/main/js/apps/application-console/ApplicationConsoleApp.tsx b/server/sonar-web/src/main/js/apps/application-console/ApplicationConsoleApp.tsx index 6a9633246c7..2c0cf5269d3 100644 --- a/server/sonar-web/src/main/js/apps/application-console/ApplicationConsoleApp.tsx +++ b/server/sonar-web/src/main/js/apps/application-console/ApplicationConsoleApp.tsx @@ -39,7 +39,7 @@ interface State { loading: boolean; } -export default class ApplicationView extends React.PureComponent { +export default class ApplicationConsoleApp extends React.PureComponent { mounted = false; state: State = { diff --git a/server/sonar-web/src/main/js/apps/application-console/BranchRowActions.tsx b/server/sonar-web/src/main/js/apps/application-console/BranchRowActions.tsx index 15538900ddb..0ce7924c01b 100644 --- a/server/sonar-web/src/main/js/apps/application-console/BranchRowActions.tsx +++ b/server/sonar-web/src/main/js/apps/application-console/BranchRowActions.tsx @@ -101,7 +101,6 @@ export default class BranchRowActions extends React.PureComponent .filter(p => p.enabled) .map(p => p.key)} onClose={this.handleCloseForm} - onCreate={() => {}} onUpdate={this.handleUpdate} /> )} diff --git a/server/sonar-web/src/main/js/apps/application-console/CreateBranchForm.tsx b/server/sonar-web/src/main/js/apps/application-console/CreateBranchForm.tsx index c8fe5f4f2c3..4a34cdd8808 100644 --- a/server/sonar-web/src/main/js/apps/application-console/CreateBranchForm.tsx +++ b/server/sonar-web/src/main/js/apps/application-console/CreateBranchForm.tsx @@ -38,8 +38,8 @@ interface Props { branch?: ApplicationBranch; enabledProjectsKey: string[]; onClose: () => void; - onCreate: (branch: ApplicationBranch) => void; - onUpdate: (name: string) => void; + onCreate?: (branch: ApplicationBranch) => void; + onUpdate?: (name: string) => void; } interface BranchesList { @@ -54,6 +54,8 @@ interface State { selectedBranches: BranchesList; } +const MAX_PROJECTS_HEIGHT = 220; +const PROJECT_HEIGHT = 22; export default class CreateBranchForm extends React.PureComponent { mounted = false; node?: HTMLElement | null = null; @@ -73,14 +75,14 @@ export default class CreateBranchForm extends React.PureComponent const branch = this.props.branch ? this.props.branch.name : undefined; this.setState({ loading: true }); getApplicationDetails(application.key, branch).then( - application => { + ({ projects }) => { if (this.mounted) { - const projects = application.projects.filter(p => + const enabledProjects = projects.filter(p => this.props.enabledProjectsKey.includes(p.key) ); - const selected = projects.filter(p => p.selected).map(p => p.key); + const selected = enabledProjects.filter(p => p.selected).map(p => p.key); const selectedBranches: BranchesList = {}; - projects.forEach(p => { + enabledProjects.forEach(p => { if (!p.enabled) { selectedBranches[p.key] = null; } else { @@ -95,7 +97,7 @@ export default class CreateBranchForm extends React.PureComponent name: branch || '', selected, loading: false, - projects, + projects: enabledProjects, selectedBranches }); } @@ -148,7 +150,9 @@ export default class CreateBranchForm extends React.PureComponent project: projectKeys, projectBranch: projectBranches }); - this.props.onUpdate(this.state.name); + if (this.props.onUpdate) { + this.props.onUpdate(this.state.name); + } } else { await addApplicationBranch({ application: this.props.application.key, @@ -156,7 +160,9 @@ export default class CreateBranchForm extends React.PureComponent project: projectKeys, projectBranch: projectBranches }); - this.props.onCreate({ name: this.state.name, isMain: false }); + if (this.props.onCreate) { + this.props.onCreate({ name: this.state.name, isMain: false }); + } } this.props.onClose(); }; @@ -183,7 +189,7 @@ export default class CreateBranchForm extends React.PureComponent if (this.node) { const modalTop = this.node.getBoundingClientRect().top; const modalHeight = this.node.offsetHeight; - const maxSelectHeight = Math.min(220, elementCount * 22 + 22); + const maxSelectHeight = Math.min(MAX_PROJECTS_HEIGHT, (elementCount + 1) * PROJECT_HEIGHT); const selectBottom = selectNode.getBoundingClientRect().top + maxSelectHeight; if (selectBottom > modalTop + modalHeight) { this.node.classList.add('inverted-direction'); diff --git a/server/sonar-web/src/main/js/apps/application-console/ProjectBranchRow.tsx b/server/sonar-web/src/main/js/apps/application-console/ProjectBranchRow.tsx index 8061640b298..9ce8862b8f7 100644 --- a/server/sonar-web/src/main/js/apps/application-console/ProjectBranchRow.tsx +++ b/server/sonar-web/src/main/js/apps/application-console/ProjectBranchRow.tsx @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { orderBy } from 'lodash'; import * as React from 'react'; import Checkbox from 'sonar-ui-common/components/controls/Checkbox'; import Select from 'sonar-ui-common/components/controls/Select'; @@ -58,11 +59,9 @@ export default class ProjectBranchRow extends React.PureComponent } parseBranches = (branches: Array) => { - return branches - .sort((a, b) => (a.name < b.name ? -1 : 1)) - .map(branch => { - return { value: branch.name, label: branch.name, isMain: branch.isMain }; - }); + return orderBy(branches, [b => b.isMain, b => b.name]).map(branch => { + return { value: branch.name, label: branch.name, isMain: branch.isMain }; + }); }; setCurrentTarget = (event: React.FocusEvent) => { diff --git a/server/sonar-web/src/main/js/apps/application-console/__tests__/__snapshots__/ApplicationBranches-test.tsx.snap b/server/sonar-web/src/main/js/apps/application-console/__tests__/__snapshots__/ApplicationBranches-test.tsx.snap index d291936d7be..b502ef33ad5 100644 --- a/server/sonar-web/src/main/js/apps/application-console/__tests__/__snapshots__/ApplicationBranches-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/application-console/__tests__/__snapshots__/ApplicationBranches-test.tsx.snap @@ -135,7 +135,6 @@ exports[`should render correctly: creating branch 1`] = ` } onClose={[Function]} onCreate={[Function]} - onUpdate={[Function]} /> `; -- 2.39.5