memberListStore.tsx 1.5 KB

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