0
0
Fork 0

feat: use mapper

This commit is contained in:
Javier Segarra 2024-11-22 15:49:01 +01:00
parent 1c2c2538ed
commit 87d3dd6fa8
4 changed files with 30 additions and 61 deletions

View File

@ -13,7 +13,6 @@ import VnConfirm from './ui/VnConfirm.vue';
import { tMobile } from 'src/composables/tMobile'; import { tMobile } from 'src/composables/tMobile';
import { useArrayData } from 'src/composables/useArrayData'; import { useArrayData } from 'src/composables/useArrayData';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
import { getDifferences } from 'src/filters';
const { push } = useRouter(); const { push } = useRouter();
const quasar = useQuasar(); 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 // Si elegimos observar los cambios del form significa que inicialmente las actions estaran deshabilitadas
const isResetting = ref(false); const isResetting = ref(false);
const hasChanges = ref(!$props.observeFormChanges); const hasChanges = ref(!$props.observeFormChanges);
const changes = ref({});
const originalData = ref({}); const originalData = ref({});
const formData = computed(() => state.get(modelValue)); const formData = computed(() => state.get(modelValue));
const defaultButtons = computed(() => ({ const defaultButtons = computed(() => ({
@ -143,40 +141,13 @@ onMounted(async () => {
hasChanges.value = hasChanges.value =
!isResetting.value && !isResetting.value &&
JSON.stringify(newVal) !== JSON.stringify(originalData.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; 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) if (!$props.url)
watch( watch(
() => arrayData.store.data, () => arrayData.store.data,
@ -224,28 +195,7 @@ async function fetch() {
originalData.value = {}; 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() { async function save() {
if ($props.observeFormChanges && !hasChanges.value) if ($props.observeFormChanges && !hasChanges.value)
return notify('globals.noChanges', 'negative'); return notify('globals.noChanges', 'negative');
@ -253,13 +203,12 @@ async function save() {
isLoading.value = true; isLoading.value = true;
try { try {
formData.value = trimData(formData.value); formData.value = trimData(formData.value);
const { method, body, url } = handleRequestArgs(); const body = $props.mapper
// // Obtener las claves del objeto original ? $props.mapper(formData.value, originalData.value)
// const originalKeys = Object.keys(body); : formData.value;
const method = $props.urlCreate ? 'post' : 'patch';
// // Construir el objeto con valores actualizados const url =
// const updatedValues = getUpdatedValues(originalKeys, formData.value); $props.urlCreate || $props.urlUpdate || $props.url || arrayData.store.url;
let response; let response;
if ($props.saveFn) response = await $props.saveFn(body); if ($props.saveFn) response = await $props.saveFn(body);
@ -332,7 +281,6 @@ defineExpose({
</script> </script>
<template> <template>
<div class="column items-center full-width"> <div class="column items-center full-width">
{{ changes }}
<QForm <QForm
ref="myForm" ref="myForm"
v-if="formData" v-if="formData"

View File

@ -0,0 +1,6 @@
export default function getUpdatedValues(keys, formData) {
return keys.reduce((acc, key) => {
acc[key] = formData[key];
return acc;
}, {});
}

View File

@ -12,9 +12,11 @@ import dateRange from './dateRange';
import toHour from './toHour'; import toHour from './toHour';
import dashOrCurrency from './dashOrCurrency'; import dashOrCurrency from './dashOrCurrency';
import getDifferences from './getDifferences'; import getDifferences from './getDifferences';
import getUpdatedValues from './getUpdatedValues';
import getParamWhere from './getParamWhere'; import getParamWhere from './getParamWhere';
export { export {
getUpdatedValues,
getDifferences, getDifferences,
toLowerCase, toLowerCase,
toLowerCamel, toLowerCamel,

View File

@ -9,6 +9,7 @@ import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue'; import VnInput from 'src/components/common/VnInput.vue';
import VnSelect from 'src/components/common/VnSelect.vue'; import VnSelect from 'src/components/common/VnSelect.vue';
import VnAvatar from 'src/components/ui/VnAvatar.vue'; import VnAvatar from 'src/components/ui/VnAvatar.vue';
import { getDifferences, getUpdatedValues } from 'src/filters';
const route = useRoute(); const route = useRoute();
const { t } = useI18n(); const { t } = useI18n();
@ -30,6 +31,13 @@ const exprBuilder = (param, value) => {
and: [{ active: { neq: false } }, handleSalesModelValue(value)], and: [{ active: { neq: false } }, handleSalesModelValue(value)],
}; };
}; };
function onBeforeSave(formData, originalData) {
return getUpdatedValues(
Object.keys(getDifferences(formData, originalData)),
formData
);
}
</script> </script>
<template> <template>
<FetchData <FetchData
@ -43,7 +51,12 @@ const exprBuilder = (param, value) => {
@on-fetch="(data) => (businessTypes = data)" @on-fetch="(data) => (businessTypes = data)"
auto-load auto-load
/> />
<FormModel :url="`Clients/${route.params.id}`" auto-load model="customer"> <FormModel
:url="`Clients/${route.params.id}`"
auto-load
model="customer"
:mapper="onBeforeSave"
>
<template #form="{ data, validate }"> <template #form="{ data, validate }">
<VnRow> <VnRow>
<VnInput <VnInput