repositoryStore.tsx 2.0 KB

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