File

libs/nx/src/generators/journey-src/lib/create-pages/single-page/example-state.ts

Index

Properties

Properties

example
id
Type string
import { BuildingBlockConfiguration } from '@allianz/taly-core/schemas';
import { camelize } from '@nx/devkit/src/utils/string-utils';

const VARIABLE_IDENTIFIER_SUFFIX = 'ExampleState';

export interface BuildingBlockWithExample {
  id: string;
  example: unknown;
}
export type BuildingBlockWithExampleMap = Map<string, BuildingBlockWithExample>;

/**
 * Take a collection of Building Block IDs and example state strings
 * and render them as a list of variable assignments to
 * output it directly into a component template.
 *
 * const myBbExampleState = {foo: "bar"}
 */
export function renderAllVariableDeclarations(items: BuildingBlockWithExampleMap) {
  const result = Array.from(items.values()).map(renderVariableDeclaration);
  return result.join('\n');
}

function renderVariableDeclaration(item: BuildingBlockWithExample) {
  const identifier = createIdentifier(item.id);
  const serializedValue = JSON.stringify(item.example, null, 4);
  return `
// EXAMPLE for Building Block '${item.id}'
// tslint:disable
${identifier} = ${serializedValue};`;
}

/**
 * Find and collect all Building Blocks with a given example state
 * and store it inside a map to further access.
 */
export function collectFromBuildingBlocks(
  blocks: Pick<BuildingBlockConfiguration, 'exampleState' | 'id'>[]
): BuildingBlockWithExampleMap {
  return blocks.reduce((accu, item) => {
    if (item.exampleState) {
      accu.set(item.id, {
        id: item.id,
        example: item.exampleState
      });
    }

    return accu;
  }, new Map<string, BuildingBlockWithExample>());
}

export function createIdentifier(buildingBlockId: string) {
  return `${camelize(buildingBlockId)}${VARIABLE_IDENTIFIER_SUFFIX}`;
}

results matching ""

    No results matching ""