organizationStore.tsx 2.3 KB

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