Fix load apps

This commit is contained in:
2025-01-22 13:57:33 +08:00
parent d0ce7b8349
commit 6aba3fc065
10 changed files with 205 additions and 85 deletions

View File

@@ -1,6 +1,6 @@
import type { JoinTable } from '@/types/model';
import type { FieldsInfo, JoinTable } from '@/types/model';
import type { KucDropdownItem } from '@/types/my-kintone';
import { client, isType, type App, type Properties, type OneOf } from './kintone-rest-api-client';
import { client, isType, type OneOf, type App, type Layout } from './kintone-rest-api-client';
export const condition = {
unset: '',
@@ -18,50 +18,87 @@ export function createEmptyJoinTable(id = Number(new Date())) {
}
export function resetTable(table: JoinTable) {
table.table = '';
table.whereConditions = [{ field: '', condition: condition.unset, data: '' }];
table.onConditions = [{ leftField: '', rightField: '' }];
table.fieldsMapping = [{ leftField: '', rightField: '' }];
}
export const EMPTY_OPTION = {
value: "",
label: "--------",
} as KucDropdownItem;
const LIMIT = 100; // 每次请求的最大应用数量
let allAppsCache: KucDropdownItem[] = [];
let allFieldsCache: Properties = {};
export const fetchApps = async (offset = 0, _apps: KucDropdownItem[] = []): Promise<KucDropdownItem[]> => {
if (allAppsCache.length) return allAppsCache;
export const loadApps = async (offset = 0, _apps: KucDropdownItem[] = []): Promise<KucDropdownItem[]> => {
const { apps } = await client.app.getApps({ limit: LIMIT, offset });
const allApps: KucDropdownItem[] = [
..._apps,
...apps.map((app: App) => ({ value: app.appId, label: app.name + 'ID: ' + app.appId + '' })),
];
if (apps.length === LIMIT) {
return fetchApps(offset + LIMIT, allApps);
return loadApps(offset + LIMIT, allApps);
}
allAppsCache = allApps.sort((a, b) => Number(b.value) - Number(a.value));
allApps.sort((a, b) => Number(b.value) - Number(a.value));
allApps.unshift(EMPTY_OPTION);
return allApps;
};
export const fetchFields = async (appId: string, filter: OneOf['type']) => {
let properties = allFieldsCache;
if (!allFieldsCache.length) {
properties = (await client.app.getFormFields({ app: appId })).properties;
allFieldsCache = properties;
export const loadAppFieldsAndLayout = async (appId: string | number = kintone.app.getId() as number) => {
return {
fields: (await client.app.getFormFields({ app: appId })).properties,
layout: (await client.app.getFormLayout({ app: appId })).layout,
} as FieldsInfo;
};
export const getFieldsDropdownItems = (
{ fields, layout }: FieldsInfo,
subTable?: string,
filterType?: OneOf['type'],
) => {
let fieldOrder: string[];
if (subTable) {
const subTableFields = layout.find((each) => each.type === 'SUBTABLE' && each.code === subTable) as any;
fieldOrder = subTableFields?.fields.map((field: { code: string }) => field.code) || [];
} else {
fieldOrder = extractNoSubTableFields(layout);
}
const labels = Object.keys(properties).reduce((acc, fieldCode) => {
const field = properties[fieldCode];
if (filter && !isType[filter](field)) return acc;
return fieldOrder.reduce((acc, fieldCode) => {
const field = fields[fieldCode];
if (filterType && !isType[filterType](field)) return acc;
acc.push({
value: fieldCode,
label: field.label + 'FC: ' + fieldCode + '',
});
return acc;
}, [] as KucDropdownItem[]);
return labels;
}, [EMPTY_OPTION]);
};
export const getField = (fieldCode: string) => {
const field = allFieldsCache[fieldCode];
if (isType.SUBTABLE(field)) {
console.log(field.fields);
}
return field;
export const getTableFieldsDropdownItems = ({ fields }: FieldsInfo, filterType?: OneOf['type']) => {
return Object.keys(fields).reduce((acc, fieldCode) => {
const field = fields[fieldCode];
if (filterType && !isType[filterType](field)) return acc;
acc.push({
value: fieldCode,
label: field.label + 'FC: ' + fieldCode + '',
});
return acc;
}, [EMPTY_OPTION]);
};
const extractNoSubTableFields = (layout: Layout) => {
return layout.reduce((acc, each) => {
if (each.type === 'SUBTABLE') {
return acc;
} else if (each.type === 'ROW') {
acc.push(...each.fields.map((field: any) => field.code));
} else if (each.type === 'GROUP') {
acc.push(...extractNoSubTableFields(each.layout));
}
return acc;
}, [] as string[]);
};
// if (isType.SUBTABLE(field)) {
// console.log(field.fields);
// }

View File

@@ -8,6 +8,7 @@ export type App = {
};
export type Properties = Awaited<ReturnType<typeof client.app.getFormFields>>['properties'];
export type Layout = Awaited<ReturnType<typeof client.app.getFormLayout>>['layout'];
export type OneOf = Properties[string];
const typeNames = [