libs/core/runtime-utils/src/get-data-for-page.ts
[buildingBlockId: string]:
|
import {
BannerBlockConfiguration,
BuildingBlockConfiguration,
PageConfigurationWithTransformedDynamicForms,
OverarchingDetailsBlockConfiguration
} from '@allianz/taly-core/schemas';
import { ValidationRule } from '@allianz/taly-core';
// eslint-disable-next-line @nx/enforce-module-boundaries
import { PageInJourneyExampleData } from '@allianz/taly-core/showroom';
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
export interface PageConfigOverrides {
[buildingBlockId: string]: {
validators?: ValidationRule[];
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
resources?: { [key: string]: unknown };
};
}
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
export interface DataForPage {
[buildingBlockId: string]: BuildingBlockConfiguration;
}
/**
* Utility function to be used in a generated page to extract the meta data matching the given page id.
* @param {PartialPageConfig} pagesConfig the pages configuration
* @param {string} id the page id to get the data for
* @param {PageConfigOverrides} pageConfigOverrides additional page-specific data that overrides the page config
* @returns {DataForPage} Building Block data for a specific page, or empty object if no data could be found.
*/
export const getDataForPage = (
pagesConfig: PageConfigurationWithTransformedDynamicForms[],
id: string,
pageConfigOverrides: PageConfigOverrides = {},
showroomDataForPage?: PageInJourneyExampleData
): DataForPage => {
const page = pagesConfig.find((item) => item.id === id);
if (!page) {
console.warn(`Could not find data for page '${id}'`);
return {};
}
const blockData = getAllBlocks(page);
if (!blockData || blockData.length === 0) {
console.warn(
`Page '${id}' seems to be empty. Please add Building Blocks to this page or remove it.`
);
return {};
}
const blockMap = blockData
.map((block) => {
const blockOverrides = pageConfigOverrides[block.id] ?? {};
return { ...block, ...blockOverrides };
})
.map((block) => {
const blockShowroomResources = showroomDataForPage?.[block.id]?.resources;
if (!block.resources && blockShowroomResources) {
return { ...block, resources: blockShowroomResources };
}
return block;
})
.reduce((collection, item) => {
return {
...collection,
[item.id]: item
};
}, {});
return blockMap;
};
function getAllBlocks(page: PageConfigurationWithTransformedDynamicForms) {
const result: (
| BuildingBlockConfiguration
| OverarchingDetailsBlockConfiguration
| BannerBlockConfiguration
)[] = [];
result.push(...(page.blocks ?? []));
if (page.overarchingDetailsBlock) {
result.push(page.overarchingDetailsBlock);
}
if (page.bannerBlock) {
result.push(page.bannerBlock);
}
return result;
}