Skip to content

Commit 45eb43c

Browse files
committed
feat: add CloneRowButton component and integrate into resource configuration
1 parent fa5ab57 commit 45eb43c

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

custom/CloneRowButton.vue

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<template>
2+
<Tooltip>
3+
<RouterLink :to="{
4+
name: `resource-create`,
5+
params: { resourceId: props.meta.resourceId },
6+
query: {
7+
values: save_btoa(JSON.stringify(redirectToCreatePage())),
8+
}
9+
}">
10+
<IconFileCloneOutline class="w-5 h-5 me-2"/>
11+
</RouterLink>
12+
<template v-slot:tooltip>
13+
{{ $t('Clone record') }}
14+
</template>
15+
</Tooltip>
16+
</template>
17+
18+
<script lang="ts" setup>
19+
import { Tooltip } from '@/afcl';
20+
import { AdminUser, type AdminForthResourceCommon } from '@/types';
21+
import { IconFileCloneOutline } from '@iconify-prerendered/vue-flowbite';
22+
import { useI18n } from 'vue-i18n';
23+
24+
const { t } = useI18n();
25+
26+
const props = defineProps<{
27+
meta: any,
28+
resource: AdminForthResourceCommon,
29+
adminUser: AdminUser,
30+
record: any,
31+
updateRecords: Function,
32+
}>();
33+
34+
function redirectToCreatePage() {
35+
const fieldsToFill = { ...props.resource.columns.filter((col: any) => col.showIn.create === true) }
36+
let dataToFill = {};
37+
for (const field of Object.values(fieldsToFill)) {
38+
dataToFill[field.name] = props.record[field.name];
39+
}
40+
return dataToFill;
41+
}
42+
43+
function save_btoa(str) {
44+
return btoa(str);
45+
}
46+
</script>

index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AdminForthPlugin } from "adminforth";
2-
import type { IAdminForth, IHttpServer, AdminForthResourcePages, AdminForthResourceColumn, AdminForthDataTypes, AdminForthResource } from "adminforth";
2+
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthResource, IAdminForthHttpResponse, AdminUser, AdminForthComponentDeclaration } from "adminforth";
33
import type { PluginOptions } from './types.js';
44

55

@@ -14,6 +14,19 @@ export default class extends AdminForthPlugin {
1414
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
1515
super.modifyResourceConfig(adminforth, resourceConfig);
1616

17+
if ( !resourceConfig.options.pageInjections ) {
18+
resourceConfig.options.pageInjections = {};
19+
}
20+
if ( !resourceConfig.options.pageInjections.list ) {
21+
resourceConfig.options.pageInjections.list = {};
22+
}
23+
if ( !resourceConfig.options.pageInjections.list.customActionIcons ) {
24+
resourceConfig.options.pageInjections.list.customActionIcons = [];
25+
}
26+
(resourceConfig.options.pageInjections.list.customActionIcons as AdminForthComponentDeclaration[]).push(
27+
{ file: this.componentPath('CloneRowButton.vue'), meta: { pluginInstanceId: this.pluginInstanceId, resourceId: this.resourceConfig.resourceId } }
28+
);
29+
1730
// simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
1831
}
1932

0 commit comments

Comments
 (0)