Makes finally dynamic component work??

This commit is contained in:
2023-08-11 17:04:39 +02:00
parent 09304b9a95
commit 9903497867
6 changed files with 69 additions and 52 deletions

View File

@@ -21,13 +21,13 @@ export interface ServiceConfig extends SectionConfig {
}
export interface ServiceGroupConfig extends SectionConfig {
items: ServiceConfig[];
items: Record<string, ServiceConfig>;
[x: string]: unknown;
}
export interface Config extends SectionConfig {
services: ServiceGroupConfig[];
services: Record<string, ServiceGroupConfig>;
[x: string]: unknown;
}
@@ -41,17 +41,17 @@ type DeepRequired<T> = T extends object
const requiredConfig: DeepRequired<Config> = {
title: '',
subtitle: '',
services: [
{
services: {
default: {
title: '',
items: [
{
items: {
default: {
title: '',
url: ''
}
]
}
}
]
}
};
export function mergeConfig(a: Config, b: any): Config {
@@ -61,7 +61,7 @@ export function mergeConfig(a: Config, b: any): Config {
export const defaultConfig: Config = {
title: 'Flanders',
services: []
services: {}
};
type SPOJO = Record<string, unknown>;
@@ -70,29 +70,21 @@ function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
const res: Type = { ...toStrip };
const referenceNames = Object.entries(reference).map(([key, value]) => key);
const allowAny: boolean = referenceNames.length == 1 && referenceNames[0] == 'default';
for (const [key, value] of Object.entries(res)) {
if (referenceNames.includes(key) == false) {
if (referenceNames.includes(key) == false && allowAny == false) {
// remove the object
delete res[key];
continue;
}
// strips further arrays
if (value instanceof Array) {
const stripped: SPOJO = {};
const childRef = (reference[key] as Array<SPOJO>)[0];
stripped[key] = value.map((v: SPOJO) => strip(v, childRef));
Object.assign(res, stripped);
continue;
}
if (typeof value != 'object') {
continue;
}
// it is a child object, we strip it further
const stripped: SPOJO = {};
stripped[key] = strip(value as SPOJO, reference[key] as SPOJO);
const childReference: SPOJO = reference[allowAny ? 'default' : key] as SPOJO;
stripped[key] = strip(value as SPOJO, childReference);
Object.assign(res, stripped);
}
return res;