All files / src/app/core/facades cms.facade.ts

34.14% Statements 14/41
14.28% Branches 3/21
0% Functions 0/11
34.14% Lines 14/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10318x 18x 18x 18x     18x 18x 18x 18x 18x         18x 18x 18x 18x       18x                                                                                                                                                                
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, combineLatest } from 'rxjs';
import { delay, switchMap, tap } from 'rxjs/operators';
 
import { CallParameters } from 'ish-core/models/call-parameters/call-parameters.model';
import { CategoryHelper } from 'ish-core/models/category/category.helper';
import { getContentInclude, loadContentInclude } from 'ish-core/store/content/includes';
import { getCompleteContentPageTree, getContentPageTree, loadContentPageTree } from 'ish-core/store/content/page-tree';
import { getContentPagelet } from 'ish-core/store/content/pagelets';
import {
  getContentPageLoading,
  getSelectedContentPage,
  setBreadcrumbForContentPage,
} from 'ish-core/store/content/pages';
import { getParametersProductList, loadParametersProductListFilter } from 'ish-core/store/content/parameters';
import { getViewContext, loadViewContextEntrypoint } from 'ish-core/store/content/viewcontexts';
import { getPGID } from 'ish-core/store/customer/user';
import { whenTruthy } from 'ish-core/utils/operators';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
@Injectable({ providedIn: 'root' })
export class CMSFacade {
  constructor(private store: Store) {}
 
  contentPage$ = this.store.pipe(select(getSelectedContentPage));
  contentPageLoading$ = this.store.pipe(select(getContentPageLoading));
 
  contentInclude$(includeId$: Observable<string>) {
    return combineLatest([includeId$.pipe(whenTruthy()), this.store.pipe(select(getPGID))]).pipe(
      delay(0), // delay ensures the apiToken cookie is deleted before a cms request without a pgid is triggered
      tap(([includeId]) => this.store.dispatch(loadContentInclude({ includeId }))),
      switchMap(([includeId]) => this.store.pipe(select(getContentInclude(includeId)), whenTruthy()))
    );
  }
 
  pagelet$(id: string) {
    return this.store.pipe(select(getContentPagelet(id)));
  }
 
  viewContext$(viewContextId: string, callParameters: CallParameters) {
    this.store.dispatch(loadViewContextEntrypoint({ viewContextId, callParameters }));
    return this.store.pipe(select(getViewContext(viewContextId, callParameters)));
  }
 
  contentPageTree$(rootId: string, depth: number) {
    // fetch only the depth that is actually needed, depth=0 returns already the next child level
    this.store.dispatch(loadContentPageTree({ rootId, depth: depth > 0 ? depth - 1 : 0 }));
    return this.store.pipe(select(getContentPageTree(rootId)));
  }
 
  completeContentPageTree$(rootId: string, depth: number) {
    // fetch only the depth that is actually needed, depth=0 returns already the next child level
    this.store.dispatch(loadContentPageTree({ rootId, depth: depth > 0 ? depth - 1 : 0 }));
    return this.store.pipe(select(getCompleteContentPageTree(rootId, depth)));
  }
 
  /**
   *
   * @param rootId is taken into consideration as first element of breadcrumb for content page
   *
   * NOTE: use 'COMPLETE' as value of rootId to get complete available page path as breadcrumb
   */
  setBreadcrumbForContentPage(rootId: string): void {
    this.store.dispatch(setBreadcrumbForContentPage({ rootId }));
  }
 
  parameterProductListFilter$(categoryId?: string, productFilter?: string, scope?: string, amount?: number) {
    const listConfiguration = this.getProductListConfiguration(categoryId, productFilter, scope, amount);
    this.store.dispatch(
      loadParametersProductListFilter({
        id: listConfiguration.id,
        searchParameter: listConfiguration.searchParameter,
        amount,
      })
    );
    return this.store.pipe(select(getParametersProductList(listConfiguration.id)));
  }
 
  private getProductListConfiguration(
    categoryId?: string,
    productFilter?: string,
    scope?: string,
    amount?: number
  ): { id: string; searchParameter: URLFormParams } {
    let id = '';
    const searchParameter: URLFormParams = {};
 
    id = categoryId ? `${id}@${categoryId}` : id;
    id = productFilter ? `${id}@${productFilter}` : id;
    id = scope ? `${id}@${scope}` : id;
    id = amount ? `${id}@${amount}` : id;
 
    Iif (categoryId && scope !== 'GlobalScope') {
      searchParameter.category = [CategoryHelper.getCategoryPath(categoryId)];
    }
 
    searchParameter.productFilter = productFilter ? [productFilter] : ['fallback_searchquerydefinition'];
 
    return { id, searchParameter };
  }
}