organizationStore.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. this.reset();
  26. },
  27. reset() {
  28. this.loading = true;
  29. this.error = null;
  30. this.errorType = null;
  31. this.organization = null;
  32. this.dirty = false;
  33. this.trigger(this.get());
  34. },
  35. onUpdate(updatedOrg: Organization, {replace = false} = {}) {
  36. this.loading = false;
  37. this.error = null;
  38. this.errorType = null;
  39. this.organization = replace ? updatedOrg : {...this.organization, ...updatedOrg};
  40. this.dirty = false;
  41. this.trigger(this.get());
  42. ReleaseStore.updateOrganization(this.organization);
  43. LatestContextStore.onUpdateOrganization(this.organization);
  44. },
  45. onFetchOrgError(err) {
  46. this.organization = null;
  47. this.errorType = null;
  48. switch (err?.status) {
  49. case 401:
  50. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS;
  51. break;
  52. case 404:
  53. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND;
  54. break;
  55. default:
  56. }
  57. this.loading = false;
  58. this.error = err;
  59. this.dirty = false;
  60. this.trigger(this.get());
  61. },
  62. get() {
  63. return {
  64. organization: this.organization,
  65. error: this.error,
  66. loading: this.loading,
  67. errorType: this.errorType,
  68. dirty: this.dirty,
  69. };
  70. },
  71. getState() {
  72. return this.get();
  73. },
  74. };
  75. const OrganizationStore = createStore(storeConfig);
  76. export default OrganizationStore;