adminSettings.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import isUndefined from 'lodash/isUndefined';
  2. import {ApiForm} from 'app/components/forms';
  3. import {t} from 'app/locale';
  4. import AsyncView from 'app/views/asyncView';
  5. import {getOption, getOptionField} from './options';
  6. const optionsAvailable = [
  7. 'system.url-prefix',
  8. 'system.admin-email',
  9. 'system.support-email',
  10. 'system.security-email',
  11. 'system.rate-limit',
  12. 'auth.allow-registration',
  13. 'auth.ip-rate-limit',
  14. 'auth.user-rate-limit',
  15. 'api.rate-limit.org-create',
  16. 'beacon.anonymous',
  17. ];
  18. type Field = ReturnType<typeof getOption>;
  19. type FieldDef = {
  20. field: Field;
  21. value: string | undefined;
  22. };
  23. type State = AsyncView['state'] & {
  24. data: Record<string, FieldDef>;
  25. };
  26. export default class AdminSettings extends AsyncView<{}, State> {
  27. get endpoint() {
  28. return '/internal/options/';
  29. }
  30. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  31. return [['data', this.endpoint]];
  32. }
  33. renderBody() {
  34. const {data} = this.state;
  35. const initialData = {};
  36. const fields = {};
  37. for (const key of optionsAvailable) {
  38. // TODO(dcramer): we should not be mutating options
  39. const option = data[key] ?? {field: {}, value: undefined};
  40. if (isUndefined(option.value) || option.value === '') {
  41. const defn = getOption(key);
  42. initialData[key] = defn.defaultValue ? defn.defaultValue() : '';
  43. } else {
  44. initialData[key] = option.value;
  45. }
  46. fields[key] = getOptionField(key, option.field);
  47. }
  48. return (
  49. <div>
  50. <h3>{t('Settings')}</h3>
  51. <ApiForm
  52. apiMethod="PUT"
  53. apiEndpoint={this.endpoint}
  54. initialData={initialData}
  55. omitDisabled
  56. requireChanges
  57. >
  58. <h4>General</h4>
  59. {fields['system.url-prefix']}
  60. {fields['system.admin-email']}
  61. {fields['system.support-email']}
  62. {fields['system.security-email']}
  63. {fields['system.rate-limit']}
  64. <h4>Security & Abuse</h4>
  65. {fields['auth.allow-registration']}
  66. {fields['auth.ip-rate-limit']}
  67. {fields['auth.user-rate-limit']}
  68. {fields['api.rate-limit.org-create']}
  69. <h4>Beacon</h4>
  70. {fields['beacon.anonymous']}
  71. </ApiForm>
  72. </div>
  73. );
  74. }
  75. }