preferencesStore.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {createStore} from 'reflux';
  2. import {CommonStoreDefinition} from './types';
  3. type Preferences = {
  4. /**
  5. * Is the sidebar collapsed to the side
  6. */
  7. collapsed?: boolean;
  8. };
  9. interface PreferenceStoreDefinition extends CommonStoreDefinition<Preferences> {
  10. getInitialState(): Preferences;
  11. hideSidebar(): void;
  12. loadInitialState(prefs: Preferences): void;
  13. prefs: Preferences;
  14. reset(): void;
  15. showSidebar(): void;
  16. }
  17. const storeConfig: PreferenceStoreDefinition = {
  18. prefs: {},
  19. init() {
  20. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  21. // listeners due to their leaky nature in tests.
  22. this.reset();
  23. },
  24. getInitialState() {
  25. return this.prefs;
  26. },
  27. reset() {
  28. this.prefs = {collapsed: false};
  29. },
  30. loadInitialState(prefs) {
  31. this.prefs = {...prefs};
  32. this.trigger(this.prefs);
  33. },
  34. hideSidebar() {
  35. this.prefs = {...this.prefs, collapsed: true};
  36. this.trigger(this.prefs);
  37. },
  38. showSidebar() {
  39. this.prefs = {...this.prefs, collapsed: false};
  40. this.trigger(this.prefs);
  41. },
  42. getState() {
  43. return this.prefs;
  44. },
  45. };
  46. /**
  47. * This store is used to hold local user preferences
  48. * Side-effects (like reading/writing to cookies) are done in associated actionCreators
  49. */
  50. const PreferenceStore = createStore(storeConfig);
  51. export default PreferenceStore;