organizationEnvironmentsStore.tsx 2.0 KB

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