organizationEnvironmentsStore.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {createStore, Store, StoreDefinition} from 'reflux';
  2. import EnvironmentActions from 'sentry/actions/environmentActions';
  3. import {Environment} from 'sentry/types';
  4. import {getDisplayName, getUrlRoutingName} from 'sentry/utils/environment';
  5. import {makeSafeRefluxStore, SafeStoreDefinition} from 'sentry/utils/makeSafeRefluxStore';
  6. type EnhancedEnvironment = Environment & {
  7. displayName: string;
  8. urlRoutingName: string;
  9. };
  10. type State = {
  11. environments: EnhancedEnvironment[] | null;
  12. error: Error | null;
  13. };
  14. type OrganizationEnvironmentsStoreInterface = {
  15. get(): State;
  16. init(): void;
  17. onFetchEnvironments(): void;
  18. onFetchEnvironmentsError(error: Error): void;
  19. onFetchEnvironmentsSuccess(environments: Environment[]): void;
  20. state: State;
  21. };
  22. const storeConfig: StoreDefinition &
  23. OrganizationEnvironmentsStoreInterface &
  24. SafeStoreDefinition = {
  25. unsubscribeListeners: [],
  26. state: {
  27. environments: null,
  28. error: null,
  29. },
  30. init() {
  31. this.state = {environments: null, error: null};
  32. this.unsubscribeListeners.push(
  33. this.listenTo(EnvironmentActions.fetchEnvironments, this.onFetchEnvironments)
  34. );
  35. this.unsubscribeListeners.push(
  36. this.listenTo(
  37. EnvironmentActions.fetchEnvironmentsSuccess,
  38. this.onFetchEnvironmentsSuccess
  39. )
  40. );
  41. this.unsubscribeListeners.push(
  42. this.listenTo(
  43. EnvironmentActions.fetchEnvironmentsError,
  44. this.onFetchEnvironmentsError
  45. )
  46. );
  47. },
  48. makeEnvironment(item: Environment): EnhancedEnvironment {
  49. return {
  50. id: item.id,
  51. name: item.name,
  52. get displayName() {
  53. return getDisplayName(item);
  54. },
  55. get urlRoutingName() {
  56. return getUrlRoutingName(item);
  57. },
  58. };
  59. },
  60. onFetchEnvironments() {
  61. this.state = {environments: null, error: null};
  62. this.trigger(this.state);
  63. },
  64. onFetchEnvironmentsSuccess(environments) {
  65. this.state = {error: null, environments: environments.map(this.makeEnvironment)};
  66. this.trigger(this.state);
  67. },
  68. onFetchEnvironmentsError(error) {
  69. this.state = {error, environments: null};
  70. this.trigger(this.state);
  71. },
  72. get() {
  73. return this.state;
  74. },
  75. };
  76. const OrganizationEnvironmentsStore = createStore(
  77. makeSafeRefluxStore(storeConfig)
  78. ) as Store & OrganizationEnvironmentsStoreInterface;
  79. export default OrganizationEnvironmentsStore;