repositoryStore.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. this.resetRepositories();
  26. },
  27. resetRepositories() {
  28. this.state = {
  29. orgSlug: undefined,
  30. repositories: undefined,
  31. repositoriesLoading: undefined,
  32. repositoriesError: undefined,
  33. };
  34. this.trigger(this.state);
  35. },
  36. loadRepositories(orgSlug: string) {
  37. this.state = {
  38. orgSlug,
  39. repositories: orgSlug === this.state.orgSlug ? this.state.repositories : undefined,
  40. repositoriesLoading: true,
  41. repositoriesError: undefined,
  42. };
  43. this.trigger(this.state);
  44. },
  45. loadRepositoriesError(err: Error) {
  46. this.state = {
  47. ...this.state,
  48. repositories: undefined,
  49. repositoriesLoading: false,
  50. repositoriesError: err,
  51. };
  52. this.trigger(this.state);
  53. },
  54. loadRepositoriesSuccess(data: Repository[]) {
  55. this.state = {
  56. ...this.state,
  57. repositories: data,
  58. repositoriesLoading: false,
  59. repositoriesError: undefined,
  60. };
  61. this.trigger(this.state);
  62. },
  63. get() {
  64. return {...this.state};
  65. },
  66. };
  67. const RepositoryStore = createStore(storeConfig);
  68. export default RepositoryStore;