memberListStore.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {User} from 'sentry/types';
  3. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  4. interface MemberListStoreDefinition extends StoreDefinition {
  5. getAll(): User[];
  6. getByEmail(email: string): User | undefined;
  7. getById(id: string): User | undefined;
  8. init(): void;
  9. isLoaded(): boolean;
  10. loadInitialData(items: User[]): void;
  11. loaded: boolean;
  12. state: User[];
  13. }
  14. const storeConfig: MemberListStoreDefinition = {
  15. // listenables: MemberActions,
  16. loaded: false,
  17. state: [],
  18. init() {
  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. getByEmail(email) {
  44. if (!this.state) {
  45. return undefined;
  46. }
  47. email = email.toLowerCase();
  48. for (let i = 0; i < this.state.length; i++) {
  49. if (this.state[i].email.toLowerCase() === email) {
  50. return this.state[i];
  51. }
  52. }
  53. return undefined;
  54. },
  55. getAll() {
  56. return this.state;
  57. },
  58. };
  59. const MemberListStore = createStore(makeSafeRefluxStore(storeConfig));
  60. export default MemberListStore;