pluginComponentBase.tsx 3.9 KB

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