Skip to content

Commit 4b62496

Browse files
author
Guilherme de Oliveira
committed
fix: prevent relations requests if field doesn't changes
1 parent 5aa1e3c commit 4b62496

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ 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;
16+
}
17+
return true;
18+
};
19+
1020
export const useCollectionRelations = (collection: string): Ref<any[]> => {
1121
const { useRelationsStore } = useStores();
1222
const { getRelationsForCollection } = useRelationsStore();
@@ -28,13 +38,13 @@ export const useDeepValues = (
2838
) => {
2939
const api = useApi();
3040
const finalValues = ref<Record<string, any>>(values.value);
31-
watch(values, async () => {
41+
watch(values, async (val, oldVal) => {
3242
const relationalData: Record<string, any> = {};
3343

3444
for (const key of Object.keys(values.value)) {
3545
const relation = relations.value.find((rel) => rel.meta?.one_field === key);
3646

37-
if (!relation || !checkFieldInTemplate(template, key)) {
47+
if (!relation || !checkFieldInTemplate(template, key) || !checkFieldNeedsUpdate(key, val, oldVal)) {
3848
continue;
3949
}
4050

@@ -88,7 +98,9 @@ export const useDeepValues = (
8898
relationalData[key] = arrayOfData;
8999
}
90100

91-
finalValues.value = { ...values.value, ...relationalData };
101+
if (Object.keys(relationalData).length) {
102+
finalValues.value = { ...values.value, ...relationalData };
103+
}
92104
});
93105

94106
return finalValues;

0 commit comments

Comments
 (0)