Skip to content

Commit d70b0ae

Browse files
committed
fix: fix isUnique resource constraint for the records edit
1 parent 9337258 commit d70b0ae

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
319319
}
320320
recordWithOriginalValues[col.name] = this.setFieldValue(col, newValues[col.name]);
321321
}
322+
323+
let error: string | null = null;
324+
await Promise.all(
325+
resource.dataSourceColumns.map(async (col) => {
326+
if (col.isUnique && !col.virtual && !error) {
327+
const exists = await this.checkUnique(resource, col, recordWithOriginalValues[col.name]);
328+
if (exists) {
329+
error = `Record with ${col.name} ${recordWithOriginalValues[col.name]} already exists`;
330+
}
331+
}
332+
})
333+
);
334+
if (error) {
335+
process.env.HEAVY_DEBUG && console.log('🪲🆕 check unique error', error);
336+
return { error, ok: false };
337+
}
338+
322339

323340
process.env.HEAVY_DEBUG && console.log(`🪲✏️ updating record id:${recordId}, values: ${JSON.stringify(recordWithOriginalValues)}`);
324341

adminforth/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,10 @@ class AdminForth implements IAdminForth {
645645
}
646646

647647
if (Object.keys(newValues).length > 0) {
648-
await connector.updateRecord({ resource, recordId, newValues });
648+
const { error } = await connector.updateRecord({ resource, recordId, newValues });
649+
if ( error ) {
650+
return { error };
651+
}
649652
}
650653

651654
// execute hook if needed

adminforth/spa/src/views/EditView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ async function saveRecord() {
191191
variant: 'success',
192192
timeout: 400000
193193
});
194+
router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: resp.recordId } });
194195
}
195196
saving.value = false;
196-
router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: resp.recordId } });
197197
}
198198
199199
</script>

0 commit comments

Comments
 (0)