memberListStore.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. this.state = [];
  18. this.loaded = false;
  19. },
  20. // TODO(dcramer): this should actually come from an action of some sorts
  21. loadInitialData(items: User[]) {
  22. this.state = items;
  23. this.loaded = true;
  24. this.trigger(this.state, 'initial');
  25. },
  26. isLoaded() {
  27. return this.loaded;
  28. },
  29. getById(id) {
  30. if (!this.state) {
  31. return undefined;
  32. }
  33. id = '' + id;
  34. for (let i = 0; i < this.state.length; i++) {
  35. if (this.state[i].id === id) {
  36. return this.state[i];
  37. }
  38. }
  39. return undefined;
  40. },
  41. getAll() {
  42. return this.state;
  43. },
  44. getState() {
  45. return this.state;
  46. },
  47. };
  48. const MemberListStore = createStore(storeConfig);
  49. export default MemberListStore;