diff --git a/src/components/FormModel.vue b/src/components/FormModel.vue index 2e580257c..3842ff947 100644 --- a/src/components/FormModel.vue +++ b/src/components/FormModel.vue @@ -97,7 +97,7 @@ const $props = defineProps({ }); const emit = defineEmits(['onFetch', 'onDataSaved']); const modelValue = computed( - () => $props.model ?? `formModel_${route?.meta?.title ?? route.name}` + () => $props.model ?? `formModel_${route?.meta?.title ?? route.name}`, ).value; const componentIsRendered = ref(false); const arrayData = useArrayData(modelValue); @@ -136,7 +136,8 @@ onMounted(async () => { if (!$props.formInitialData) { if ($props.autoLoad && $props.url) await fetch(); - else if (arrayData.store.data) updateAndEmit('onFetch', arrayData.store.data); + else if (arrayData.store.data) + updateAndEmit('onFetch', { val: arrayData.store.data }); } if ($props.observeFormChanges) { watch( @@ -148,7 +149,7 @@ onMounted(async () => { JSON.stringify(newVal) !== JSON.stringify(originalData.value); isResetting.value = false; }, - { deep: true } + { deep: true }, ); } }); @@ -156,7 +157,7 @@ onMounted(async () => { if (!$props.url) watch( () => arrayData.store.data, - (val) => updateAndEmit('onFetch', val) + (val) => updateAndEmit('onFetch', { val }), ); watch( @@ -165,7 +166,7 @@ watch( originalData.value = null; reset(); await fetch(); - } + }, ); onBeforeRouteLeave((to, from, next) => { @@ -194,7 +195,7 @@ async function fetch() { }); if (Array.isArray(data)) data = data[0] ?? {}; - updateAndEmit('onFetch', data); + updateAndEmit('onFetch', { val: data }); } catch (e) { state.set(modelValue, {}); originalData.value = {}; @@ -222,7 +223,11 @@ async function save() { if ($props.urlCreate) notify('globals.dataCreated', 'positive'); - updateAndEmit('onDataSaved', formData.value, response?.data); + updateAndEmit('onDataSaved', { + val: formData.value, + res: response?.data, + old: originalData.value, + }); if ($props.reload) await arrayData.fetch({}); hasChanges.value = false; } finally { @@ -236,7 +241,7 @@ async function saveAndGo() { } function reset() { - updateAndEmit('onFetch', originalData.value); + updateAndEmit('onFetch', { val: originalData.value }); if ($props.observeFormChanges) { hasChanges.value = false; isResetting.value = true; @@ -254,16 +259,16 @@ function filter(value, update, filterOptions) { (ref) => { ref.setOptionIndex(-1); ref.moveOptionSelection(1, true); - } + }, ); } -function updateAndEmit(evt, val, res) { +function updateAndEmit(evt, { val, res, old } = { val: null, res: null, old: null }) { state.set(modelValue, val); originalData.value = val && JSON.parse(JSON.stringify(val)); if (!$props.url) arrayData.store.data = val; - emit(evt, state.get(modelValue), res); + emit(evt, state.get(modelValue), res, old); } function trimData(data) { diff --git a/src/components/common/VnCardBeta.vue b/src/components/common/VnCardBeta.vue index a1f07ff17..f237a300c 100644 --- a/src/components/common/VnCardBeta.vue +++ b/src/components/common/VnCardBeta.vue @@ -49,12 +49,22 @@ onBeforeMount(async () => { if (props.baseUrl) { onBeforeRouteUpdate(async (to, from) => { + if (hasRouteParam(to.params)) { + const { matched } = router.currentRoute.value; + const { name } = matched.at(-3); + if (name) { + router.push({ name, params: to.params }); + } + } if (to.params.id !== from.params.id) { arrayData.store.url = `${props.baseUrl}/${to.params.id}`; await arrayData.fetch({ append: false, updateRouter: false }); } }); } +function hasRouteParam(params, valueToCheck = ':addressId') { + return Object.values(params).includes(valueToCheck); +}