repositoryStore.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import Reflux from 'reflux';
  2. import RepoActions from 'sentry/actions/repositoryActions';
  3. import {Repository} from 'sentry/types';
  4. type RepositoryStoreInterface = {
  5. get(): {
  6. orgSlug?: string;
  7. repositories?: Repository[];
  8. repositoriesError?: Error;
  9. repositoriesLoading?: boolean;
  10. };
  11. loadRepositories(orgSlug: string): void;
  12. loadRepositoriesError(error: Error): void;
  13. loadRepositoriesSuccess(data: Repository[]): void;
  14. state: {
  15. orgSlug?: string;
  16. repositories?: Repository[];
  17. repositoriesError?: Error;
  18. repositoriesLoading?: boolean;
  19. };
  20. };
  21. const storeConfig: Reflux.StoreDefinition & RepositoryStoreInterface = {
  22. listenables: RepoActions,
  23. state: {
  24. orgSlug: undefined,
  25. repositories: undefined,
  26. repositoriesLoading: undefined,
  27. repositoriesError: undefined,
  28. },
  29. init() {
  30. this.resetRepositories();
  31. },
  32. resetRepositories() {
  33. this.state = {
  34. orgSlug: undefined,
  35. repositories: undefined,
  36. repositoriesLoading: undefined,
  37. repositoriesError: undefined,
  38. };
  39. this.trigger(this.state);
  40. },
  41. loadRepositories(orgSlug: string) {
  42. this.state = {
  43. orgSlug,
  44. repositories: orgSlug === this.state.orgSlug ? this.state.repositories : undefined,
  45. repositoriesLoading: true,
  46. repositoriesError: undefined,
  47. };
  48. this.trigger(this.state);
  49. },
  50. loadRepositoriesError(err: Error) {
  51. this.state = {
  52. ...this.state,
  53. repositories: undefined,
  54. repositoriesLoading: false,
  55. repositoriesError: err,
  56. };
  57. this.trigger(this.state);
  58. },
  59. loadRepositoriesSuccess(data: Repository[]) {
  60. this.state = {
  61. ...this.state,
  62. repositories: data,
  63. repositoriesLoading: false,
  64. repositoriesError: undefined,
  65. };
  66. this.trigger(this.state);
  67. },
  68. get() {
  69. return {...this.state};
  70. },
  71. };
  72. const RepositoryStore = Reflux.createStore(storeConfig) as Reflux.Store &
  73. RepositoryStoreInterface;
  74. export default RepositoryStore;