repositoryStore.tsx 1.9 KB

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