pluginComponentBase.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import * as React 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 React.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. }
  46. api = new Client();
  47. fetchData() {
  48. // Allow children to implement this
  49. }
  50. onSubmit() {
  51. // Allow children to implement this
  52. }
  53. onLoad(callback, ...args) {
  54. this.setState(
  55. {
  56. state: FormState.LOADING,
  57. },
  58. callbackWithArgs(this, callback, ...args)
  59. );
  60. }
  61. onLoadSuccess() {
  62. this.setState({
  63. state: FormState.READY,
  64. });
  65. }
  66. onLoadError(callback, ...args) {
  67. this.setState(
  68. {
  69. state: FormState.ERROR,
  70. },
  71. callbackWithArgs(this, callback, ...args)
  72. );
  73. addErrorMessage(t('An error occurred.'));
  74. }
  75. onSave(callback, ...args) {
  76. if (this.state.state === FormState.SAVING) {
  77. return;
  78. }
  79. callback = callbackWithArgs(this, callback, ...args);
  80. this.setState(
  81. {
  82. state: FormState.SAVING,
  83. },
  84. () => {
  85. addLoadingMessage(t('Saving changes\u2026'));
  86. callback && callback();
  87. }
  88. );
  89. }
  90. onSaveSuccess(callback, ...args) {
  91. callback = callbackWithArgs(this, callback, ...args);
  92. this.setState(
  93. {
  94. state: FormState.READY,
  95. },
  96. () => callback && callback()
  97. );
  98. setTimeout(() => {
  99. addSuccessMessage(t('Success!'));
  100. }, 0);
  101. }
  102. onSaveError(callback, ...args) {
  103. callback = callbackWithArgs(this, callback, ...args);
  104. this.setState(
  105. {
  106. state: FormState.ERROR,
  107. },
  108. () => callback && callback()
  109. );
  110. setTimeout(() => {
  111. addErrorMessage(t('Unable to save changes. Please try again.'));
  112. }, 0);
  113. }
  114. onSaveComplete(callback, ...args) {
  115. clearIndicators();
  116. callback = callbackWithArgs(this, callback, ...args);
  117. callback && callback();
  118. }
  119. renderField(props: Omit<GenericFieldProps, 'formState'>): React.ReactNode {
  120. props = {...props};
  121. const newProps = {
  122. ...props,
  123. formState: this.state.state,
  124. };
  125. return <GenericField key={newProps.config?.name} {...newProps} />;
  126. }
  127. }
  128. export default PluginComponentBase;