File

src/app/shared/widgets/apps/my-shop-md/shop-category/shop-category.service.ts

Index

Properties

Properties

_embedded
_embedded: literal type
Type : literal type
_links
_links: literal type
Type : literal type
Optional
import { tap, map } from "rxjs/operators";
import { Injectable } from "@angular/core";
import { Observable, ReplaySubject } from "rxjs";
import { HttpClient } from "@angular/common/http";
import { ShopCategory } from "./shop-category";
import { ProgressbarService } from "../../../../components/progressbar/progressbar.service";
import { Link } from "../../../../components/hal/hal";

interface ShopCategoryContext {
  results: ReplaySubject<any>;
  dimensionUri?: string;
}

export interface ShopCategoryResource {
  _embedded: {
    categories: ShopCategory[];
  };
  _links?: {
    children: Link;
    [idx: string]: Link | Link[];
  };
}

@Injectable()
export class ShopCategoryService {
  private _selectedCategory: ShopCategory;

  private contexts: { [key: string]: ShopCategoryContext } = {};
  constructor(
    public authHttp: HttpClient,
    public _progressbarService: ProgressbarService
  ) {}

  public getOrderedResults(identifier: string): ReplaySubject<any> {
    return this.getContext(identifier).results;
  }

  private getContext(identifier): ShopCategoryContext {
    if (this.contexts[identifier]) {
      return this.contexts[identifier];
    }
    const context = {
      results: new ReplaySubject<any>(),
    };
    this.contexts[identifier] = context;
    return context;
  }

  // Clears the map of subjects. Should get called whenever an app using shop-categories is disposed
  public cleanSubjects() {
    this.contexts = {};
  }

  public setOrderedResults(identifier, data) {
    this.getContext(identifier).results.next(data);
  }

  public getCurrentDimensionUri(identifier): string {
    return this.getContext(identifier).dimensionUri;
  }

  public setCurrentDimensionUri(identifier, data) {
    this.getContext(identifier).dimensionUri = data;
  }

  public loadOrderedResults(inputurl) {
    this._progressbarService.addRequest();
    return this.authHttp.get(inputurl, {}).pipe(
      tap(
        (data) => this._progressbarService.requestFinished(),
        (err) => {
          this._progressbarService.requestFinished();
        }
      )
    );
  }

  getRootCategories(url): Observable<ShopCategoryResource> {
    this._progressbarService.addRequest();
    return this.authHttp.get(url).pipe(
      map((res) => <any>res),
      tap(
        (data) => this._progressbarService.requestFinished(),
        (err) => this._progressbarService.requestFinished()
      )
    );
  }
  getCategories(childUrl: string): Observable<ShopCategoryResource> {
    this._progressbarService.addRequest();
    return this.authHttp.get(childUrl).pipe(
      map((res) => <any>res),
      tap(
        (data) => this._progressbarService.requestFinished(),
        (err) => this._progressbarService.requestFinished()
      )
    );
  }
}

results matching ""

    No results matching ""