organizationStore.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {createStore} from 'reflux';
  2. import {ORGANIZATION_FETCH_ERROR_TYPES} from 'sentry/constants';
  3. import {Organization} from 'sentry/types';
  4. import RequestError from 'sentry/utils/requestError/requestError';
  5. import HookStore from './hookStore';
  6. import LatestContextStore from './latestContextStore';
  7. import ReleaseStore from './releaseStore';
  8. import {CommonStoreDefinition} from './types';
  9. type State = {
  10. dirty: boolean;
  11. loading: boolean;
  12. organization: Organization | null;
  13. error?: RequestError | null;
  14. errorType?: string | null;
  15. };
  16. interface OrganizationStoreDefinition extends CommonStoreDefinition<State> {
  17. get(): State;
  18. init(): void;
  19. onFetchOrgError(err: RequestError): void;
  20. onUpdate(org: Organization, options?: {replace: true}): void;
  21. onUpdate(org: Partial<Organization>, options?: {replace?: false}): void;
  22. reset(): void;
  23. }
  24. const storeConfig: OrganizationStoreDefinition = {
  25. init() {
  26. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  27. // listeners due to their leaky nature in tests.
  28. this.reset();
  29. },
  30. reset() {
  31. this.loading = true;
  32. this.error = null;
  33. this.errorType = null;
  34. this.organization = null;
  35. this.dirty = false;
  36. this.trigger(this.get());
  37. },
  38. onUpdate(updatedOrg: Organization, {replace = false} = {}) {
  39. this.loading = false;
  40. this.error = null;
  41. this.errorType = null;
  42. this.organization = replace ? updatedOrg : {...this.organization, ...updatedOrg};
  43. this.dirty = false;
  44. this.trigger(this.get());
  45. ReleaseStore.updateOrganization(this.organization);
  46. LatestContextStore.onUpdateOrganization(this.organization);
  47. HookStore.getCallback(
  48. 'react-hook:route-activated',
  49. 'setOrganization'
  50. )?.(this.organization);
  51. },
  52. onFetchOrgError(err) {
  53. this.organization = null;
  54. this.errorType = null;
  55. switch (err?.status) {
  56. case 401:
  57. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS;
  58. break;
  59. case 404:
  60. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND;
  61. break;
  62. default:
  63. }
  64. this.loading = false;
  65. this.error = err;
  66. this.dirty = false;
  67. this.trigger(this.get());
  68. },
  69. get() {
  70. return {
  71. organization: this.organization,
  72. error: this.error,
  73. loading: this.loading,
  74. errorType: this.errorType,
  75. dirty: this.dirty,
  76. };
  77. },
  78. getState() {
  79. return this.get();
  80. },
  81. };
  82. const OrganizationStore = createStore(storeConfig);
  83. export default OrganizationStore;