organizationEnvironmentsStore.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {createStore} from 'reflux';
  2. import {Environment} from 'sentry/types';
  3. import {getDisplayName, getUrlRoutingName} from 'sentry/utils/environment';
  4. import {CommonStoreDefinition} from './types';
  5. type EnhancedEnvironment = Environment & {
  6. displayName: string;
  7. urlRoutingName: string;
  8. };
  9. type State = {
  10. environments: EnhancedEnvironment[] | null;
  11. error: Error | null;
  12. };
  13. interface OrganizationEnvironmentsStoreDefinition extends CommonStoreDefinition<State> {
  14. init(): void;
  15. onFetchEnvironments(): void;
  16. onFetchEnvironmentsError(error: Error): void;
  17. onFetchEnvironmentsSuccess(environments: Environment[]): void;
  18. state: State;
  19. }
  20. const storeConfig: OrganizationEnvironmentsStoreDefinition = {
  21. state: {
  22. environments: null,
  23. error: null,
  24. },
  25. init() {
  26. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  27. // listeners due to their leaky nature in tests.
  28. this.state = {environments: null, error: null};
  29. },
  30. makeEnvironment(item: Environment): EnhancedEnvironment {
  31. return {
  32. id: item.id,
  33. name: item.name,
  34. get displayName() {
  35. return getDisplayName(item);
  36. },
  37. get urlRoutingName() {
  38. return getUrlRoutingName(item);
  39. },
  40. };
  41. },
  42. onFetchEnvironments() {
  43. this.state = {environments: null, error: null};
  44. this.trigger(this.state);
  45. },
  46. onFetchEnvironmentsSuccess(environments) {
  47. this.state = {error: null, environments: environments.map(this.makeEnvironment)};
  48. this.trigger(this.state);
  49. },
  50. onFetchEnvironmentsError(error) {
  51. this.state = {error, environments: null};
  52. this.trigger(this.state);
  53. },
  54. getState() {
  55. return this.state;
  56. },
  57. };
  58. const OrganizationEnvironmentsStore = createStore(storeConfig);
  59. export default OrganizationEnvironmentsStore;