memberListStore.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {User} from 'sentry/types';
  3. type State = {
  4. cursor: string | null;
  5. hasMore: boolean | null;
  6. loading: boolean;
  7. members: User[];
  8. };
  9. // XXX(epurkhiser): Either this store is completely wrong, or it is misnamed, a
  10. // `Member` has one `User`, this stores users not members.
  11. interface MemberListStoreDefinition extends StoreDefinition {
  12. getAll(): User[];
  13. getById(memberId: string): User | undefined;
  14. getState(): State;
  15. init(): void;
  16. loadInitialData(items: User[], hasMore?: boolean | null, cursor?: string | null): void;
  17. reset(): void;
  18. state: State;
  19. }
  20. const storeConfig: MemberListStoreDefinition = {
  21. state: {
  22. members: [],
  23. loading: true,
  24. hasMore: null,
  25. cursor: null,
  26. },
  27. init() {
  28. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  29. // listeners due to their leaky nature in tests.
  30. this.reset();
  31. },
  32. reset() {
  33. this.state = {
  34. members: [],
  35. loading: true,
  36. hasMore: null,
  37. cursor: null,
  38. };
  39. },
  40. loadInitialData(items: User[], hasMore, cursor) {
  41. this.state = {
  42. members: items,
  43. loading: false,
  44. hasMore: hasMore ?? this.state.hasMore,
  45. cursor: cursor ?? this.state.cursor,
  46. };
  47. this.trigger(this.state, 'initial');
  48. },
  49. getById(memberId) {
  50. return this.state.members.find(({id}) => memberId === id);
  51. },
  52. getAll() {
  53. return this.state.members;
  54. },
  55. getState() {
  56. return this.state;
  57. },
  58. };
  59. const MemberListStore = createStore(storeConfig);
  60. export default MemberListStore;