Skip to content

Commit e486b33

Browse files
committed
feat: enhance FrontendAPI with save interceptor methods and update views to utilize them
1 parent 5a5170d commit e486b33

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

adminforth/spa/src/adminforth.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,14 @@ export function useAdminforth() {
228228
const api = frontendAPI as FrontendAPI;
229229
return {
230230
registerSaveInterceptor: (handler: (ctx: { action: 'create'|'edit'; values: any; resource: any; }) => Promise<{ ok: boolean; error?: string | null; extra?: object; }>) => api.registerSaveInterceptor(handler),
231-
api,
231+
alert: (params: AlertParams) => api.alert(params),
232+
confirm: (params: ConfirmParams) => api.confirm(params),
233+
list: api.list,
234+
show: api.show,
235+
menu: api.menu,
236+
closeUserMenuDropdown: () => api.closeUserMenuDropdown(),
237+
runSaveInterceptors: (params: { action: 'create'|'edit'; values: any; resource: any; resourceId: string; }) => api.runSaveInterceptors(params),
238+
clearSaveInterceptors: (resourceId?: string) => api.clearSaveInterceptors(resourceId),
232239
};
233240
}
234241

adminforth/spa/src/views/CreateView.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const router = useRouter();
103103
const record = ref({});
104104
105105
const coreStore = useCoreStore();
106-
const { api } = useAdminforth();
106+
const { clearSaveInterceptors, runSaveInterceptors } = useAdminforth();
107107
108108
const { t } = useI18n();
109109
@@ -119,7 +119,7 @@ async function onUpdateRecord(newRecord: any) {
119119
}
120120
121121
onBeforeMount(() => {
122-
api.clearSaveInterceptors(route.params.resourceId as string);
122+
clearSaveInterceptors(route.params.resourceId as string);
123123
});
124124
125125
onMounted(async () => {
@@ -174,7 +174,7 @@ async function saveRecord() {
174174
const requiredColumns = coreStore.resource?.columns.filter(c => c.required?.create === true) || [];
175175
const requiredColumnsToSkip = requiredColumns.filter(c => checkShowIf(c, record.value, coreStore.resource?.columns || []) === false);
176176
saving.value = true;
177-
const interceptorsResult = await api.runSaveInterceptors({
177+
const interceptorsResult = await runSaveInterceptors({
178178
action: 'create',
179179
values: record.value,
180180
resource: coreStore.resource,

adminforth/spa/src/views/EditView.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import type { AdminForthResourceColumn } from '@/types/Back';
8787
8888
const { t } = useI18n();
8989
const coreStore = useCoreStore();
90-
const { api } = useAdminforth();
90+
const { clearSaveInterceptors, runSaveInterceptors } = useAdminforth();
9191
9292
const isValid = ref(false);
9393
const validating = ref(false);
@@ -129,7 +129,7 @@ const editableRecord = computed(() => {
129129
})
130130
131131
onBeforeMount(() => {
132-
api.clearSaveInterceptors(route.params.resourceId as string);
132+
clearSaveInterceptors(route.params.resourceId as string);
133133
});
134134
135135
onMounted(async () => {
@@ -164,7 +164,7 @@ async function saveRecord() {
164164
}
165165
166166
saving.value = true;
167-
const interceptorsResult = await api.runSaveInterceptors({
167+
const interceptorsResult = await runSaveInterceptors({
168168
action: 'edit',
169169
values: record.value,
170170
resource: coreStore.resource,

adminforth/types/FrontendAPI.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ export interface FrontendAPIInterface {
136136
* Close the user menu dropdown
137137
*/
138138
closeUserMenuDropdown(): void;
139+
140+
/**
141+
* Run save interceptors for a specific resource or all resources if no resourceId is provided
142+
*/
143+
runSaveInterceptors(params: { action: 'create'|'edit'; values: any; resource: any; resourceId: string; }): Promise<{ ok: boolean; error?: string | null; extra?: object; }>;
144+
145+
/**
146+
* Clear save interceptors for a specific resource or all resources if no resourceId is provided
147+
*
148+
* @param resourceId - The resource ID to clear interceptors for
149+
*/
150+
clearSaveInterceptors(resourceId?: string): void;
139151
}
140152

141153
export type ConfirmParams = {

dev-demo/custom/AfComponents.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ import { IconSearchOutline } from '@iconify-prerendered/vue-flowbite'
317317
import { DatePicker } from '@/afcl';
318318
import CustomRangePicker from "@/components/CustomRangePicker.vue";
319319
import Toast from '@/components/Toast.vue';
320+
import { useAdminforth } from '@/adminforth';
321+
322+
const { alert } = useAdminforth();
320323
321324
const files: Ref<File[]> = ref([])
322325
@@ -338,7 +341,7 @@ watch(valueStart, (newVal) => {
338341
});
339342
340343
function doSmth(){
341-
344+
alert({message: 'Hello world', variant: 'success'})
342345
}
343346
344347
</script>

0 commit comments

Comments
 (0)