Skip to content

Commit b2d0a7c

Browse files
committed
Fix bugs when creating new item
1 parent 47637bb commit b2d0a7c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/utils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ export function checkFieldInTemplate(template: string, field: string) {
99
}
1010

1111
/** Simple check which fields are used */
12-
function shouldUpdate(template: string, computedField: string, val: Record<string, any>, oldVal: Record<string, any>) {
12+
function shouldUpdate(
13+
template: string,
14+
computedField: string,
15+
val: Record<string, any>,
16+
oldVal: Record<string, any>,
17+
pk: string | number,
18+
) {
19+
// creating new item
20+
if (val.id && pk === '+') {
21+
return false;
22+
}
23+
1324
for (const key of Object.keys(val)) {
1425
if (
1526
key !== computedField &&
@@ -59,11 +70,12 @@ export const useDeepValues = (
5970
async (val, oldVal) => {
6071
const valObj = JSON.parse(val);
6172
const oldValObj = oldVal !== undefined ? JSON.parse(oldVal) : {};
62-
if (!shouldUpdate(template, computedField, valObj, oldValObj)) {
73+
if (!shouldUpdate(template, computedField, valObj, oldValObj, pk)) {
6374
return;
6475
}
6576

6677
let relationalData: Record<string, any> = {};
78+
const pkFinal = values.value.id || pk;
6779

6880
for (const key of Object.keys(values.value)) {
6981
const relation = relations.value.find((rel) => [rel.meta?.one_field, rel.meta?.many_field].includes(key));
@@ -109,12 +121,12 @@ export const useDeepValues = (
109121
itemCache = {};
110122
}
111123

112-
if (pk !== '+') {
124+
if (pkFinal !== '+') {
113125
let data;
114126
if (key in fieldCache) {
115127
data = fieldCache[key];
116128
} else {
117-
data = (await api.get(`items/${collection}/${pk}`, {
129+
data = (await api.get(`items/${collection}/${pkFinal}`, {
118130
params: {
119131
fields: [key],
120132
},

0 commit comments

Comments
 (0)