feat: refs #6919 add customUrl prop to VnCardBeta for flexible URL handling
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2025-01-09 11:01:48 +01:00
parent 2462b5f38e
commit d23eb052f4
1 changed files with 14 additions and 6 deletions

View File

@ -1,5 +1,5 @@
<script setup>
import { onBeforeMount } from 'vue';
import { onBeforeMount, computed } from 'vue';
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router';
import { useArrayData } from 'src/composables/useArrayData';
import { useStateStore } from 'stores/useStateStore';
@ -10,6 +10,7 @@ import VnSubToolbar from '../ui/VnSubToolbar.vue';
const props = defineProps({
dataKey: { type: String, required: true },
url: { type: String, default: undefined },
customUrl: { type: String, default: undefined },
idInWhere: { type: Boolean, default: false },
filter: { type: Object, default: () => {} },
descriptor: { type: Object, required: true },
@ -24,6 +25,7 @@ const route = useRoute();
const router = useRouter();
const regex = /(\/\d+)/;
const url = computed(() => props.url || props.customUrl);
const arrayData = useArrayData(props.dataKey, {
url: props.url,
filter: props.filter,
@ -32,9 +34,11 @@ const arrayData = useArrayData(props.dataKey, {
onBeforeMount(async () => {
try {
if (props.idInWhere) arrayData.store.filter.where = { id: route.params.id };
else if (!regex.test(props.url))
arrayData.store.url = `${props.url}/${route.params.id}`;
const id = 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 {
const { matched: matches } = router.currentRoute.value;
@ -45,8 +49,12 @@ onBeforeMount(async () => {
onBeforeRouteUpdate(async (to, from) => {
if (to.params.id !== from.params.id) {
if (props.idInWhere) arrayData.store.filter.where = { id: to.params.id };
else arrayData.store.url = `${props.url}/${to.params.id}`;
const id = to.params.id;
if (props.idInWhere) arrayData.store.filter.where = { id };
else if (props.customUrl)
arrayData.store.url = url.value.replace(regex, `/${id}`);
else arrayData.store.url = `${url.value}/${id}`;
await arrayData.fetch({ updateRouter: false });
}
});