Skip to content

Commit fb56762

Browse files
author
Guilherme de Oliveira
committed
fix: update finalValues also when relation fields doenst changes
1 parent 4b62496 commit fb56762

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/interface.vue

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script lang="ts">
77
import { ComputedRef, defineComponent, inject, ref, watch } from 'vue';
88
import { parseExpression } from './operations';
9-
import { checkFieldInTemplate, useDeepValues, useCollectionRelations } from './utils';
9+
import { useDeepValues, useCollectionRelations } from './utils';
1010
1111
export default defineComponent({
1212
props: {
@@ -43,6 +43,7 @@ export default defineComponent({
4343
inject<ComputedRef<Record<string, any>>>('values')!,
4444
relations,
4545
props.collection,
46+
props.field,
4647
props.primaryKey,
4748
props.template
4849
);
@@ -52,15 +53,13 @@ export default defineComponent({
5253
computedValue.value = compute();
5354
}
5455
55-
watch(values, (val, oldVal) => {
56-
if (shouldUpdate(val, oldVal)) {
57-
if (props.displayOnly) {
58-
computedValue.value = compute();
59-
} else {
60-
const newValue = compute();
61-
if (newValue !== props.value) {
62-
emit('input', newValue);
63-
}
56+
watch(values, () => {
57+
if (props.displayOnly) {
58+
computedValue.value = compute();
59+
} else {
60+
const newValue = compute();
61+
if (newValue !== props.value) {
62+
emit('input', newValue);
6463
}
6564
}
6665
});
@@ -70,16 +69,6 @@ export default defineComponent({
7069
computedValue,
7170
};
7271
73-
/** Simple check which fields are used */
74-
function shouldUpdate(val: Record<string, any>, oldVal: Record<string, any>) {
75-
for (const key of Object.keys(val)) {
76-
if (key !== props.field && checkFieldInTemplate(props.template, key) && val[key] !== oldVal[key]) {
77-
return true;
78-
}
79-
}
80-
return false;
81-
}
82-
8372
function compute() {
8473
return props.template.replace(/{{.*?}}/g, (match) => {
8574
const expression = match.slice(2, -2).trim();

src/utils.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ export function checkFieldInTemplate(template: string, field: string) {
77
return (matches || []).some((m) => m.includes(field));
88
}
99

10-
const checkFieldNeedsUpdate = (field: string, newVal: Record<string, any>, oldVal: Record<string, any>) => {
11-
if (oldVal[field] === newVal[field]) {
12-
return false;
13-
}
14-
if (JSON.stringify(oldVal[field]) === JSON.stringify(newVal[field])) {
15-
return false;
10+
/** Simple check which fields are used */
11+
function shouldUpdate(template: string, computedField: string, val: Record<string, any>, oldVal: Record<string, any>) {
12+
for (const key of Object.keys(val)) {
13+
if (
14+
key !== computedField &&
15+
checkFieldInTemplate(template, key) &&
16+
val[key] !== oldVal[key] &&
17+
JSON.stringify(val[key]) !== JSON.stringify(oldVal[key])
18+
) {
19+
return true;
20+
}
1621
}
17-
return true;
18-
};
22+
return false;
23+
}
1924

2025
export const useCollectionRelations = (collection: string): Ref<any[]> => {
2126
const { useRelationsStore } = useStores();
@@ -33,18 +38,24 @@ export const useDeepValues = (
3338
values: Ref<Record<string, any>>,
3439
relations: Ref<any[]>,
3540
collection: string,
41+
computedField: string,
3642
pk: string,
3743
template: string
3844
) => {
3945
const api = useApi();
40-
const finalValues = ref<Record<string, any>>(values.value);
41-
watch(values, async (val, oldVal) => {
46+
const finalValues = ref<Record<string, any>>({});
47+
watch(values, async () => {
48+
if (!shouldUpdate(template, computedField, values.value, finalValues.value)) {
49+
finalValues.value = values.value;
50+
return;
51+
}
52+
4253
const relationalData: Record<string, any> = {};
4354

4455
for (const key of Object.keys(values.value)) {
4556
const relation = relations.value.find((rel) => rel.meta?.one_field === key);
4657

47-
if (!relation || !checkFieldInTemplate(template, key) || !checkFieldNeedsUpdate(key, val, oldVal)) {
58+
if (!relation) {
4859
continue;
4960
}
5061

@@ -98,9 +109,7 @@ export const useDeepValues = (
98109
relationalData[key] = arrayOfData;
99110
}
100111

101-
if (Object.keys(relationalData).length) {
102-
finalValues.value = { ...values.value, ...relationalData };
103-
}
112+
finalValues.value = { ...values.value, ...relationalData };
104113
});
105114

106115
return finalValues;

0 commit comments

Comments
 (0)