0
0
Fork 0

perf: aprroach to simplify

This commit is contained in:
Javier Segarra 2024-09-03 23:44:13 +02:00
parent 00beeaf51b
commit 414a72e6a8
5 changed files with 116 additions and 33 deletions

View File

@ -1,6 +1,6 @@
<script setup> <script setup>
import axios from 'axios'; import axios from 'axios';
import { onMounted, watch, ref, reactive } from 'vue'; import { onMounted, watch, ref, reactive, computed } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { QSeparator, useQuasar } from 'quasar'; import { QSeparator, useQuasar } from 'quasar';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
@ -9,6 +9,8 @@ import { toLowerCamel } from 'src/filters';
import routes from 'src/router/modules'; import routes from 'src/router/modules';
import LeftMenuItem from './LeftMenuItem.vue'; import LeftMenuItem from './LeftMenuItem.vue';
import LeftMenuItemGroup from './LeftMenuItemGroup.vue'; import LeftMenuItemGroup from './LeftMenuItemGroup.vue';
import { useState } from 'src/composables/useState';
const state = useState();
const { t } = useI18n(); const { t } = useI18n();
const route = useRoute(); const route = useRoute();
@ -27,15 +29,22 @@ const expansionItemElements = reactive({});
onMounted(async () => { onMounted(async () => {
await navigation.fetchPinned(); await navigation.fetchPinned();
getRoutes(); getRoutes();
handleHiddenSections();
}); });
function handleHiddenSections() {
state.getHiddenSections().value.forEach((hidde) => {
const item = items.value.find((item) => item.name === hidde);
if (item) item.hidden = true;
});
}
watch( watch(
() => route.matched, () => route.params.id,
() => { () => {
items.value = []; items.value = [];
getRoutes(); getRoutes();
}, handleHiddenSections();
{ deep: true } }
); );
function findMatches(search, item) { function findMatches(search, item) {
@ -220,7 +229,13 @@ const handleItemExpansion = (itemName) => {
</template> </template>
<template v-if="$props.source === 'card'"> <template v-if="$props.source === 'card'">
<template v-for="item in items" :key="item.name"> <template v-for="item in items" :key="item.name">
<LeftMenuItem v-if="!item.children" :item="item" /> <LeftMenuItem
:class="{
hidden: item.hidden || false,
}"
v-if="!item.children"
:item="item"
/>
<QList v-else> <QList v-else>
<QExpansionItem <QExpansionItem
v-ripple v-ripple

View File

@ -19,6 +19,7 @@ const acls = ref([]);
const tokenConfig = ref({}); const tokenConfig = ref({});
const drawer = ref(true); const drawer = ref(true);
const headerMounted = ref(false); const headerMounted = ref(false);
const hiddenSections = ref([]);
export function useState() { export function useState() {
function getUser() { function getUser() {
@ -51,6 +52,17 @@ export function useState() {
function setAcls(data) { function setAcls(data) {
acls.value = data; acls.value = data;
} }
function getHiddenSections() {
return computed(() => hiddenSections.value);
}
function setHiddenSections(data) {
hiddenSections.value.push(data);
}
function popHiddenSections(data) {
const index = hiddenSections.value.findIndex((item) => item === data);
if (index > -1) hiddenSections.value = hiddenSections.value.splice(0, index);
}
function getTokenConfig() { function getTokenConfig() {
return computed(() => { return computed(() => {
return tokenConfig.value; return tokenConfig.value;
@ -78,6 +90,9 @@ export function useState() {
setUser, setUser,
getRoles, getRoles,
setRoles, setRoles,
getHiddenSections,
popHiddenSections,
setHiddenSections,
getAcls, getAcls,
setAcls, setAcls,
getTokenConfig, getTokenConfig,

View File

@ -4,7 +4,7 @@ import InvoiceInDescriptor from './InvoiceInDescriptor.vue';
import InvoiceInFilter from '../InvoiceInFilter.vue'; import InvoiceInFilter from '../InvoiceInFilter.vue';
import InvoiceInSearchbar from '../InvoiceInSearchbar.vue'; import InvoiceInSearchbar from '../InvoiceInSearchbar.vue';
import axios from 'axios'; import axios from 'axios';
import { onBeforeRouteUpdate } from 'vue-router'; // import { onBeforeRouteUpdate } from 'vue-router';
const filter = { const filter = {
include: [ include: [
@ -23,19 +23,19 @@ const filter = {
], ],
}; };
onBeforeRouteUpdate(async (to) => { // onBeforeRouteUpdate(async (to) => {
const card = to.matched.find((route) => route.name === 'InvoiceInCard'); // const card = to.matched.find((route) => route.name === 'InvoiceInCard');
const corrective = card.children.find( // const corrective = card.children.find(
(route) => route.name === 'InvoiceInCorrective' // (route) => route.name === 'InvoiceInCorrective'
); // );
const isRectificative = !!( // const isRectificative = !!(
await axios.get('InvoiceInCorrections', { // await axios.get('InvoiceInCorrections', {
params: { filter: { where: { correctingFk: to.params.id } } }, // params: { filter: { where: { correctingFk: to.params.id } } },
}) // })
).data.length; // ).data.length;
corrective.meta.hidden = !isRectificative; // corrective.meta.hidden = !isRectificative;
}); // });
</script> </script>
<template> <template>
<VnCard <VnCard

View File

@ -13,7 +13,20 @@ import { useRole } from 'src/composables/useRole';
import { useUserConfig } from 'src/composables/useUserConfig'; import { useUserConfig } from 'src/composables/useUserConfig';
import { useTokenConfig } from 'src/composables/useTokenConfig'; import { useTokenConfig } from 'src/composables/useTokenConfig';
import { useAcl } from 'src/composables/useAcl'; import { useAcl } from 'src/composables/useAcl';
import axios from 'axios';
//FIXME: Está es una implementación dinámica
const HIDDEN_SECTIONS = {
InvoiceInCard: {
InvoiceInCorrective: async (to) => {
const isRectificative = !!(
await axios.get('InvoiceInCorrections', {
params: { filter: { where: { correctingFk: to.params.id } } },
})
).data.length;
return !isRectificative;
},
},
};
const state = useState(); const state = useState();
const session = useSession(); const session = useSession();
const { t, te } = i18n.global; const { t, te } = i18n.global;
@ -66,6 +79,35 @@ export default route(function (/* { store, ssrContext } */) {
return true; return true;
}); });
for await (const [mainKey, mainValue] of Object.entries(HIDDEN_SECTIONS)) {
console.log(mainKey, mainValue);
if (to.matched.find((route) => route.name === mainKey)) {
for await (const [childKey, childValue] of Object.entries(
HIDDEN_SECTIONS[mainKey]
)) {
const res = await childValue(to);
if (res) {
state.setHiddenSections(childKey);
} else {
state.popHiddenSections(childKey);
}
}
}
}
//FIXME: es una implementacion ad-hoc. Si vale, considerar la opción dinámica
// if (to.matched.find((route) => route.name === 'InvoiceInCard')) {
// const isRectificative = !!(
// await axios.get('InvoiceInCorrections', {
// params: { filter: { where: { correctingFk: to.params.id } } },
// })
// ).data.length;
// if (!isRectificative) {
// state.setHiddenSections('InvoiceInCorrective');
// } else state.popHiddenSections('InvoiceInCorrective');
// // console.log(to.matched[1].meta.hidden);
// // to.matched[1].meta.hidden = !isRectificative;
// // console.log(to.matched[1].meta.hidden);
// }
if (!hasRequiredRoles) { if (!hasRequiredRoles) {
return next({ path: '/' }); return next({ path: '/' });
} }
@ -74,12 +116,23 @@ export default route(function (/* { store, ssrContext } */) {
next(); next();
}); });
Router.afterEach((to) => { Router.afterEach(async (to) => {
let title = t(`login.title`); let title = t(`login.title`);
const matches = to.matched; const matches = to.matched;
if (matches && matches.length > 1) { if (matches && matches.length > 1) {
const module = matches[1]; const module = matches[1];
// if (to.matched.find((route) => route.name === 'InvoiceInCard')) {
// const isRectificative = !!(
// await axios.get('InvoiceInCorrections', {
// params: { filter: { where: { correctingFk: to.params.id } } },
// })
// ).data.length;
// module.meta.hidden = !isRectificative;
// // stateStore.leftDrawer = isRectificative;
// }
// console.error(module.meta);
const moduleTitle = module.meta && module.meta.title; const moduleTitle = module.meta && module.meta.title;
if (moduleTitle) { if (moduleTitle) {
title = t(`globals.pageTitles.${moduleTitle}`); title = t(`globals.pageTitles.${moduleTitle}`);

View File

@ -64,22 +64,22 @@ export default {
path: ':id', path: ':id',
component: () => import('src/pages/InvoiceIn/Card/InvoiceInCard.vue'), component: () => import('src/pages/InvoiceIn/Card/InvoiceInCard.vue'),
redirect: { name: 'InvoiceInSummary' }, redirect: { name: 'InvoiceInSummary' },
beforeEnter: async (to, from, next) => { // beforeEnter: async (to, from, next) => {
const isRectificative = !!( // const isRectificative = !!(
await axios.get('InvoiceInCorrections', { // await axios.get('InvoiceInCorrections', {
params: { filter: { where: { correctingFk: to.params.id } } }, // params: { filter: { where: { correctingFk: to.params.id } } },
}) // })
).data.length; // ).data.length;
const card = to.matched.find((route) => route.name === 'InvoiceInCard'); // const card = to.matched.find((route) => route.name === 'InvoiceInCard');
const corrective = card.children.find( // const corrective = card.children.find(
(route) => route.name === 'InvoiceInCorrective' // (route) => route.name === 'InvoiceInCorrective'
); // );
corrective.meta.hidden = !isRectificative; // corrective.meta.hidden = !isRectificative;
next(); // next();
}, // },
children: [ children: [
{ {
name: 'InvoiceInSummary', name: 'InvoiceInSummary',