Skip to content

Commit 5fda99a

Browse files
committed
feat: add primaryKey prop to vision components and update related logic
1 parent 653c16f commit 5fda99a

File tree

4 files changed

+57
-60
lines changed

4 files changed

+57
-60
lines changed

custom/visionAction.vue

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
:tableColumnsIndexes="tableColumnsIndexes"
2626
:selected="selected"
2727
:isAiResponseReceived="isAiResponseReceived"
28+
:primaryKey="primaryKey"
2829
/>
2930
<Button
3031
class="w-64"
@@ -67,6 +68,7 @@ const tableColumnsIndexes = ref([]);
6768
const customFieldNames = ref([]);
6869
const selected = ref<any[]>([]);
6970
const isAiResponseReceived = ref([]);
71+
const primaryKey = props.meta.primaryKey;
7072
7173
const openDialog = async () => {
7274
confirmDialog.value.open();
@@ -82,9 +84,9 @@ const openDialog = async () => {
8284
analyzeFields();
8385
}
8486
85-
// watch(selected, (val) => {
86-
// console.log('Selected changed:', val);
87-
// }, { deep: true });
87+
watch(selected, (val) => {
88+
console.log('Selected changed:', val);
89+
}, { deep: true });
8890
8991
const closeDialog = () => {
9092
confirmDialog.value.close();
@@ -105,14 +107,11 @@ function generateTableHeaders(outputFields) {
105107
headers.push({ label: 'Field name', fieldName: 'label' });
106108
headers.push({ label: 'Source Images', fieldName: 'images' });
107109
108-
if (outputFields.length > 0) {
109-
const sampleField = outputFields[0];
110-
for (const key in sampleField) {
111-
headers.push({
112-
label: formatLabel(key),
113-
fieldName: key,
114-
});
115-
}
110+
for (const key in outputFields) {
111+
headers.push({
112+
label: formatLabel(key),
113+
fieldName: key,
114+
});
116115
}
117116
return headers;
118117
}
@@ -144,22 +143,19 @@ function generateTableColumns() {
144143
145144
function setSelected() {
146145
selected.value = records.value.map(() => ({}));
147-
148146
records.value.forEach((record, index) => {
149-
props.meta.outputFields.forEach((fieldObj, i) => {
150-
for (const key in fieldObj) {
151-
if(isInColumnEnum(key)){
152-
const colEnum = props.meta.columnEnums.find(c => c.name === key);
153-
const object = colEnum.enum.find(item => item.value === record[key]);
154-
selected.value[index][key] = object ? record[key] : null;
155-
} else {
156-
selected.value[index][key] = record[key];
157-
}
147+
for (const key in props.meta.outputFields) {
148+
if(isInColumnEnum(key)){
149+
const colEnum = props.meta.columnEnums.find(c => c.name === key);
150+
const object = colEnum.enum.find(item => item.value === record[key]);
151+
selected.value[index][key] = object ? record[key] : null;
152+
} else {
153+
selected.value[index][key] = record[key];
158154
}
159-
selected.value[index].isChecked = true;
160-
selected.value[index].id = record.id;
161-
isAiResponseReceived.value[index] = true;
162-
});
155+
}
156+
selected.value[index].isChecked = true;
157+
selected.value[index][primaryKey] = record[primaryKey];
158+
isAiResponseReceived.value[index] = true;
163159
});
164160
}
165161

custom/visionTable.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,32 @@
4444
</template>
4545
<!-- CUSTOM FIELD TEMPLATES -->
4646
<template v-for="n in customFieldNames" :key="n" #[`cell:${n}`]="{ item, column }">
47-
<div v-if="isAiResponseReceived[tableColumnsIndexes.findIndex(el => el.id === item.id)]">
47+
<div v-if="isAiResponseReceived[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])]">
4848
<div v-if="isInColumnEnum(n)">
4949
<Select
5050
:options="convertColumnEnumToSelectOptions(props.meta.columnEnums, n)"
51-
v-model="selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n]"
51+
v-model="selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n]"
5252
>
5353
</Select>
5454
</div>
55-
<div v-else-if="typeof selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n] === 'string' || typeof selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n] === 'object'">
55+
<div v-else-if="typeof selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n] === 'string' || typeof selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n] === 'object'">
5656
<Textarea
5757
class="w-full h-full"
5858
type="text"
59-
v-model="selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n]"
59+
v-model="selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n]"
6060
>
6161
</Textarea>
6262
</div>
63-
<div v-else-if="typeof selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n] === 'boolean'">
63+
<div v-else-if="typeof selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n] === 'boolean'">
6464
<Toggle
65-
v-model="selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n]"
65+
v-model="selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n]"
6666
>
6767
</Toggle>
6868
</div>
6969
<div v-else>
7070
<Input
7171
type="number"
72-
v-model="selected[tableColumnsIndexes.findIndex(el => el.id === item.id)][n]"
72+
v-model="selected[tableColumnsIndexes.findIndex(el => el[primaryKey] === item[primaryKey])][n]"
7373
class="w-full "
7474
:fullWidth="true"
7575
/>
@@ -95,7 +95,8 @@ const props = defineProps<{
9595
customFieldNames: any,
9696
tableColumnsIndexes: any,
9797
selected: any,
98-
isAiResponseReceived: boolean[]
98+
isAiResponseReceived: boolean[],
99+
primaryKey: any
99100
}>();
100101
101102
const zoomedImage = ref(null)

index.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
1414
}
1515

1616
// Compile Handlebars templates in outputFields using record fields as context
17-
private compileOutputFieldsTemplates(record: any): Record<string, string>[] {
18-
return this.options.outputFields.map((fieldObj) => {
19-
const compiled: Record<string, string> = {};
20-
for (const [key, templateStr] of Object.entries(fieldObj)) {
21-
try {
22-
const tpl = Handlebars.compile(String(templateStr));
23-
compiled[key] = tpl(record);
24-
} catch {
25-
compiled[key] = String(templateStr);
26-
}
17+
private compileOutputFieldsTemplates(record: any): Record<string, string> {
18+
const compiled: Record<string, string> = {};
19+
for (const [key, templateStr] of Object.entries(this.options.fillFieldsFromImages)) {
20+
try {
21+
const tpl = Handlebars.compile(String(templateStr));
22+
compiled[key] = tpl(record);
23+
} catch {
24+
compiled[key] = String(templateStr);
2725
}
28-
return compiled;
29-
});
26+
}
27+
return compiled;
3028
}
3129

3230
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
@@ -35,30 +33,32 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
3533
//check if options names are provided
3634
const columns = this.resourceConfig.columns;
3735
let columnEnums = [];
38-
for (const field of this.options.outputFields) {
39-
for (const [key, value] of Object.entries(field)) {
40-
const column = columns.find(c => c.name.toLowerCase() === key.toLowerCase());
41-
if (column) {
42-
if(column.enum){
43-
(field as any)[key] = `${value} Select ${key} from the list (USE ONLY VALUE FIELD. USE ONLY VALUES FROM THIS LIST): ${JSON.stringify(column.enum)}`;
44-
columnEnums.push({
45-
name: key,
46-
enum: column.enum,
47-
});
48-
}
49-
} else {
50-
throw new Error(`⚠️ No column found for key "${key}"`);
36+
for (const [key, value] of Object.entries(this.options.fillFieldsFromImages)) {
37+
const column = columns.find(c => c.name.toLowerCase() === key.toLowerCase());
38+
if (column) {
39+
if(column.enum){
40+
(this.options.fillFieldsFromImages as any)[key] = `${value} Select ${key} from the list (USE ONLY VALUE FIELD. USE ONLY VALUES FROM THIS LIST): ${JSON.stringify(column.enum)}`;
41+
columnEnums.push({
42+
name: key,
43+
enum: column.enum,
44+
});
5145
}
46+
} else {
47+
throw new Error(`⚠️ No column found for key "${key}"`);
5248
}
5349
}
5450

51+
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
52+
console.log('Primary Key Column:', primaryKeyColumn);
53+
5554
const pageInjection = {
5655
file: this.componentPath('visionAction.vue'),
5756
meta: {
5857
pluginInstanceId: this.pluginInstanceId,
59-
outputFields: this.options.outputFields,
58+
outputFields: this.options.fillFieldsFromImages,
6059
actionName: this.options.actionName,
6160
columnEnums: columnEnums,
61+
primaryKey: primaryKeyColumn.name,
6262
}
6363
}
6464

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImageVisionAdapter, AdminUser, IAdminForth, StorageAdapter } from "admi
44
export interface PluginOptions {
55
actionName: string,
66
visionAdapter: ImageVisionAdapter,
7-
outputFields: Record<string, string>[],
7+
fillFieldsFromImages: Record<string, string>,
88
attachFiles?: ({ record }: {
99
record: any,
1010
}) => string[] | Promise<string[]>,

0 commit comments

Comments
 (0)