Skip to content

Commit c24b2bd

Browse files
committed
feat: add utility functions for API calls and list management
1 parent 96c5fc7 commit c24b2bd

File tree

3 files changed

+100
-96
lines changed

3 files changed

+100
-96
lines changed

adminforth/spa/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './utils';
2+
export * from './listUtils';
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { nextTick, onMounted, ref, resolveComponent } from 'vue';
2+
import { callAdminForthApi } from '@/utils';
3+
import { type AdminForthResourceCommon } from '../types/Common';
4+
import { useAdminforth } from '@/adminforth';
5+
import { showErrorTost } from '@/composables/useFrontendApi'
6+
7+
8+
export async function getList(resource: AdminForthResourceCommon, isPageLoaded: boolean, page: number | null , pageSize: number, sort: any, checkboxes:{ value: any[] }, filters: any = [] ) {
9+
let rows: any[] = [];
10+
let totalRows: number | null = null;
11+
if (!isPageLoaded) {
12+
return;
13+
}
14+
const data = await callAdminForthApi({
15+
path: '/get_resource_data',
16+
method: 'POST',
17+
body: {
18+
source: 'list',
19+
resourceId: resource.resourceId,
20+
limit: pageSize,
21+
offset: ((page || 1) - 1) * pageSize,
22+
filters: filters,
23+
sort: sort,
24+
}
25+
});
26+
if (data.error) {
27+
showErrorTost(data.error);
28+
rows = [];
29+
totalRows = 0;
30+
return {rows, totalRows, error: data.error};
31+
}
32+
rows = data.data?.map((row: any) => {
33+
if (resource?.columns?.find(c => c.primaryKey)?.foreignResource) {
34+
row._primaryKeyValue = row[resource.columns.find(c => c.primaryKey)!.name].pk;
35+
} else if (resource) {
36+
row._primaryKeyValue = row[resource.columns.find(c => c.primaryKey)!.name];
37+
}
38+
return row;
39+
});
40+
totalRows = data.total;
41+
42+
// if checkboxes have items which are not in current data, remove them
43+
checkboxes.value = checkboxes.value.filter((pk: any) => rows.some((r: any) => r._primaryKeyValue === pk));
44+
await nextTick();
45+
return { rows, totalRows };
46+
}
47+
48+
49+
50+
export async function startBulkAction(actionId: string, resource: AdminForthResourceCommon, checkboxes: { value: any[] },
51+
bulkActionLoadingStates: {value: Record<string, boolean>}, getListInner: () => Promise<any>) {
52+
const action = resource?.options?.bulkActions?.find(a => a.id === actionId);
53+
const { confirm, alert } = useAdminforth();
54+
55+
if (action?.confirm) {
56+
const confirmed = await confirm({
57+
message: action.confirm,
58+
});
59+
if (!confirmed) {
60+
return;
61+
}
62+
}
63+
bulkActionLoadingStates.value[actionId] = true;
64+
65+
const data = await callAdminForthApi({
66+
path: '/start_bulk_action',
67+
method: 'POST',
68+
body: {
69+
resourceId: resource.resourceId,
70+
actionId: actionId,
71+
recordIds: checkboxes.value
72+
}
73+
});
74+
bulkActionLoadingStates.value[actionId] = false;
75+
if (data?.ok) {
76+
checkboxes.value = [];
77+
await getListInner();
78+
79+
if (data.successMessage) {
80+
alert({
81+
message: data.successMessage,
82+
variant: 'success'
83+
});
84+
}
85+
86+
}
87+
if (data?.error) {
88+
showErrorTost(data.error);
89+
}
90+
}
Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { nextTick, onMounted, ref, resolveComponent } from 'vue';
2-
import type { CoreConfig } from './spa_types/core';
3-
import type { ValidationObject } from './types/Common.js';
4-
import router from "./router";
5-
import { useCoreStore } from './stores/core';
6-
import { useUserStore } from './stores/user';
2+
import type { CoreConfig } from '../spa_types/core';
3+
import type { ValidationObject } from '../types/Common.js';
4+
import router from "../router";
5+
import { useCoreStore } from '../stores/core';
6+
import { useUserStore } from '../stores/user';
77
import { Dropdown } from 'flowbite';
8-
import adminforth from './adminforth';
8+
import adminforth from '../adminforth';
99
import sanitizeHtml from 'sanitize-html'
1010
import debounce from 'debounce';
1111
import type { AdminForthResourceColumnInputCommon, Predicate } from '@/types/Common';
12-
import { i18nInstance } from './i18n'
12+
import { i18nInstance } from '../i18n'
1313

