repositoryStore.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type {StoreDefinition} from 'reflux';
  2. import {createStore} from 'reflux';
  3. import type {Repository} from 'sentry/types/integrations';
  4. type State = {
  5. orgSlug?: string;
  6. repositories?: Repository[];
  7. repositoriesError?: Error;
  8. repositoriesLoading?: boolean;
  9. };
  10. interface RepositoryStoreDefinition extends StoreDefinition {
  11. get(): State;
  12. init(): void;
  13. loadRepositories(orgSlug: string): void;
  14. loadRepositoriesError(error: Error): void;
  15. loadRepositoriesSuccess(data: Repository[]): void;
  16. resetRepositories(): void;
  17. state: State;
  18. }
  19. const storeConfig: RepositoryStoreDefinition = {
  20. state: {
  21. orgSlug: undefined,
  22. repositories: undefined,
  23. repositoriesLoading: undefined,
  24. repositoriesError: undefined,
  25. },
  26. init() {
  27. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  28. // listeners due to their leaky nature in tests.
  29. this.resetRepositories();
  30. },
  31. resetRepositories() {
  32. this.state = {
  33. orgSlug: undefined,
  34. repositories: undefined,
  35. repositoriesLoading: undefined,
  36. repositoriesError: undefined,
  37. };
  38. this.trigger(this.state);
  39. },
  40. loadRepositories(orgSlug: string) {
  41. this.state = {
  42. orgSlug,
  43. repositories: orgSlug === this.state.orgSlug ? this.state.repositories : undefined,
  44. repositoriesLoading: true,
  45. repositoriesError: undefined,
  46. };
  47. this.trigger(this.state);
  48. },
  49. loadRepositoriesError(err: Error) {
  50. this.state = {
  51. ...this.state,
  52. repositories: undefined,
  53. repositoriesLoading: false,
  54. repositoriesError: err,
  55. };
  56. this.trigger(this.state);
  57. },
  58. loadRepositoriesSuccess(data: Repository[]) {
  59. this.state = {
  60. ...this.state,
  61. repositories: data,
  62. repositoriesLoading: false,
  63. repositoriesError: undefined,
  64. };
  65. this.trigger(this.state);
  66. },
  67. get() {
  68. return {...this.state};
  69. },
  70. };
  71. const RepositoryStore = createStore(storeConfig);
  72. export default RepositoryStore;