Skip to content

Commit

Permalink
Merge pull request #26 from rezo-labs/patch/deep_values
Browse files Browse the repository at this point in the history
Fix deep values
  • Loading branch information
duydvu committed Dec 26, 2022
2 parents 47637bb + ee67479 commit 6457b2b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/interface.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
34 changes: 26 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ export function checkFieldInTemplate(template: string, field: string) {
}

/** Simple check which fields are used */
function shouldUpdate(template: string, computedField: string, val: Record<string, any>, oldVal: Record<string, any>) {
for (const key of Object.keys(val)) {
function shouldUpdate(
template: string,
computedField: string,
val: Record<string, any>,
oldVal: Record<string, any>,
pk: string | number,
) {
// creating new item
if (val.id && pk === '+') {
return false;
}

for (const key of Object.keys({ ...oldVal, ...val })) {
if (
key !== computedField &&
checkFieldInTemplate(template, key) &&
Expand Down Expand Up @@ -59,13 +70,20 @@ 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;
}

for (const key of Object.keys(oldValObj)) {
if (!(key in valObj)) {
valObj[key] = null;
}
}

let relationalData: Record<string, any> = {};
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)) {
Expand All @@ -75,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: [],
Expand Down Expand Up @@ -109,12 +127,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],
},
Expand Down Expand Up @@ -171,7 +189,7 @@ export const useDeepValues = (
relationalData[key] = isM2O ? arrayOfData[0] : arrayOfData;
}

finalValues.value = { ...values.value, ...relationalData };
finalValues.value = { ...valObj, ...relationalData };
},
{
deep: false,
Expand Down

0 comments on commit 6457b2b

Please sign in to comment.