organizationEnvironmentsStore.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. this.state = {environments: null, error: null};
  27. },
  28. makeEnvironment(item: Environment): EnhancedEnvironment {
  29. return {
  30. id: item.id,
  31. name: item.name,
  32. get displayName() {
  33. return getDisplayName(item);
  34. },
  35. get urlRoutingName() {
  36. return getUrlRoutingName(item);
  37. },
  38. };
  39. },
  40. onFetchEnvironments() {
  41. this.state = {environments: null, error: null};
  42. this.trigger(this.state);
  43. },
  44. onFetchEnvironmentsSuccess(environments) {
  45. this.state = {error: null, environments: environments.map(this.makeEnvironment)};
  46. this.trigger(this.state);
  47. },
  48. onFetchEnvironmentsError(error) {
  49. this.state = {error, environments: null};
  50. this.trigger(this.state);
  51. },
  52. getState() {
  53. return this.state;
  54. },
  55. };
  56. const OrganizationEnvironmentsStore = createStore(storeConfig);
  57. export default OrganizationEnvironmentsStore;