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.

PageHeader.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 { Link } from 'react-router';
  22. import CreateProfileForm from './CreateProfileForm';
  23. import RestoreProfileForm from './RestoreProfileForm';
  24. import { Profile } from '../types';
  25. import { getProfilePath } from '../utils';
  26. import { Actions } from '../../../api/quality-profiles';
  27. import { Alert } from '../../../components/ui/Alert';
  28. import { Button } from '../../../components/ui/buttons';
  29. import { translate } from '../../../helpers/l10n';
  30. import { withRouter, Router } from '../../../components/hoc/withRouter';
  31. interface Props {
  32. actions: Actions;
  33. languages: Array<{ key: string; name: string }>;
  34. organization: string | null;
  35. profiles: Profile[];
  36. router: Pick<Router, 'push'>;
  37. updateProfiles: () => Promise<void>;
  38. }
  39. interface State {
  40. createFormOpen: boolean;
  41. restoreFormOpen: boolean;
  42. }
  43. export class PageHeader extends React.PureComponent<Props, State> {
  44. state: State = {
  45. createFormOpen: false,
  46. restoreFormOpen: false
  47. };
  48. handleCreateClick = () => {
  49. this.setState({ createFormOpen: true });
  50. };
  51. handleCreate = (profile: Profile) => {
  52. this.props.updateProfiles().then(
  53. () => {
  54. this.props.router.push(
  55. getProfilePath(profile.name, profile.language, this.props.organization)
  56. );
  57. },
  58. () => {}
  59. );
  60. };
  61. closeCreateForm = () => {
  62. this.setState({ createFormOpen: false });
  63. };
  64. handleRestoreClick = () => {
  65. this.setState({ restoreFormOpen: true });
  66. };
  67. closeRestoreForm = () => {
  68. this.setState({ restoreFormOpen: false });
  69. };
  70. render() {
  71. const { actions, languages, organization, profiles } = this.props;
  72. return (
  73. <header className="page-header">
  74. <h1 className="page-title">{translate('quality_profiles.page')}</h1>
  75. {actions.create && (
  76. <div className="page-actions">
  77. <Button
  78. disabled={languages.length === 0}
  79. id="quality-profiles-create"
  80. onClick={this.handleCreateClick}>
  81. {translate('create')}
  82. </Button>
  83. <Button
  84. className="little-spacer-left"
  85. id="quality-profiles-restore"
  86. onClick={this.handleRestoreClick}>
  87. {translate('restore')}
  88. </Button>
  89. {languages.length === 0 && (
  90. <Alert className="spacer-top" variant="warning">
  91. {translate('quality_profiles.no_languages_available')}
  92. </Alert>
  93. )}
  94. </div>
  95. )}
  96. <div className="page-description markdown">
  97. {translate('quality_profiles.intro1')}
  98. <br />
  99. {translate('quality_profiles.intro2')}
  100. <Link
  101. className="spacer-left"
  102. target="_blank"
  103. to={{
  104. pathname: '/documentation/instance-administration/quality-profiles/'
  105. }}>
  106. {translate('learn_more')}
  107. </Link>
  108. </div>
  109. {this.state.restoreFormOpen && (
  110. <RestoreProfileForm
  111. onClose={this.closeRestoreForm}
  112. onRestore={this.props.updateProfiles}
  113. organization={organization}
  114. />
  115. )}
  116. {this.state.createFormOpen && (
  117. <CreateProfileForm
  118. languages={languages}
  119. onClose={this.closeCreateForm}
  120. onCreate={this.handleCreate}
  121. organization={organization}
  122. profiles={profiles}
  123. />
  124. )}
  125. </header>
  126. );
  127. }
  128. }
  129. export default withRouter(PageHeader);