repositoryStore.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {Repository} from 'sentry/types';
  3. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  4. interface RepositoryStoreDefinition extends StoreDefinition {
  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. resetRepositories(): void;
  15. state: {
  16. orgSlug?: string;
  17. repositories?: Repository[];
  18. repositoriesError?: Error;
  19. repositoriesLoading?: boolean;
  20. };
  21. }
  22. const storeConfig: RepositoryStoreDefinition = {
  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 = createStore(makeSafeRefluxStore(storeConfig));
  73. export default RepositoryStore;