preferencesStore.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. this.reset();
  21. },
  22. getInitialState() {
  23. return this.prefs;
  24. },
  25. reset() {
  26. this.prefs = {collapsed: false};
  27. },
  28. loadInitialState(prefs) {
  29. this.prefs = {...prefs};
  30. this.trigger(this.prefs);
  31. },
  32. hideSidebar() {
  33. this.prefs = {...this.prefs, collapsed: true};
  34. this.trigger(this.prefs);
  35. },
  36. showSidebar() {
  37. this.prefs = {...this.prefs, collapsed: false};
  38. this.trigger(this.prefs);
  39. },
  40. getState() {
  41. return this.prefs;
  42. },
  43. };
  44. /**
  45. * This store is used to hold local user preferences
  46. * Side-effects (like reading/writing to cookies) are done in associated actionCreators
  47. */
  48. const PreferenceStore = createStore(storeConfig);
  49. export default PreferenceStore;