organizationEnvironmentsStore.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {createStore} from 'reflux';
  2. import {Environment} from 'sentry/types';
  3. import {getDisplayName, getUrlRoutingName} from 'sentry/utils/environment';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. import {CommonStoreDefinition} from './types';
  6. type EnhancedEnvironment = Environment & {
  7. displayName: string;
  8. urlRoutingName: string;
  9. };
  10. type State = {
  11. environments: EnhancedEnvironment[] | null;
  12. error: Error | null;
  13. };
  14. interface OrganizationEnvironmentsStoreDefinition extends CommonStoreDefinition<State> {
  15. init(): void;
  16. onFetchEnvironments(): void;
  17. onFetchEnvironmentsError(error: Error): void;
  18. onFetchEnvironmentsSuccess(environments: Environment[]): void;
  19. state: State;
  20. }
  21. const storeConfig: OrganizationEnvironmentsStoreDefinition = {
  22. unsubscribeListeners: [],
  23. state: {
  24. environments: null,
  25. error: null,
  26. },
  27. init() {
  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(makeSafeRefluxStore(storeConfig));
  59. export default OrganizationEnvironmentsStore;