aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/application-console/BranchRowActions.tsx
blob: 15538900ddbe00eff0a44d651e9e94ece41a41c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * SonarQube
 * Copyright (C) 2009-2020 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 { without } from 'lodash';
import * as React from 'react';
import ActionsDropdown, {
  ActionsDropdownItem
} from 'sonar-ui-common/components/controls/ActionsDropdown';
import ConfirmButton from 'sonar-ui-common/components/controls/ConfirmButton';
import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
import { deleteApplicationBranch } from '../../api/application';
import { Application } from '../../types/application';
import CreateBranchForm from './CreateBranchForm';
import { ApplicationBranch } from './utils';

interface Props {
  application: Application;
  branch: ApplicationBranch;
  onUpdateBranches: (branches: Array<ApplicationBranch>) => void;
}

interface State {
  isUpdating: boolean;
}

export default class BranchRowActions extends React.PureComponent<Props, State> {
  state: State = { isUpdating: false };

  handleDelete = () => {
    const { application, branch } = this.props;
    return deleteApplicationBranch(application.key, branch.name).then(() => {
      this.props.onUpdateBranches(without(application.branches, branch));
    });
  };

  handleUpdate = (newBranchName: string) => {
    this.props.onUpdateBranches(
      this.props.application.branches.map(branch => {
        if (branch.name === this.props.branch.name) {
          branch.name = newBranchName;
        }
        return branch;
      })
    );
  };

  handleCloseForm = () => {
    this.setState({ isUpdating: false });
  };

  handleUpdateClick = () => {
    this.setState({ isUpdating: true });
  };

  render() {
    return (
      <>
        <ConfirmButton
          confirmButtonText={translate('delete')}
          isDestructive={true}
          modalBody={translateWithParameters(
            'application_console.branches.delete.warning_x',
            this.props.branch.name
          )}
          modalHeader={translate('application_console.branches.delete')}
          onConfirm={this.handleDelete}>
          {({ onClick }) => (
            <ActionsDropdown>
              <ActionsDropdownItem onClick={this.handleUpdateClick}>
                {translate('edit')}
              </ActionsDropdownItem>
              <ActionsDropdownItem destructive={true} onClick={onClick}>
                {translate('delete')}
              </ActionsDropdownItem>
            </ActionsDropdown>
          )}
        </ConfirmButton>

        {this.state.isUpdating && (
          <CreateBranchForm
            application={this.props.application}
            branch={this.props.branch}
            enabledProjectsKey={this.props.application.projects
              .filter(p => p.enabled)
              .map(p => p.key)}
            onClose={this.handleCloseForm}
            onCreate={() => {}}
            onUpdate={this.handleUpdate}
          />
        )}
      </>
    );
  }
}