refactor: refs #6919 fine tunning vnCard
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2025-01-09 17:10:21 +01:00
parent b7f2a320c7
commit 9e06a48841
4 changed files with 25 additions and 45 deletions

View File

@ -11,7 +11,6 @@ import RightMenu from 'components/common/RightMenu.vue';
const props = defineProps({ const props = defineProps({
dataKey: { type: String, required: true }, dataKey: { type: String, required: true },
url: { type: String, default: undefined }, url: { type: String, default: undefined },
customUrl: { type: String, default: undefined },
filter: { type: Object, default: () => {} }, filter: { type: Object, default: () => {} },
descriptor: { type: Object, required: true }, descriptor: { type: Object, required: true },
filterPanel: { type: Object, default: undefined }, filterPanel: { type: Object, default: undefined },
@ -24,27 +23,20 @@ const props = defineProps({
const stateStore = useStateStore(); const stateStore = useStateStore();
const route = useRoute(); const route = useRoute();
const router = useRouter(); const router = useRouter();
const regex = /(\/\d+)/;
const searchRightDataKey = computed(() => { const searchRightDataKey = computed(() => {
if (!props.searchDataKey) return route.name; if (!props.searchDataKey) return route.name;
return props.searchDataKey; return props.searchDataKey;
}); });
const url = computed(() => props.url || props.customUrl);
const arrayData = useArrayData(props.dataKey, { const arrayData = useArrayData(props.dataKey, {
url: url.value, url: props.url,
filter: props.filter, filter: props.filter,
oneRecord: true, oneRecord: true,
}); });
onBeforeMount(async () => { onBeforeMount(async () => {
try { try {
const id = route.params.id; await fetchData(route.params.id);
if (props.idInWhere) arrayData.store.filter.where = { id };
else if (props.customUrl) arrayData.store.url = url.value;
else if (!regex.test(url.value)) arrayData.store.url = `${url.value}/${id}`;
await arrayData.fetch({ append: false, updateRouter: false });
} catch { } catch {
const { matched: matches } = router.currentRoute.value; const { matched: matches } = router.currentRoute.value;
const { path } = matches.at(-1); const { path } = matches.at(-1);
@ -53,16 +45,15 @@ onBeforeMount(async () => {
}); });
onBeforeRouteUpdate(async (to, from) => { onBeforeRouteUpdate(async (to, from) => {
if (to.params.id !== from.params.id) {
const id = to.params.id; const id = to.params.id;
if (props.idInWhere) arrayData.store.filter.where = { id }; if (id !== from.params.id) await fetchData(id, true);
else if (props.customUrl)
arrayData.store.url = url.value.replace(regex, `/${id}`);
else arrayData.store.url = `${url.value}/${id}`;
await arrayData.fetch({ updateRouter: false });
}
}); });
async function fetchData(id, append = false) {
if (props.idInWhere) arrayData.store.filter.where = { id };
else arrayData.store.url = props.url.replace(/(\/\d+)/, `/${id}`);
await arrayData.fetch({ append, updateRouter: false });
}
</script> </script>
<template> <template>
<QDrawer <QDrawer
@ -89,7 +80,7 @@ onBeforeRouteUpdate(async (to, from) => {
<QPage> <QPage>
<VnSubToolbar /> <VnSubToolbar />
<div :class="[useCardSize(), $attrs.class]"> <div :class="[useCardSize(), $attrs.class]">
<RouterView :key="route.path" /> <RouterView :key="$route.path" />
</div> </div>
</QPage> </QPage>
</QPageContainer> </QPageContainer>

View File

@ -1,6 +1,6 @@
<script setup> <script setup>
import { onBeforeMount, computed } from 'vue'; import { onBeforeMount } from 'vue';
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router'; import { useRouter, onBeforeRouteUpdate } from 'vue-router';
import { useArrayData } from 'src/composables/useArrayData'; import { useArrayData } from 'src/composables/useArrayData';
import { useStateStore } from 'stores/useStateStore'; import { useStateStore } from 'stores/useStateStore';
import useCardSize from 'src/composables/useCardSize'; import useCardSize from 'src/composables/useCardSize';
@ -10,7 +10,6 @@ import VnSubToolbar from '../ui/VnSubToolbar.vue';
const props = defineProps({ const props = defineProps({
dataKey: { type: String, required: true }, dataKey: { type: String, required: true },
url: { type: String, default: undefined }, url: { type: String, default: undefined },
customUrl: { type: String, default: undefined },
idInWhere: { type: Boolean, default: false }, idInWhere: { type: Boolean, default: false },
filter: { type: Object, default: () => {} }, filter: { type: Object, default: () => {} },
descriptor: { type: Object, required: true }, descriptor: { type: Object, required: true },
@ -21,11 +20,7 @@ const props = defineProps({
}); });
const stateStore = useStateStore(); const stateStore = useStateStore();
const route = useRoute();
const router = useRouter(); const router = useRouter();
const regex = /(\/\d+)/;
const url = computed(() => props.url || props.customUrl);
const arrayData = useArrayData(props.dataKey, { const arrayData = useArrayData(props.dataKey, {
url: props.url, url: props.url,
filter: props.filter, filter: props.filter,
@ -34,12 +29,7 @@ const arrayData = useArrayData(props.dataKey, {
onBeforeMount(async () => { onBeforeMount(async () => {
try { try {
const id = route.params.id; await fetchData(router.currentRoute.value.params.id);
if (props.idInWhere) arrayData.store.filter.where = { id };
else if (props.customUrl) arrayData.store.url = url.value;
else if (!regex.test(url.value)) arrayData.store.url = `${url.value}/${id}`;
await arrayData.fetch({ append: false, updateRouter: false });
} catch { } catch {
const { matched: matches } = router.currentRoute.value; const { matched: matches } = router.currentRoute.value;
const { path } = matches.at(-1); const { path } = matches.at(-1);
@ -48,16 +38,15 @@ onBeforeMount(async () => {
}); });
onBeforeRouteUpdate(async (to, from) => { onBeforeRouteUpdate(async (to, from) => {
if (to.params.id !== from.params.id) {
const id = to.params.id; const id = to.params.id;
if (props.idInWhere) arrayData.store.filter.where = { id }; if (id !== from.params.id) await fetchData(id, true);
else if (props.customUrl)
arrayData.store.url = url.value.replace(regex, `/${id}`);
else arrayData.store.url = `${url.value}/${id}`;
await arrayData.fetch({ updateRouter: false });
}
}); });
async function fetchData(id, append = false) {
if (props.idInWhere) arrayData.store.filter.where = { id };
else arrayData.store.url = props.url.replace(/(\/\d+)/, `/${id}`);
await arrayData.fetch({ append, updateRouter: false });
}
</script> </script>
<template> <template>
<Teleport to="#left-panel" v-if="stateStore.isHeaderMounted()"> <Teleport to="#left-panel" v-if="stateStore.isHeaderMounted()">
@ -67,6 +56,6 @@ onBeforeRouteUpdate(async (to, from) => {
</Teleport> </Teleport>
<VnSubToolbar /> <VnSubToolbar />
<div :class="[useCardSize(), $attrs.class]"> <div :class="[useCardSize(), $attrs.class]">
<RouterView :key="route.path" /> <RouterView :key="$route.path" />
</div> </div>
</template> </template>

View File

@ -6,7 +6,7 @@ import CustomerFilter from '../CustomerFilter.vue';
<template> <template>
<VnCard <VnCard
data-key="Customer" data-key="Customer"
:custom-url="`Clients/${$route.params.id}/getCard`" :url="`Clients/${$route.params.id}/getCard`"
:descriptor="CustomerDescriptor" :descriptor="CustomerDescriptor"
:filter-panel="$route.name != 'CustomerConsumption' && CustomerFilter" :filter-panel="$route.name != 'CustomerConsumption' && CustomerFilter"
search-data-key="CustomerList" search-data-key="CustomerList"

View File

@ -6,7 +6,7 @@ import ItemListFilter from '../ItemListFilter.vue';
<template> <template>
<VnCard <VnCard
data-key="Item" data-key="Item"
:custom-url="`Items/${$route.params.id}/getCard`" :url="`Items/${$route.params.id}/getCard`"
:descriptor="ItemDescriptor" :descriptor="ItemDescriptor"
:filter-panel="ItemListFilter" :filter-panel="ItemListFilter"
search-data-key="ItemList" search-data-key="ItemList"