File
results
|
results: ReplaySubject<any>
|
Type : ReplaySubject<any>
|
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()
)
);
}
}