preferencesStore.tsx 1.2 KB

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