File

libs/core/src/lib/services/building-block-meta.service.ts

Description

Service to access any given meta data for a Building Block page injected by BUILDING_BLOCK_META_DATA during the generation of a page. The service is used by Building Block Pages and needs access to the page-local BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance. Working alternative would be injecting it directly into each of the page modules but this lazy approach supports tree shaking way better.

Index

Methods
Accessors

Constructor

constructor()

Methods

getBusinessEvents
getBusinessEvents(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
getData
getData(buildingBlockId: string, key)
Parameters :
Name Type Optional
buildingBlockId string No
key No
Returns : Record<string, BuildingBlockMetaData>
getNavigationData
getNavigationData(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
getResources
getResources(buildingBlockId: string)
Parameters :
Name Type Optional
buildingBlockId string No
Returns : Record<string, BuildingBlockMetaData>
hasData
hasData(buildingBlockId: string, key)
Parameters :
Name Type Optional
buildingBlockId string No
key No
Returns : boolean
hasMetaData
hasMetaData(id: string)
Parameters :
Name Type Optional
id string No
Returns : any
hasResources
hasResources(id: string)
Parameters :
Name Type Optional
id string No
Returns : boolean

Accessors

metaData
getmetaData()
import { Injectable, inject } from '@angular/core';
import { BUILDING_BLOCK_NAVIGATION_TYPE } from '../building-block/navigation';
import { type ValidationConfigItem } from '../form-support/validation.model';
import { BUILDING_BLOCK_META_DATA } from '../tokens';
import { BuildingBlockMetaServiceInterface } from './building-block-meta-service-interface';
import { type BusinessEventConfig } from './taly-business-event.model';

export type BuildingBlockPageMetaData = Record<string, BuildingBlockMetaData>;

export interface BuildingBlockMetaData {
  resources: unknown;
  businessEvents?: Record<string, string>;
  navigation?: Record<string, string>;
  validators?: ValidationConfigItem[];
}

/**
 * Service to access any given meta data for a Building Block page
 * injected by BUILDING_BLOCK_META_DATA during the generation of a page.
 * The service is used by Building Block Pages and needs access to the page-local
 * BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance.
 * Working alternative would be injecting it directly into each of the page modules but this lazy approach
 * supports tree shaking way better.
 */
@Injectable({
  providedIn: 'any'
})
export class BuildingBlockMetaService implements BuildingBlockMetaServiceInterface {
  private _metaData: BuildingBlockPageMetaData;

  constructor() {
    const metaData = inject<BuildingBlockPageMetaData>(BUILDING_BLOCK_META_DATA, {
      optional: true
    });

    this._metaData = metaData ?? {};
  }

  get metaData() {
    return this._metaData;
  }

  getData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
    if (!this.hasMetaData(buildingBlockId)) {
      throw new Error(`Could not find key ${buildingBlockId} in metadata list`);
    }
    return this.metaData[buildingBlockId][key];
  }

  hasData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
    return this.hasMetaData(buildingBlockId) && !!this.metaData[buildingBlockId][key];
  }

  getResources(buildingBlockId: string) {
    return this.getData(buildingBlockId, 'resources');
  }

  getBusinessEvents(buildingBlockId: string) {
    return this.getData(buildingBlockId, 'businessEvents') as Record<string, BusinessEventConfig>;
  }

  getNavigationData(buildingBlockId: string) {
    return (this.getData(buildingBlockId, 'navigation') ?? {}) as Record<
      string,
      BUILDING_BLOCK_NAVIGATION_TYPE
    >;
  }

  hasResources(id: string) {
    return this.hasMetaData(id) && !!this.metaData[id].resources;
  }

  hasMetaData(id: string) {
    return Object.keys(this.metaData).includes(id);
  }
}

results matching ""

    No results matching ""