committerStore.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Reflux from 'reflux';
  2. import CommitterActions from 'app/actions/committerActions';
  3. import {Committer} from 'app/types';
  4. type CommitterStoreInterface = {
  5. state: {
  6. // Use `getCommitterStoreKey` to generate key
  7. [key: string]: {
  8. committers?: Committer[];
  9. committersLoading?: boolean;
  10. committersError?: Error;
  11. };
  12. };
  13. load(orgSlug: string, projectSlug: string, eventId: string): void;
  14. loadSuccess(
  15. orgSlug: string,
  16. projectSlug: string,
  17. eventId: string,
  18. data: Committer[]
  19. ): void;
  20. loadError(orgSlug: string, projectSlug: string, eventId: string, error: Error): void;
  21. get(
  22. orgSlug: string,
  23. projectSlug: string,
  24. eventId: string
  25. ): {
  26. committers?: Committer[];
  27. committersLoading?: boolean;
  28. committersError?: Error;
  29. };
  30. };
  31. export const CommitterStoreConfig: Reflux.StoreDefinition & CommitterStoreInterface = {
  32. listenables: CommitterActions,
  33. state: {},
  34. init() {
  35. this.reset();
  36. },
  37. reset() {
  38. this.state = {};
  39. this.trigger(this.state);
  40. },
  41. load(orgSlug: string, projectSlug: string, eventId: string) {
  42. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  43. this.state = {
  44. ...this.state,
  45. [key]: {
  46. committers: undefined,
  47. committersLoading: true,
  48. committersError: undefined,
  49. },
  50. };
  51. this.trigger(this.state);
  52. },
  53. loadError(orgSlug: string, projectSlug: string, eventId: string, err: Error) {
  54. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  55. this.state = {
  56. ...this.state,
  57. [key]: {
  58. committers: undefined,
  59. committersLoading: false,
  60. committersError: err,
  61. },
  62. };
  63. this.trigger(this.state);
  64. },
  65. loadSuccess(orgSlug: string, projectSlug: string, eventId: string, data: Committer[]) {
  66. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  67. this.state = {
  68. ...this.state,
  69. [key]: {
  70. committers: data,
  71. committersLoading: false,
  72. committersError: undefined,
  73. },
  74. };
  75. this.trigger(this.state);
  76. },
  77. get(orgSlug: string, projectSlug: string, eventId: string) {
  78. const key = getCommitterStoreKey(orgSlug, projectSlug, eventId);
  79. return {...this.state[key]};
  80. },
  81. };
  82. export function getCommitterStoreKey(
  83. orgSlug: string,
  84. projectSlug: string,
  85. eventId: string
  86. ): string {
  87. return `${orgSlug} ${projectSlug} ${eventId}`;
  88. }
  89. type CommitterStore = Reflux.Store & CommitterStoreInterface;
  90. const CommitterStore = Reflux.createStore(CommitterStoreConfig) as CommitterStore;
  91. export default CommitterStore;