enumOptions and enum reader used everywhere
This commit is contained in:
@@ -96,7 +96,7 @@ export default function EnhancedTable({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (muiType === 'singleSelect' && field.options) {
|
if (muiType === 'singleSelect') {
|
||||||
col.valueOptions = toGridValueOptions(getFieldOptions(field));
|
col.valueOptions = toGridValueOptions(getFieldOptions(field));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,7 +381,7 @@ function FieldRenderer({ params, field, fieldKey, config, onNavigate, navigate,
|
|||||||
|
|
||||||
if (field.type === 'datetime' || field.type === 'date') return value ? new Date(value).toLocaleString() : '';
|
if (field.type === 'datetime' || field.type === 'date') return value ? new Date(value).toLocaleString() : '';
|
||||||
|
|
||||||
if (field.type === 'enum' && field.options) {
|
if (field.type === 'enum') {
|
||||||
const opt = getFieldOptions(field).find(o => o.key === value);
|
const opt = getFieldOptions(field).find(o => o.key === value);
|
||||||
return opt?.value ?? value;
|
return opt?.value ?? value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,8 +111,8 @@ function extractOptions(
|
|||||||
): string[] {
|
): string[] {
|
||||||
const values = new Set<string>();
|
const values = new Set<string>();
|
||||||
|
|
||||||
if (field.type === 'enum' && field.options) {
|
if (field.type === 'enum') {
|
||||||
return getFieldOptions(field).map(o => o.key);
|
return getFieldOptions(field).map(o => o.value);
|
||||||
}
|
}
|
||||||
if (!data) return [];
|
if (!data) return [];
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,16 @@ interface ResourceViewProps {
|
|||||||
|
|
||||||
import { GridPaginationModel } from '@mui/x-data-grid';
|
import { GridPaginationModel } from '@mui/x-data-grid';
|
||||||
|
|
||||||
function getFilterDisplayFields(field: ResourceField): string[] {
|
function getDisplayString(item: any, field: ResourceField): string {
|
||||||
if (!field.displayField) return [];
|
if (item == null || typeof item !== 'object') return String(item ?? '');
|
||||||
return (Array.isArray(field.displayField) ? field.displayField : [field.displayField]).filter(
|
if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, item);
|
||||||
(df): df is string => !!df
|
const df = field.displayField;
|
||||||
);
|
if (!df) return item.name ?? item.title ?? item.label ?? item.id ?? JSON.stringify(item);
|
||||||
|
if (Array.isArray(df)) {
|
||||||
|
const parts = df.map((k: string) => item[k]).filter((v: any) => v != null);
|
||||||
|
return parts.length > 0 ? parts.join(' ') : '';
|
||||||
|
}
|
||||||
|
return String(item[df] ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyClientFilters(
|
function applyClientFilters(
|
||||||
@@ -60,20 +65,12 @@ function applyClientFilters(
|
|||||||
|
|
||||||
if (Array.isArray(filterValue)) {
|
if (Array.isArray(filterValue)) {
|
||||||
if (field.type === "array" && Array.isArray(itemValue)) {
|
if (field.type === "array" && Array.isArray(itemValue)) {
|
||||||
return itemValue.some((el: any) => {
|
return itemValue.some((el: any) =>
|
||||||
if (el != null && typeof el === "object") {
|
filterValue.includes(getDisplayString(el, field))
|
||||||
if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, el));
|
);
|
||||||
const dispFields = getFilterDisplayFields(field);
|
|
||||||
return dispFields.some((df) => filterValue.includes(String(el[df])));
|
|
||||||
}
|
|
||||||
return filterValue.includes(String(el));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (itemValue && typeof itemValue === "object") {
|
if (itemValue && typeof itemValue === "object") {
|
||||||
if (field.enumOption?.value) return filterValue.includes(resolveTemplate(field.enumOption.value, itemValue));
|
return filterValue.includes(getDisplayString(itemValue, field));
|
||||||
const dispFields = getFilterDisplayFields(field);
|
|
||||||
const itemDisplay = dispFields.map((df) => itemValue[df]).filter((v) => v != null).join(" ");
|
|
||||||
return filterValue.includes(itemDisplay);
|
|
||||||
}
|
}
|
||||||
return filterValue.includes(String(itemValue));
|
return filterValue.includes(String(itemValue));
|
||||||
}
|
}
|
||||||
@@ -85,20 +82,13 @@ function applyClientFilters(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === "array" && Array.isArray(itemValue)) {
|
if (field.type === "array" && Array.isArray(itemValue)) {
|
||||||
return itemValue.some((el: any) => {
|
return itemValue.some((el: any) =>
|
||||||
if (el != null && typeof el === "object") {
|
getDisplayString(el, field) === String(filterValue)
|
||||||
if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, el) === String(filterValue);
|
);
|
||||||
const dispFields = getFilterDisplayFields(field);
|
|
||||||
return dispFields.some((df) => String(el[df]) === String(filterValue));
|
|
||||||
}
|
|
||||||
return String(el) === String(filterValue);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemValue && typeof itemValue === "object") {
|
if (itemValue && typeof itemValue === "object") {
|
||||||
if (field.enumOption?.value) return resolveTemplate(field.enumOption.value, itemValue) === String(filterValue);
|
return getDisplayString(itemValue, field) === String(filterValue);
|
||||||
const dispFields = getFilterDisplayFields(field);
|
|
||||||
return dispFields.some((df) => String(itemValue[df]) === String(filterValue));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return String(itemValue) === String(filterValue);
|
return String(itemValue) === String(filterValue);
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ export default function FormField({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 5. Enum Handling
|
// 5. Enum Handling
|
||||||
if (field.type === 'enum' && field.options) {
|
if (field.type === 'enum') {
|
||||||
const options = getFieldOptions(field);
|
const options = getFieldOptions(field);
|
||||||
return (
|
return (
|
||||||
<FormControl fullWidth>
|
<FormControl fullWidth>
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ export interface AppConfig {
|
|||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
authBaseUrl: string;
|
authBaseUrl: string;
|
||||||
resources: ResourceConfig[];
|
resources: ResourceConfig[];
|
||||||
|
enums: Record<string, string[]>;
|
||||||
profile?: {
|
profile?: {
|
||||||
resource: string;
|
resource: string;
|
||||||
extraFields?: Record<string, any>;
|
extraFields?: Record<string, any>;
|
||||||
|
|||||||
@@ -36,6 +36,26 @@ function mapOpenApiType(prop: any): FieldType {
|
|||||||
/**
|
/**
|
||||||
* Recursively converts OpenAPI schemas to ResourceField map
|
* Recursively converts OpenAPI schemas to ResourceField map
|
||||||
*/
|
*/
|
||||||
|
function mergeProperties(schema: any): { properties: Record<string, any>; required: string[] } {
|
||||||
|
let properties: Record<string, any> = {};
|
||||||
|
let required: string[] = [];
|
||||||
|
|
||||||
|
if (schema.allOf) {
|
||||||
|
for (const sub of schema.allOf) {
|
||||||
|
const merged = mergeProperties(sub);
|
||||||
|
properties = { ...properties, ...merged.properties };
|
||||||
|
required = [...required, ...merged.required];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (schema.properties) {
|
||||||
|
properties = { ...properties, ...schema.properties };
|
||||||
|
}
|
||||||
|
if (schema.required) {
|
||||||
|
required = [...required, ...schema.required];
|
||||||
|
}
|
||||||
|
return { properties, required };
|
||||||
|
}
|
||||||
|
|
||||||
function parseSchemaFields(
|
function parseSchemaFields(
|
||||||
schema: any,
|
schema: any,
|
||||||
resourceName: string,
|
resourceName: string,
|
||||||
@@ -43,12 +63,19 @@ function parseSchemaFields(
|
|||||||
configuration: Record<string, any> = {}
|
configuration: Record<string, any> = {}
|
||||||
): Record<string, ResourceField> {
|
): Record<string, ResourceField> {
|
||||||
const fields: Record<string, ResourceField> = {};
|
const fields: Record<string, ResourceField> = {};
|
||||||
const properties = schema.properties || {};
|
const { properties, required } = mergeProperties(schema);
|
||||||
const required = schema.required || [];
|
|
||||||
const overrides = configuration[resourceName]?.fields || {};
|
const overrides = configuration[resourceName]?.fields || {};
|
||||||
|
|
||||||
for (const [key, prop] of Object.entries(properties) as [string, any]) {
|
for (const [key, prop] of Object.entries(properties) as [string, any]) {
|
||||||
const type = mapOpenApiType(prop);
|
// Resolve oneOf/anyOf by merging all branch properties
|
||||||
|
let resolvedProp = prop;
|
||||||
|
if (prop.oneOf || prop.anyOf) {
|
||||||
|
const branches = prop.oneOf || prop.anyOf;
|
||||||
|
const merged = mergeProperties({ allOf: branches });
|
||||||
|
resolvedProp = { ...prop, type: 'object', properties: merged.properties, required: merged.required };
|
||||||
|
}
|
||||||
|
|
||||||
|
const type = mapOpenApiType(resolvedProp);
|
||||||
const override = overrides[key];
|
const override = overrides[key];
|
||||||
|
|
||||||
// Explicitly skip 'id' as it's the primary key and handled elsewhere
|
// Explicitly skip 'id' as it's the primary key and handled elsewhere
|
||||||
@@ -57,12 +84,12 @@ function parseSchemaFields(
|
|||||||
fields[key] = {
|
fields[key] = {
|
||||||
type,
|
type,
|
||||||
label:
|
label:
|
||||||
prop.title ||
|
resolvedProp.title ||
|
||||||
key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, " "),
|
key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, " "),
|
||||||
required: required.includes(key),
|
required: required.includes(key),
|
||||||
options: prop.enum,
|
options: resolvedProp.enum,
|
||||||
readOnly:
|
readOnly:
|
||||||
prop.readOnly ||
|
resolvedProp.readOnly ||
|
||||||
key === "created_at" ||
|
key === "created_at" ||
|
||||||
key === "updated_at",
|
key === "updated_at",
|
||||||
...override,
|
...override,
|
||||||
@@ -71,9 +98,9 @@ function parseSchemaFields(
|
|||||||
// STRICT RELATION DETECTION
|
// STRICT RELATION DETECTION
|
||||||
// A field is a relation ONLY if its schema object (or items schema)
|
// A field is a relation ONLY if its schema object (or items schema)
|
||||||
// exactly matches a schema that is defined as a resource.
|
// exactly matches a schema that is defined as a resource.
|
||||||
let targetSchema = prop;
|
let targetSchema = resolvedProp;
|
||||||
if (type === "array" && prop.items) {
|
if (type === "array" && resolvedProp.items) {
|
||||||
targetSchema = prop.items;
|
targetSchema = resolvedProp.items;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this schema object is registered as a resource
|
// Check if this schema object is registered as a resource
|
||||||
@@ -98,8 +125,8 @@ function parseSchemaFields(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Recursively parse nested objects (only if not a relation)
|
// Recursively parse nested objects (only if not a relation)
|
||||||
if (fields[key].type === "object" && prop.properties && !relation) {
|
if (fields[key].type === "object" && resolvedProp.properties && !relation) {
|
||||||
fields[key].schema = parseSchemaFields(prop, resourceName, schemaToResourceMap, configuration);
|
fields[key].schema = parseSchemaFields(resolvedProp, resourceName, schemaToResourceMap, configuration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +214,16 @@ export async function loadConfigFromOpenApi(baseUrl: string, configuration: Reco
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collect standalone enum schemas (e.g. FetchRequestStatus, AccountType, etc.)
|
||||||
|
const enums: Record<string, string[]> = {};
|
||||||
|
if (api.components?.schemas) {
|
||||||
|
for (const [name, schema] of Object.entries(api.components.schemas) as [string, any]) {
|
||||||
|
if (schema.enum) {
|
||||||
|
enums[name] = schema.enum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const serverBaseUrl = import.meta.env.VITE_API_BASE_URL || (api.servers?.[0]?.url ?? "")
|
const serverBaseUrl = import.meta.env.VITE_API_BASE_URL || (api.servers?.[0]?.url ?? "")
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -195,6 +232,7 @@ export async function loadConfigFromOpenApi(baseUrl: string, configuration: Reco
|
|||||||
baseUrl: serverBaseUrl,
|
baseUrl: serverBaseUrl,
|
||||||
authBaseUrl: authBaseUrl,
|
authBaseUrl: authBaseUrl,
|
||||||
resources,
|
resources,
|
||||||
|
enums,
|
||||||
profile: profileConfiguration,
|
profile: profileConfiguration,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ export function resolveTemplate(template: string, item: any): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] {
|
export function getFieldOptions(field: ResourceField, relationData?: any[]): SelectOption[] {
|
||||||
if (field.type === 'enum' && field.options) {
|
if (field.type === 'enum') {
|
||||||
return field.options.map(opt => ({
|
return (field.options ?? []).map(opt => ({
|
||||||
key: opt,
|
key: opt,
|
||||||
value: field.enumLabels?.[opt] ?? opt,
|
value: field.enumLabels?.[opt] ?? opt,
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ export default function FetchRequests() {
|
|||||||
|
|
||||||
const config = useConfig();
|
const config = useConfig();
|
||||||
const fetchRes = config?.resources.find((r: any) => r.name === "fetch-requests");
|
const fetchRes = config?.resources.find((r: any) => r.name === "fetch-requests");
|
||||||
const formatOptions: string[] = (fetchRes?.fields?.source?.schema?.format?.options as string[]) ?? ["axis", "icici"];
|
const formatOptions: string[] = fetchRes?.fields?.source?.schema?.format?.options as string[] ?? [];
|
||||||
|
|
||||||
const createMutation = useCreateFetchRequest();
|
const createMutation = useCreateFetchRequest();
|
||||||
const updateMutation = useUpdateFetchRequest();
|
const updateMutation = useUpdateFetchRequest();
|
||||||
@@ -345,7 +345,7 @@ export default function FetchRequests() {
|
|||||||
input={<OutlinedInput label="Status" />}
|
input={<OutlinedInput label="Status" />}
|
||||||
renderValue={(selected) => (selected as string[]).join(", ")}
|
renderValue={(selected) => (selected as string[]).join(", ")}
|
||||||
>
|
>
|
||||||
{["pending", "processing", "paused", "raw_expenses_done", "enriched_done", "completed", "failed"].map((s) => (
|
{(config?.enums?.FetchRequestStatus ?? []).map((s: string) => (
|
||||||
<MenuItem key={s} value={s}>{s.replace(/_/g, " ")}</MenuItem>
|
<MenuItem key={s} value={s}>{s.replace(/_/g, " ")}</MenuItem>
|
||||||
))}
|
))}
|
||||||
</Select>
|
</Select>
|
||||||
|
|||||||
Reference in New Issue
Block a user