ViewStore.ts 974 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { types } from 'mobx-state-tree';
  2. import { toJS } from 'mobx';
  3. import { toUrlParams } from 'app/core/utils/url';
  4. const QueryInnerValueType = types.union(types.string, types.boolean, types.number);
  5. const QueryValueType = types.union(QueryInnerValueType, types.array(QueryInnerValueType));
  6. export const ViewStore = types
  7. .model({
  8. path: types.string,
  9. query: types.map(QueryValueType),
  10. })
  11. .views(self => ({
  12. get currentUrl() {
  13. let path = self.path;
  14. if (self.query.size) {
  15. path += '?' + toUrlParams(toJS(self.query));
  16. }
  17. return path;
  18. },
  19. }))
  20. .actions(self => {
  21. function updateQuery(query: any) {
  22. self.query.clear();
  23. for (let key of Object.keys(query)) {
  24. self.query.set(key, query[key]);
  25. }
  26. }
  27. function updatePathAndQuery(path: string, query: any) {
  28. self.path = path;
  29. updateQuery(query);
  30. }
  31. return {
  32. updateQuery,
  33. updatePathAndQuery,
  34. };
  35. });