14-
import { type AdminForthResourceCommon } from './types/Common';
15-
import { useAdminforth } from '@/adminforth';
16-
import { showErrorTost } from '@/composables/useFrontendApi'
1714

1815

1916
const LS_LANG_KEY = `afLanguage`;
@@ -117,7 +114,7 @@ export const loadFile = (file: string) => {
117114
baseUrl = new URL(`./${path}`, import.meta.url).href;
118115
} else if (file.startsWith('@@/')) {
119116
path = file.replace('@@/', '');
120-
baseUrl = new URL(`./custom/${path}`, import.meta.url).href;
117+
baseUrl = new URL(`../custom/${path}`, import.meta.url).href;
121118
} else {
122119
baseUrl = new URL(`./${file}`, import.meta.url).href;
123120
}
@@ -519,88 +516,3 @@ export function btoa_function(source: string): string {
519516
export function atob_function(source: string): string {
520517
return atob(source);
521518
}
522-
523-
524-
export async function getList(resource: AdminForthResourceCommon, isPageLoaded: boolean, page: number | null , pageSize: number, sort: any, checkboxes:{ value: any[] }, filters: any = [] ) {
525-
let rows: any[] = [];
526-
let totalRows: number | null = null;
527-
if (!isPageLoaded) {
528-
return;
529-
}
530-
const data = await callAdminForthApi({
531-
path: '/get_resource_data',
532-
method: 'POST',
533-
body: {
534-
source: 'list',
535-
resourceId: resource.resourceId,
536-
limit: pageSize,
537-
offset: ((page || 1) - 1) * pageSize,
538-
filters: filters,
539-
sort: sort,
540-
}
541-
});
542-
if (data.error) {
543-
showErrorTost(data.error);
544-
rows = [];
545-
totalRows = 0;
546-
return {rows, totalRows, error: data.error};
547-
}
548-
rows = data.data?.map((row: any) => {
549-
if (resource?.columns?.find(c => c.primaryKey)?.foreignResource) {
550-
row._primaryKeyValue = row[resource.columns.find(c => c.primaryKey)!.name].pk;
551-
} else if (resource) {
552-
row._primaryKeyValue = row[resource.columns.find(c => c.primaryKey)!.name];
553-
}
554-
return row;
555-
});
556-
totalRows = data.total;
557-
558-
// if checkboxes have items which are not in current data, remove them
559-
checkboxes.value = checkboxes.value.filter((pk: any) => rows.some((r: any) => r._primaryKeyValue === pk));
560-
await nextTick();
561-
return { rows, totalRows };
562-
}
563-
564-
565-
566-
export async function startBulkAction(actionId: string, resource: AdminForthResourceCommon, checkboxes: { value: any[] },
567-
bulkActionLoadingStates: {value: Record<string, boolean>}, getListInner: () => Promise<any>) {
568-
const action = resource?.options?.bulkActions?.find(a => a.id === actionId);
569-
const { confirm, alert } = useAdminforth();
570-
571-
if (action?.confirm) {
572-
const confirmed = await confirm({
573-
message: action.confirm,
574-
});
575-
if (!confirmed) {
576-
return;
577-
}
578-
}
579-
bulkActionLoadingStates.value[actionId] = true;
580-
581-
const data = await callAdminForthApi({
582-
path: '/start_bulk_action',
583-
method: 'POST',
584-
body: {
585-
resourceId: resource.resourceId,
586-
actionId: actionId,
587-
recordIds: checkboxes.value
588-
}
589-
});
590-
bulkActionLoadingStates.value[actionId] = false;
591-
if (data?.ok) {
592-
checkboxes.value = [];
593-
await getListInner();
594-
595-
if (data.successMessage) {
596-
alert({
597-
message: data.successMessage,
598-
variant: 'success'
599-
});
600-
}
601-
602-
}
603-
if (data?.error) {
604-
showErrorTost(data.error);
605-
}
606-
}

0 commit comments

Comments
 (0)