committerStore.tsx 2.5 KB

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