pluginComponentBase.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Component} from 'react';
  2. import isFunction from 'lodash/isFunction';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. clearIndicators,
  8. } from 'sentry/actionCreators/indicator';
  9. import {Client} from 'sentry/api';
  10. import {FormState, GenericField} from 'sentry/components/deprecatedforms';
  11. import {t} from 'sentry/locale';
  12. const callbackWithArgs = function (context: any, callback: any, ...args: any) {
  13. return isFunction(callback) ? callback.bind(context, ...args) : undefined;
  14. };
  15. type GenericFieldProps = Parameters<typeof GenericField>[0];
  16. type Props = {};
  17. type State = {state: GenericFieldProps['formState']};
  18. class PluginComponentBase<
  19. P extends Props = Props,
  20. S extends State = State
  21. > extends Component<P, S> {
  22. constructor(props: P, context: any) {
  23. super(props, context);
  24. [
  25. 'onLoadSuccess',
  26. 'onLoadError',
  27. 'onSave',
  28. 'onSaveSuccess',
  29. 'onSaveError',
  30. 'onSaveComplete',
  31. 'renderField',
  32. ].map(method => (this[method] = this[method].bind(this)));
  33. if (this.fetchData) {
  34. this.fetchData = this.onLoad.bind(this, this.fetchData.bind(this));
  35. }
  36. if (this.onSubmit) {
  37. this.onSubmit = this.onSave.bind(this, this.onSubmit.bind(this));
  38. }
  39. this.state = {
  40. state: FormState.READY,
  41. } as Readonly<S>;
  42. }
  43. componentWillUnmount() {
  44. this.api.clear();
  45. window.clearTimeout(this.successMessageTimeout);
  46. window.clearTimeout(this.errorMessageTimeout);
  47. }
  48. successMessageTimeout: number | undefined = undefined;
  49. errorMessageTimeout: number | undefined = undefined;
  50. api = new Client();
  51. fetchData() {
  52. // Allow children to implement this
  53. }
  54. onSubmit() {
  55. // Allow children to implement this
  56. }
  57. onLoad(callback, ...args) {
  58. this.setState(
  59. {
  60. state: FormState.LOADING,
  61. },
  62. callbackWithArgs(this, callback, ...args)
  63. );
  64. }
  65. onLoadSuccess() {
  66. this.setState({
  67. state: FormState.READY,
  68. });
  69. }
  70. onLoadError(callback, ...args) {
  71. this.setState(
  72. {
  73. state: FormState.ERROR,
  74. },
  75. callbackWithArgs(this, callback, ...args)
  76. );
  77. addErrorMessage(t('An error occurred.'));
  78. }
  79. onSave(callback, ...args) {
  80. if (this.state.state === FormState.SAVING) {
  81. return;
  82. }
  83. callback = callbackWithArgs(this, callback, ...args);
  84. this.setState(
  85. {
  86. state: FormState.SAVING,
  87. },
  88. () => {
  89. addLoadingMessage(t('Saving changes\u2026'));
  90. callback && callback();
  91. }
  92. );
  93. }
  94. onSaveSuccess(callback, ...args) {
  95. callback = callbackWithArgs(this, callback, ...args);
  96. this.setState(
  97. {
  98. state: FormState.READY,
  99. },
  100. () => callback && callback()
  101. );
  102. window.clearTimeout(this.successMessageTimeout);
  103. this.successMessageTimeout = window.setTimeout(() => {
  104. addSuccessMessage(t('Success!'));
  105. }, 0);
  106. }
  107. onSaveError(callback, ...args) {
  108. callback = callbackWithArgs(this, callback, ...args);
  109. this.setState(
  110. {
  111. state: FormState.ERROR,
  112. },
  113. () => callback && callback()
  114. );
  115. window.clearTimeout(this.errorMessageTimeout);
  116. this.errorMessageTimeout = window.setTimeout(() => {
  117. addErrorMessage(t('Unable to save changes. Please try again.'));
  118. }, 0);
  119. }
  120. onSaveComplete(callback, ...args) {
  121. clearIndicators();
  122. callback = callbackWithArgs(this, callback, ...args);
  123. callback && callback();
  124. }
  125. renderField(props: Omit<GenericFieldProps, 'formState'>): React.ReactNode {
  126. props = {...props};
  127. const newProps = {
  128. ...props,
  129. formState: this.state.state,
  130. };
  131. return <GenericField key={newProps.config?.name} {...newProps} />;
  132. }
  133. }
  134. export default PluginComponentBase;