vuex_helpers.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { defineAsyncComponent } from "vue";
  2. import { useStore } from "vuex";
  3. const mapModalState = (namespace, map) => {
  4. const modalState = {};
  5. // console.log("MAP MODAL STATE", namespace);
  6. Object.entries(map).forEach(([mapKey, mapValue]: [string, (value: object) => void]) => {
  7. modalState[mapKey] = function func() {
  8. // console.log(
  9. // 321,
  10. // namespace
  11. // .replace(
  12. // "MODAL_MODULE_PATH",
  13. // namespace.indexOf("MODAL_MODULE_PATH") !== -1
  14. // ? this.modalModulePath
  15. // : null
  16. // )
  17. // .replace("MODAL_UUID", this.modalUuid)
  18. // .split("/")
  19. // );
  20. // console.log(3211, mapKey);
  21. const state = namespace
  22. .replace(
  23. "MODAL_MODULE_PATH",
  24. namespace.indexOf("MODAL_MODULE_PATH") !== -1
  25. ? this.modalModulePath
  26. : null
  27. )
  28. .replace("MODAL_UUID", this.modalUuid)
  29. .split("/")
  30. .reduce((a, b) => a[b], this.$store.state);
  31. // console.log(32111, state);
  32. // if (state) console.log(321111, mapValue(state));
  33. // else console.log(321111, "NADA");
  34. if (state) return mapValue(state);
  35. return mapValue({});
  36. };
  37. });
  38. return modalState;
  39. };
  40. const mapModalActions = (namespace, map) => {
  41. const modalState = {};
  42. map.forEach(mapValue => {
  43. modalState[mapValue] = function func(value) {
  44. return this.$store.dispatch(
  45. `${namespace
  46. .replace(
  47. "MODAL_MODULE_PATH",
  48. namespace.indexOf("MODAL_MODULE_PATH") !== -1
  49. ? this.modalModulePath
  50. : null
  51. )
  52. .replace("MODAL_UUID", this.modalUuid)}/${mapValue}`,
  53. value
  54. );
  55. };
  56. });
  57. return modalState;
  58. };
  59. const mapModalComponents = (baseDirectory, map) => {
  60. const modalComponents = {};
  61. Object.entries(map).forEach(([mapKey, mapValue]) => {
  62. modalComponents[mapKey] = () =>
  63. defineAsyncComponent(() =>
  64. import(`./${baseDirectory}/${mapValue}`)
  65. );
  66. });
  67. return modalComponents;
  68. };
  69. const useModalState = (namespace, options) => {
  70. const store = useStore();
  71. const modalState = namespace
  72. .replace(
  73. "MODAL_MODULE_PATH",
  74. namespace.indexOf("MODAL_MODULE_PATH") !== -1
  75. ? options.modalModulePath
  76. : null
  77. )
  78. .replace("MODAL_UUID", options.modalUuid)
  79. .split("/")
  80. .reduce((a, b) => a[b], store.state);
  81. return modalState ? modalState : {};
  82. }
  83. const useModalActions = (namespace, actions, options) => {
  84. const store = useStore();
  85. const pathStart = `${namespace
  86. .replace(
  87. "MODAL_MODULE_PATH",
  88. namespace.indexOf("MODAL_MODULE_PATH") !== -1
  89. ? options.modalModulePath
  90. : null
  91. )
  92. .replace("MODAL_UUID", options.modalUuid)}`;
  93. const actionDispatchers = actions.map(actionName => ([actionName, function func(value) {
  94. return store.dispatch(
  95. `${pathStart}/${actionName}`,
  96. value
  97. );
  98. }]));
  99. return Object.fromEntries(actionDispatchers);
  100. }
  101. export { mapModalState, mapModalActions, mapModalComponents, useModalState, useModalActions };