-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
83 lines (70 loc) · 2.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { AdminForthPlugin } from "adminforth";
import type { IAdminForth, IHttpServer, AdminForthResourcePages, AdminForthResourceColumn, AdminForthDataTypes, AdminForthResource } from "adminforth";
import type { PluginOptions } from './types.js';
export default class ListInPlaceEditPlugin extends AdminForthPlugin {
options: PluginOptions;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
const targetColumns = resourceConfig.columns.filter(col =>
this.options.columns.includes(col.name)
);
targetColumns.forEach(column => {
if (column.components?.list) {
throw new Error(`Column ${column.name} already has a list component defined. ListInplaceEdit plugin cannot be used on columns that already have list components.`);
}
if (!column.components) {
column.components = {};
}
column.components.list = {
file: this.componentPath('InPlaceEdit.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
columnName: column.name
}
};
});
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
// Validate that columns exist if specific columns were specified
this.options.columns.forEach(colName => {
if (!resourceConfig.columns.find(c => c.name === colName)) {
throw new Error(`Column ${colName} specified in ListInplaceEdit plugin not found in resource ${resourceConfig.label}`);
}
});
}
instanceUniqueRepresentation(pluginOptions: any): string {
return 'single';
}
setupEndpoints(server: IHttpServer) {
// Add endpoint to update single field
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/update-field`,
handler: async ({ body, adminUser }) => {
const { resourceId, recordId, field, value } = body;
const resource = this.adminforth.config.resources.find(r => r.resourceId === resourceId);
// Create update object with just the single field
const updateRecord = { [field]: value };
// Use AdminForth's built-in update method
const connector = this.adminforth.connectors[resource.dataSource];
const oldRecord = await connector.getRecordByPrimaryKey(resource, recordId)
const result = await this.adminforth.updateResourceRecord({
resource,
recordId,
record: updateRecord,
oldRecord,
adminUser
});
if (result.error) {
return { error: result.error };
}
const updatedRecord = await connector.getRecordByPrimaryKey(resource, recordId);
return { record: updatedRecord };
}
});
}
}