memberListStore.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {User} from 'sentry/types';
  3. interface MemberListStoreDefinition extends StoreDefinition {
  4. getAll(): User[];
  5. getById(id: string): User | undefined;
  6. getState(): User[];
  7. init(): void;
  8. isLoaded(): boolean;
  9. loadInitialData(items: User[]): void;
  10. loaded: boolean;
  11. state: User[];
  12. }
  13. const storeConfig: MemberListStoreDefinition = {
  14. loaded: false,
  15. state: [],
  16. init() {
  17. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  18. // listeners due to their leaky nature in tests.
  19. this.state = [];
  20. this.loaded = false;
  21. },
  22. // TODO(dcramer): this should actually come from an action of some sorts
  23. loadInitialData(items: User[]) {
  24. this.state = items;
  25. this.loaded = true;
  26. this.trigger(this.state, 'initial');
  27. },
  28. isLoaded() {
  29. return this.loaded;
  30. },
  31. getById(id) {
  32. if (!this.state) {
  33. return undefined;
  34. }
  35. id = '' + id;
  36. for (let i = 0; i < this.state.length; i++) {
  37. if (this.state[i].id === id) {
  38. return this.state[i];
  39. }
  40. }
  41. return undefined;
  42. },
  43. getAll() {
  44. return this.state;
  45. },
  46. getState() {
  47. return this.state;
  48. },
  49. };
  50. const MemberListStore = createStore(storeConfig);
  51. export default MemberListStore;