From 87d3dd6fa85e5954f8b5726c220e65d5d7906530 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 22 Nov 2024 15:49:01 +0100 Subject: [PATCH] feat: use mapper --- src/components/FormModel.vue | 68 +++---------------- src/filters/getUpdatedValues.js | 6 ++ src/filters/index.js | 2 + src/pages/Customer/Card/CustomerBasicData.vue | 15 +++- 4 files changed, 30 insertions(+), 61 deletions(-) create mode 100644 src/filters/getUpdatedValues.js diff --git a/src/components/FormModel.vue b/src/components/FormModel.vue index fdb799545..1eb3d5594 100644 --- a/src/components/FormModel.vue +++ b/src/components/FormModel.vue @@ -13,7 +13,6 @@ import VnConfirm from './ui/VnConfirm.vue'; import { tMobile } from 'src/composables/tMobile'; import { useArrayData } from 'src/composables/useArrayData'; import { useRoute } from 'vue-router'; -import { getDifferences } from 'src/filters'; const { push } = useRouter(); const quasar = useQuasar(); @@ -103,7 +102,6 @@ const isLoading = ref(false); // Si elegimos observar los cambios del form significa que inicialmente las actions estaran deshabilitadas const isResetting = ref(false); const hasChanges = ref(!$props.observeFormChanges); -const changes = ref({}); const originalData = ref({}); const formData = computed(() => state.get(modelValue)); const defaultButtons = computed(() => ({ @@ -143,40 +141,13 @@ onMounted(async () => { hasChanges.value = !isResetting.value && JSON.stringify(newVal) !== JSON.stringify(originalData.value); - console.error(getDifferences(originalData.value, newVal)); - console.error(getDifferences(newVal, originalData.value)); - console.error(getDiff(newVal, originalData.value)); - console.error(getDiff(originalData.value, newVal)); - console.error(getDifferencess(originalData.value, newVal)); - changes.value = { - ...changes.value, - ...getDifferences(originalData.value, oldVal), - }; isResetting.value = false; }, - { deep: true, flush: 'sync' } + { deep: true } ); } }); -function getDifferencess(oldVal, newVal) { - return Object.keys(newVal).reduce((diff, key) => { - if (oldVal[key] !== newVal[key]) { - diff[key] = newVal[key]; - } - return diff; - }, {}); -} -function getDiff(o2, o1) { - let diff = Object.keys(o2).reduce((diff, key) => { - if (o1[key] === o2[key]) return diff; - return { - ...diff, - [key]: o2[key], - }; - }, {}); - return diff; -} if (!$props.url) watch( () => arrayData.store.data, @@ -224,28 +195,7 @@ async function fetch() { originalData.value = {}; } } -function handleRequestArgs() { - const isUrlCreate = $props.urlCreate; - const differences = getUpdatedValues( - Object.keys(getDifferences(formData.value, originalData.value)), - formData.value - ); - return { - method: isUrlCreate ? 'post' : 'patch', - url: isUrlCreate || $props.urlUpdate || $props.url || arrayData.store.url, - body: $props.mapper - ? $props.mapper(formData.value) - : isUrlCreate - ? formData.value - : differences, - }; -} -function getUpdatedValues(keys, formData) { - return keys.reduce((acc, key) => { - acc[key] = formData[key]; - return acc; - }, {}); -} + async function save() { if ($props.observeFormChanges && !hasChanges.value) return notify('globals.noChanges', 'negative'); @@ -253,13 +203,12 @@ async function save() { isLoading.value = true; try { formData.value = trimData(formData.value); - const { method, body, url } = handleRequestArgs(); - // // Obtener las claves del objeto original - // const originalKeys = Object.keys(body); - - // // Construir el objeto con valores actualizados - // const updatedValues = getUpdatedValues(originalKeys, formData.value); - + const body = $props.mapper + ? $props.mapper(formData.value, originalData.value) + : formData.value; + const method = $props.urlCreate ? 'post' : 'patch'; + const url = + $props.urlCreate || $props.urlUpdate || $props.url || arrayData.store.url; let response; if ($props.saveFn) response = await $props.saveFn(body); @@ -332,7 +281,6 @@ defineExpose({