File

libs/common/web-components/src/back-link-adapter/back-link-aem-adapter.service.ts

Index

Properties

Properties

backLinkConfig
Type string
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { WEB_COMPONENT_ID } from '../utils/tokens';
import { BackLinkConfigElement, BackLinkConfiguration } from './back-link-adapter.model';
import { BackLinkAdapterService } from './back-link-adapter.service';

interface RootCustomConfiguration {
  backLinkConfig: string;
}

@Injectable()
export class BackLinkAemAdapterService extends BackLinkAdapterService {
  private backLinkConfig?: BackLinkConfiguration;
  private readonly rootElemAttribute = 'customconfiguration';
  backLinkData: BackLinkConfigElement | undefined;

  constructor(
    @Inject(WEB_COMPONENT_ID) private readonly webComponentId: string,
    @Inject(DOCUMENT) private readonly document: Document
  ) {
    super();
    this.backLinkData = this.extractBackLinkData();
  }

  private extractBackLinkConfig(): BackLinkConfiguration | undefined {
    let config: RootCustomConfiguration | undefined;
    let parsedBackLinkConfig: BackLinkConfiguration | undefined;

    const rootElem: Element | null = this.document.querySelector(this.webComponentId);
    if (!rootElem) return;

    const configString: string | null = rootElem.getAttribute(this.rootElemAttribute);
    if (!configString) return;

    try {
      config = JSON.parse(configString);
      if (!config) return;
    } catch (error) {
      throw Error(
        `Error while parsing the content from the "${this.rootElemAttribute}" attribute in the "${this.webComponentId}" element\n${error}`
      );
    }

    try {
      const backLinkConfig: string | undefined = config.backLinkConfig;
      if (!backLinkConfig) return;

      parsedBackLinkConfig = JSON.parse(backLinkConfig);
    } catch (error) {
      throw Error(
        `Error while parsing the content from the "backLinkConfig" field in "${this.rootElemAttribute}". The back link configuration doesn't have the proper format\n${error}`
      );
    }

    return parsedBackLinkConfig;
  }

  private getDefaultData(): BackLinkConfigElement | undefined {
    let backLinkData: BackLinkConfigElement | undefined;

    if (this.backLinkConfig?.default?.path) {
      backLinkData = this.backLinkConfig.default;
    }

    return backLinkData;
  }

  private extractBackLinkData(): BackLinkConfigElement | undefined {
    this.backLinkConfig = this.extractBackLinkConfig();

    if (!this.backLinkConfig) return;

    const referrer = this.document.referrer;

    if (!referrer) {
      return this.getDefaultData();
    }

    const configItem = this.backLinkConfig.backLinks?.find((backLink: BackLinkConfigElement) =>
      referrer.includes(backLink.path)
    );

    if (!configItem) {
      return this.getDefaultData();
    }

    const backLinkData = {
      path: this.document.referrer,
      stageLinkLabel: configItem.stageLinkLabel,
      nextButtonLabel: configItem.nextButtonLabel
    };

    return backLinkData;
  }
}

results matching ""

    No results matching ""