repositoryStore.tsx 1.9 KB

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