organizationStore.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import OrganizationActions from 'sentry/actions/organizationActions';
  3. import {ORGANIZATION_FETCH_ERROR_TYPES} from 'sentry/constants';
  4. import {Organization} from 'sentry/types';
  5. import {
  6. makeSafeRefluxStore,
  7. SafeRefluxStore,
  8. SafeStoreDefinition,
  9. } from 'sentry/utils/makeSafeRefluxStore';
  10. import RequestError from 'sentry/utils/requestError/requestError';
  11. import {CommonStoreInterface} from './types';
  12. type UpdateOptions = {
  13. replace?: boolean;
  14. };
  15. type State = {
  16. dirty: boolean;
  17. loading: boolean;
  18. organization: Organization | null;
  19. error?: RequestError | null;
  20. errorType?: string | null;
  21. };
  22. type OrganizationStoreInterface = CommonStoreInterface<State> & {
  23. get(): State;
  24. init(): void;
  25. onFetchOrgError(err: RequestError): void;
  26. onUpdate(org: Organization, options: UpdateOptions): void;
  27. reset(): void;
  28. };
  29. const storeConfig: StoreDefinition & OrganizationStoreInterface & SafeStoreDefinition = {
  30. unsubscribeListeners: [],
  31. init() {
  32. this.reset();
  33. this.unsubscribeListeners.push(
  34. this.listenTo(OrganizationActions.update, this.onUpdate)
  35. );
  36. this.unsubscribeListeners.push(this.listenTo(OrganizationActions.reset, this.reset));
  37. this.unsubscribeListeners.push(
  38. this.listenTo(OrganizationActions.fetchOrgError, this.onFetchOrgError)
  39. );
  40. },
  41. reset() {
  42. this.loading = true;
  43. this.error = null;
  44. this.errorType = null;
  45. this.organization = null;
  46. this.dirty = false;
  47. this.trigger(this.get());
  48. },
  49. onUpdate(updatedOrg: Organization, {replace = false}: UpdateOptions = {}) {
  50. this.loading = false;
  51. this.error = null;
  52. this.errorType = null;
  53. this.organization = replace ? updatedOrg : {...this.organization, ...updatedOrg};
  54. this.dirty = false;
  55. this.trigger(this.get());
  56. },
  57. onFetchOrgError(err: RequestError) {
  58. this.organization = null;
  59. this.errorType = null;
  60. switch (err?.status) {
  61. case 401:
  62. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS;
  63. break;
  64. case 404:
  65. this.errorType = ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND;
  66. break;
  67. default:
  68. }
  69. this.loading = false;
  70. this.error = err;
  71. this.dirty = false;
  72. this.trigger(this.get());
  73. },
  74. get() {
  75. return {
  76. organization: this.organization,
  77. error: this.error,
  78. loading: this.loading,
  79. errorType: this.errorType,
  80. dirty: this.dirty,
  81. };
  82. },
  83. getState() {
  84. return this.get();
  85. },
  86. };
  87. const OrganizationStore = createStore(
  88. makeSafeRefluxStore(storeConfig)
  89. ) as SafeRefluxStore & OrganizationStoreInterface;
  90. export default OrganizationStore;