Skip to content

Commit 42f5c22

Browse files
committed
feat: implement isAllowedToSave as a function for dynamic save permissions and update error handling
1 parent 9cbe2f3 commit 42f5c22

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

custom/visionAction.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<Dialog ref="confirmDialog">
99
<div
1010
class="fixed inset-0 z-20 flex items-center justify-center bg-black/40"
11-
@click="closeDialog"
1211
>
1312
<div
1413
class="bulk-vision-dialog flex items-center justify-center relative max-w-[95vw] min-w-[640px] max-h-[90vh] bg-white dark:bg-gray-900 rounded-md shadow-2xl overflow-hidden"
@@ -73,7 +72,7 @@ import VisionTable from './visionTable.vue'
7372
import adminforth from '@/adminforth';
7473
import { useI18n } from 'vue-i18n';
7574
import { useRoute } from 'vue-router';
76-
import { type AdminUser, type AdminForthResourceCommon } from '@/types';
75+
import { AdminUser, type AdminForthResourceCommon } from '@/types';
7776
7877
const route = useRoute();
7978
const { t } = useI18n();
@@ -435,7 +434,7 @@ async function saveData() {
435434
props.clearCheckboxes();
436435
} else if (res.ok === false) {
437436
adminforth.alert({
438-
message: 'You are not allowed to save.',
437+
message: res.error,
439438
variant: 'danger',
440439
timeout: 'unlimited',
441440
});

index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
338338
server.endpoint({
339339
method: 'POST',
340340
path: `/plugin/${this.pluginInstanceId}/update_fields`,
341-
handler: async ( body ) => {
342-
if(this.options.isAllowedToSave !== false) {
343-
const selectedIds = body.body.selectedIds || [];
344-
const fieldsToUpdate = body.body.fields || {};
341+
handler: async ({ body, adminUser, headers }) => {
342+
let isAllowedToSave: any = { ok: true, error: '' };
343+
if(this.options.isAllowedToSave) {
344+
isAllowedToSave = await this.options.isAllowedToSave({ record: {}, adminUser: adminUser, resource: this.resourceConfig });
345+
}
346+
if (isAllowedToSave.ok !== false) {
347+
const selectedIds = body.selectedIds || [];
348+
const fieldsToUpdate = body.fields || {};
345349
const outputImageFields = [];
346350
if (this.options.generateImages) {
347351
for (const [key, value] of Object.entries(this.options.generateImages)) {
@@ -380,7 +384,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
380384
await Promise.all(updates);
381385
return { ok: true };
382386
} else {
383-
return { ok: false }
387+
return { ok: false, error: isAllowedToSave.error };
384388
}
385389
}
386390
});

types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,15 @@ export interface PluginOptions {
5151
/**
5252
* Whether the user is allowed to save the generated images
5353
*/
54-
isAllowedToSave?: boolean
54+
isAllowedToSave?: ({ record, adminUser, resource }: {
55+
record: any;
56+
adminUser: any;
57+
resource: any;
58+
}) => Promise<{
59+
ok: boolean;
60+
error: string;
61+
} | {
62+
ok: boolean;
63+
error?: undefined;
64+
}>
5565
}

0 commit comments

Comments
 (0)