Skip to content

Commit a698e8c

Browse files
committed
fix: fix shouldUpdate return false in v10.5.2
Directus v10.5.2 sometimes passes primaryKey = '+' on initial load. The interface passes this field as value so it does not change when the primaryKey is updated.
1 parent 4808bc6 commit a698e8c

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/interface.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</template>
1818

1919
<script lang="ts">
20-
import { ComputedRef, defineComponent, inject, ref, watch } from 'vue';
20+
import { ComputedRef, defineComponent, inject, ref, watch, toRefs } from 'vue';
2121
import { parseExpression } from './operations';
2222
import { useDeepValues, useCollectionRelations } from './utils';
2323
import { useCollection } from '@directus/extensions-sdk';
@@ -82,12 +82,14 @@ export default defineComponent({
8282
const defaultValues = useCollection(props.collection).defaults
8383
const computedValue = ref<string | number | null>(props.value);
8484
const relations = useCollectionRelations(props.collection);
85+
const { collection, field, primaryKey } = toRefs(props)
86+
8587
const values = useDeepValues(
8688
inject<ComputedRef<Record<string, any>>>('values')!,
8789
relations,
88-
props.collection,
89-
props.field,
90-
props.primaryKey,
90+
collection,
91+
field,
92+
primaryKey,
9193
props.template
9294
);
9395
const errorMsg = ref<string | null>(null);

src/utils.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ function shouldUpdate(
1414
computedField: string,
1515
val: Record<string, any>,
1616
oldVal: Record<string, any>,
17-
pk: string | number,
1817
) {
19-
// creating new item
20-
if (val.id && pk === '+') {
21-
return false;
22-
}
23-
18+
const changedFields = [];
2419
for (const key of Object.keys({ ...oldVal, ...val })) {
2520
if (
2621
key !== computedField &&
27-
checkFieldInTemplate(template, key) &&
2822
val[key] !== oldVal[key] &&
2923
JSON.stringify(val[key]) !== JSON.stringify(oldVal[key])
3024
) {
31-
return true;
25+
changedFields.push(key);
3226
}
3327
}
34-
return false;
28+
29+
if (!changedFields.length) {
30+
// update even if no fields changed
31+
return true;
32+
}
33+
34+
return changedFields.some((field) => checkFieldInTemplate(template, field));
3535
}
3636

3737
export const useCollectionRelations = (collection: string): Ref<Relation[]> => {
@@ -49,14 +49,16 @@ interface IRelationUpdate {
4949
export const useDeepValues = (
5050
values: Ref<Record<string, any>>,
5151
relations: Ref<Relation[]>,
52-
collection: string,
53-
computedField: string,
54-
pk: string | number,
52+
collection: Ref<string>,
53+
computedField: Ref<string>,
54+
pk: Ref<string | number>,
5555
template: string
5656
) => {
5757
const api = useApi();
58-
const { currentUser } = useStores().useUserStore();
59-
const finalValues = ref<Record<string, any>>({});
58+
const userStore = useStores().useUserStore();
59+
const finalValues = ref<Record<string, any>>({
60+
__currentUser: userStore.currentUser,
61+
});
6062
let fieldCache: Record<string, any> = {};
6163
let itemCache: Record<string, any> = {};
6264
// Directus store o2m value as reference so when o2m updated, val & oldVal in watch are the same.
@@ -71,7 +73,7 @@ export const useDeepValues = (
7173
async (val, oldVal) => {
7274
const valObj = JSON.parse(val);
7375
const oldValObj = oldVal !== undefined ? JSON.parse(oldVal) : {};
74-
if (!shouldUpdate(template, computedField, valObj, oldValObj, pk)) {
76+
if (!shouldUpdate(template, computedField.value, valObj, oldValObj)) {
7577
return;
7678
}
7779

@@ -91,7 +93,7 @@ export const useDeepValues = (
9193
continue;
9294
}
9395

94-
const isM2O = relation.collection === collection;
96+
const isM2O = relation.collection === collection.value;
9597
const fieldName = isM2O ? relation.meta?.many_field : relation.meta?.one_field;
9698

9799
let fieldChanges = valObj[fieldName!] as IRelationUpdate ?? {
@@ -195,7 +197,7 @@ export const useDeepValues = (
195197
relationalData[key] = isM2O ? arrayOfData[0] : arrayOfData;
196198
}
197199

198-
finalValues.value = { ...valObj, ...relationalData, __currentUser: currentUser };
200+
finalValues.value = { ...valObj, ...relationalData, __currentUser: userStore.currentUser };
199201
},
200202
{
201203
deep: false,

0 commit comments

Comments
 (0)