You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { Button } from 'sonar-ui-common/components/controls/buttons';
  22. import ConfirmButton from 'sonar-ui-common/components/controls/ConfirmButton';
  23. import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
  24. import { deleteApplication } from '../../api/application';
  25. import { deletePortfolio, deleteProject } from '../../api/components';
  26. import addGlobalSuccessMessage from '../../app/utils/addGlobalSuccessMessage';
  27. import { Router, withRouter } from '../../components/hoc/withRouter';
  28. import { isApplication, isPortfolioLike } from '../../types/component';
  29. interface Props {
  30. component: Pick<T.Component, 'key' | 'name' | 'qualifier'>;
  31. router: Pick<Router, 'replace'>;
  32. }
  33. export class Form extends React.PureComponent<Props> {
  34. handleDelete = async () => {
  35. const { component } = this.props;
  36. let deleteMethod = deleteProject;
  37. let redirectTo = '/';
  38. if (isPortfolioLike(component.qualifier)) {
  39. deleteMethod = deletePortfolio;
  40. redirectTo = '/portfolios';
  41. } else if (isApplication(component.qualifier)) {
  42. deleteMethod = deleteApplication;
  43. }
  44. await deleteMethod(component.key);
  45. addGlobalSuccessMessage(
  46. translateWithParameters('project_deletion.resource_deleted', component.name)
  47. );
  48. this.props.router.replace(redirectTo);
  49. };
  50. render() {
  51. const { component } = this.props;
  52. return (
  53. <ConfirmButton
  54. confirmButtonText={translate('delete')}
  55. isDestructive={true}
  56. modalBody={translateWithParameters(
  57. 'project_deletion.delete_resource_confirmation',
  58. component.name
  59. )}
  60. modalHeader={translate('qualifier.delete', component.qualifier)}
  61. onConfirm={this.handleDelete}>
  62. {({ onClick }) => (
  63. <Button className="button-red" id="delete-project" onClick={onClick}>
  64. {translate('delete')}
  65. </Button>
  66. )}
  67. </ConfirmButton>
  68. );
  69. }
  70. }
  71. export default withRouter(Form);