0
0
Fork 0

fix(url): redirect

This commit is contained in:
Alex Moreno 2024-06-10 13:14:24 +02:00
parent 128904970b
commit ed07b4d620
2 changed files with 16 additions and 16 deletions

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { onBeforeMount, computed, watchEffect } from 'vue'; import { computed } from 'vue';
import { useRoute } from 'vue-router'; import { useRoute } 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';
@ -29,20 +29,10 @@ const url = computed(() => {
return props.customUrl; return props.customUrl;
}); });
const arrayData = useArrayData(props.dataKey, { useArrayData(props.dataKey, {
url: url.value, url: url.value,
filter: props.filter, filter: props.filter,
}); });
onBeforeMount(async () => {
if (!props.baseUrl) arrayData.store.filter.where = { id: route.params.id };
await arrayData.fetch({ append: false });
});
watchEffect(() => {
if (Array.isArray(arrayData.store.data))
arrayData.store.data = arrayData.store.data[0];
});
</script> </script>
<template> <template>
<template v-if="stateStore.isHeaderMounted()"> <template v-if="stateStore.isHeaderMounted()">

View File

@ -1,4 +1,4 @@
import { onMounted, ref, computed } from 'vue'; import { onMounted, ref, computed, onUnmounted } from 'vue';
import { useRouter, useRoute } from 'vue-router'; import { useRouter, useRoute } from 'vue-router';
import axios from 'axios'; import axios from 'axios';
import { useArrayDataStore } from 'stores/useArrayDataStore'; import { useArrayDataStore } from 'stores/useArrayDataStore';
@ -18,6 +18,10 @@ export function useArrayData(key, userOptions) {
const page = ref(1); const page = ref(1);
onUnmounted(() => {
destroy();
});
onMounted(() => { onMounted(() => {
setOptions(); setOptions();
store.skip = 0; store.skip = 0;
@ -30,6 +34,7 @@ export function useArrayData(key, userOptions) {
delete params.filter; delete params.filter;
store.userParams = { ...params, ...store.userParams }; store.userParams = { ...params, ...store.userParams };
store.userFilter = { ...JSON.parse(filter), ...store.userFilter }; store.userFilter = { ...JSON.parse(filter), ...store.userFilter };
console.log('store.userParams', store.userParams, store.userFilter);
} }
}); });
@ -121,6 +126,7 @@ export function useArrayData(key, userOptions) {
} }
function destroy() { function destroy() {
console.log('DESTROY', key);
if (arrayDataStore.get(key)) { if (arrayDataStore.get(key)) {
arrayDataStore.clear(key); arrayDataStore.clear(key);
} }
@ -217,10 +223,14 @@ export function useArrayData(key, userOptions) {
? path.replace(/\/(list|:id)|-list/, `/${store.data[0].id}`) ? path.replace(/\/(list|:id)|-list/, `/${store.data[0].id}`)
: path.replace(/:id.*/, ''); : path.replace(/:id.*/, '');
console.log('to: ', to, store.userParams, store.userFilter, route.query);
if (route.path != to) { if (route.path != to) {
store.userParams = {}; destroy();
store.userFilter = {}; const pushUrl = { path: to };
return router.push({ path: to }); if (to.endsWith('/list') || to.endsWith('/'))
pushUrl.query = newUrl.query;
console.log('pushUrl: ', to, pushUrl.query);
return router.push(pushUrl);
} }
} }