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.

SubCategoryDefinitionsList.tsx 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { groupBy, isEqual, sortBy } from 'lodash';
  21. import * as React from 'react';
  22. import { getSubCategoryDescription, getSubCategoryName, sanitizeTranslation } from '../utils';
  23. import DefinitionsList from './DefinitionsList';
  24. import EmailForm from './EmailForm';
  25. interface Props {
  26. category: string;
  27. component?: T.Component;
  28. fetchValues: Function;
  29. settings: Array<T.Setting & { definition: T.SettingCategoryDefinition }>;
  30. subCategory?: string;
  31. }
  32. export default class SubCategoryDefinitionsList extends React.PureComponent<Props> {
  33. componentDidMount() {
  34. this.fetchValues();
  35. }
  36. componentDidUpdate(prevProps: Props) {
  37. const prevKeys = prevProps.settings.map(setting => setting.definition.key);
  38. const keys = this.props.settings.map(setting => setting.definition.key);
  39. if (prevProps.component !== this.props.component || !isEqual(prevKeys, keys)) {
  40. this.fetchValues();
  41. }
  42. }
  43. fetchValues() {
  44. const keys = this.props.settings.map(setting => setting.definition.key).join();
  45. this.props.fetchValues(keys, this.props.component && this.props.component.key);
  46. }
  47. renderEmailForm = (subCategoryKey: string) => {
  48. const isEmailSettings = this.props.category === 'general' && subCategoryKey === 'email';
  49. if (!isEmailSettings) {
  50. return null;
  51. }
  52. return <EmailForm />;
  53. };
  54. render() {
  55. const bySubCategory = groupBy(this.props.settings, setting => setting.definition.subCategory);
  56. const subCategories = Object.keys(bySubCategory).map(key => ({
  57. key,
  58. name: getSubCategoryName(bySubCategory[key][0].definition.category, key),
  59. description: getSubCategoryDescription(bySubCategory[key][0].definition.category, key)
  60. }));
  61. const sortedSubCategories = sortBy(subCategories, subCategory =>
  62. subCategory.name.toLowerCase()
  63. );
  64. const filteredSubCategories = this.props.subCategory
  65. ? sortedSubCategories.filter(c => c.key === this.props.subCategory)
  66. : sortedSubCategories;
  67. return (
  68. <ul className="settings-sub-categories-list">
  69. {filteredSubCategories.map(subCategory => (
  70. <li key={subCategory.key}>
  71. <h2 className="settings-sub-category-name">{subCategory.name}</h2>
  72. {subCategory.description != null && (
  73. <div
  74. className="settings-sub-category-description markdown"
  75. dangerouslySetInnerHTML={{ __html: sanitizeTranslation(subCategory.description) }}
  76. />
  77. )}
  78. <DefinitionsList
  79. component={this.props.component}
  80. settings={bySubCategory[subCategory.key]}
  81. />
  82. {this.renderEmailForm(subCategory.key)}
  83. </li>
  84. ))}
  85. </ul>
  86. );
  87. }
  88. }