UNPKG

loop-modules

Version:

Shared modules for the Loop product suite.

111 lines (100 loc) 3.73 kB
// angular import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; // libs import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; // libs import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable'; // app import { APIDispatcher, APIRequestOptions, ResponseBody } from '../utils/index'; import { QueryAction, SetEntriesAction, SetSelectedAction } from '../actions/loop-document.action'; import { LoopDocumentState } from '../states/loop-document.state'; @Injectable() export class LoopDocumentService extends APIDispatcher { constructor(public http: Http, public store: Store<LoopDocumentState>) { super(http, store); } /** * Fetchines a collection of LoopDocument records * * @param {number} [page=0] The page of the results to request * @param {number} [size=10] The size of each page to request * @param {string} The optional search parameter to filter by */ query(page: number = 0, size: number = 10, options: APIRequestOptions = {}): Observable<any> { return Observable.create((observer: any) => { if(this.lastRequest) { if(this.lastRequest.options !== options) { this.lastRequest.currentPage = page; this.lastRequest.requestSize = size; this.lastRequest.options = options; } } this.get('ma_loop/api/v3/content/fetch', page, size, options).map(res => res.json()) .catch(this.handleError) .subscribe((response: ResponseBody) => { this.loaded = true; this.lastRequest = response; this.lastRequest.currentPage = page; this.lastRequest.requestSize = size; this.lastRequest.options = options; this.store.dispatch(new QueryAction(response.content)); observer.next(response.content); observer.complete(); }); }); } /** * Fetches a collection of LoopDocument records based on a collection of identities * * @param {string[]} identities The LoopDocument identities to fetch records for */ fetch(identities: string[]): Observable<any> { return this.post('ma_loop/api/v3/content/fetch', identities) .map(res => res.json()) .catch(this.handleError); } fetchForStore(identities: string[]) { this.fetch(identities) .subscribe((response: ResponseBody) => { this.loaded = true; this.store.dispatch(new QueryAction(response.content)); }); } /** * Queries the next page from the back-end service, based on the previous request * * * @memberOf LoopDocumentService */ getNextPage() { if(this.hasNext && !this.fetchingNext) { this.fetchingNext = true; this.query(this.lastRequest.currentPage + 1, this.lastRequest.requestSize, this.lastRequest.options).subscribe(() => { this.fetchingNext = false; }); } } /** * Resets the entries in the app-state slice for Loop Documents * * * @memberOf LoopDocumentService */ resetEntries() { this.loaded = false; this.lastRequest = undefined; this.store.dispatch(new SetEntriesAction([])); } /** * Resets the selected entries in the app-state slice for Loop Documents * * * @memberOf LoopDocumentService */ resetSelectedEntries() { this.store.dispatch(new SetSelectedAction([])); } }