organizationEnvironmentsStore.tsx 2.3 KB

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