vuex_helpers.ts 3.1 KB

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