From b2d0a7c4a1339f7c491a1f73a048876bc82877a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20=C4=90=E1=BB=A9c=20Duy?= Date: Mon, 26 Dec 2022 16:09:58 +0700 Subject: [PATCH 1/2] Fix bugs when creating new item --- src/utils.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 36191d87..c9f12c3f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,18 @@ export function checkFieldInTemplate(template: string, field: string) { } /** Simple check which fields are used */ -function shouldUpdate(template: string, computedField: string, val: Record, oldVal: Record) { +function shouldUpdate( + template: string, + computedField: string, + val: Record, + oldVal: Record, + pk: string | number, +) { + // creating new item + if (val.id && pk === '+') { + return false; + } + for (const key of Object.keys(val)) { if ( key !== computedField && @@ -59,11 +70,12 @@ export const useDeepValues = ( async (val, oldVal) => { const valObj = JSON.parse(val); const oldValObj = oldVal !== undefined ? JSON.parse(oldVal) : {}; - if (!shouldUpdate(template, computedField, valObj, oldValObj)) { + if (!shouldUpdate(template, computedField, valObj, oldValObj, pk)) { return; } let relationalData: Record = {}; + const pkFinal = values.value.id || pk; for (const key of Object.keys(values.value)) { const relation = relations.value.find((rel) => [rel.meta?.one_field, rel.meta?.many_field].includes(key)); @@ -109,12 +121,12 @@ export const useDeepValues = ( itemCache = {}; } - if (pk !== '+') { + if (pkFinal !== '+') { let data; if (key in fieldCache) { data = fieldCache[key]; } else { - data = (await api.get(`items/${collection}/${pk}`, { + data = (await api.get(`items/${collection}/${pkFinal}`, { params: { fields: [key], }, From ee67479a09bfb19fe72eaebb932bdcda4310f1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20=C4=90=E1=BB=A9c=20Duy?= Date: Mon, 26 Dec 2022 17:58:20 +0700 Subject: [PATCH 2/2] Fix bugs when clearing value --- src/interface.vue | 3 +++ src/utils.ts | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/interface.vue b/src/interface.vue index 91b4d194..76ca2488 100644 --- a/src/interface.vue +++ b/src/interface.vue @@ -103,6 +103,9 @@ export default defineComponent({ const expression = match.slice(2, -2).trim(); return parseExpression(expression, values.value); }); + + errorMsg.value = null; + if (['integer', 'decimal', 'bigInteger'].includes(props.type)) { return parseInt(res) || 0; } diff --git a/src/utils.ts b/src/utils.ts index c9f12c3f..40a9f39f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,7 +21,7 @@ function shouldUpdate( return false; } - for (const key of Object.keys(val)) { + for (const key of Object.keys({ ...oldVal, ...val })) { if ( key !== computedField && checkFieldInTemplate(template, key) && @@ -74,10 +74,16 @@ export const useDeepValues = ( return; } + for (const key of Object.keys(oldValObj)) { + if (!(key in valObj)) { + valObj[key] = null; + } + } + let relationalData: Record = {}; - const pkFinal = values.value.id || pk; + const pkFinal = valObj.id || pk; - for (const key of Object.keys(values.value)) { + for (const key of Object.keys(valObj)) { const relation = relations.value.find((rel) => [rel.meta?.one_field, rel.meta?.many_field].includes(key)); if (!relation || !checkFieldInTemplate(template, key)) { @@ -87,7 +93,7 @@ export const useDeepValues = ( const isM2O = relation.collection === collection; const fieldName = isM2O ? relation.meta?.many_field : relation.meta?.one_field; - let fieldChanges = values.value[fieldName!] as IRelationUpdate ?? { + let fieldChanges = valObj[fieldName!] as IRelationUpdate ?? { create: [], update: [], delete: [], @@ -183,7 +189,7 @@ export const useDeepValues = ( relationalData[key] = isM2O ? arrayOfData[0] : arrayOfData; } - finalValues.value = { ...values.value, ...relationalData }; + finalValues.value = { ...valObj, ...relationalData }; }, { deep: false,