loop-modules
Version:
Shared modules for the Loop product suite.
81 lines (68 loc) • 2.86 kB
text/typescript
// angular
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
// libs
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Store } from '@ngrx/store';
// app
import { QueryAction, SetEntriesAction } from '../actions/loop-session.action';
import { APIDispatcher, APIRequestOptions, ResponseBody } from '../utils/index';
import { LoopSessionPayload } from '../interfaces/loop-session-payload.interface';
import { LoopSessionUpdatePayload } from '../interfaces/loop-session-update-payload.interface';
export class LoopSessionService extends APIDispatcher {
constructor(public http: Http, public store: Store<any>) {
super(http, store);
}
find(identity: string): any {
return this.http.get('/ma_loop/api/v3/sessions/' + identity).map(res => res.json()).catch(this.handleError);
}
findDocumentActions(identity: string): any {
return this.http.get('/ma_loop/api/v1/session/' + identity).map(res => res.json()).catch(this.handleError);
}
ratings(identity: string): any {
return this.http.get('/ma_loop/api/v3/sessions/' + identity + '/ratings').map(res => res.json()).catch(this.handleError);
}
query(page: number = 0, size: number = 10, options: APIRequestOptions = { sortBy: 'created_at', sortDir: 'desc' }) {
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/sessions/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));
});
}
getNextPage() {
if(this.hasNext) {
this.query(this.lastRequest.currentPage + 1, this.lastRequest.requestSize, this.lastRequest.options);
}
}
save(payload: LoopSessionPayload): any {
return this.http.post('/ma_loop/api/v3/sessions/', payload);
}
update(payload: LoopSessionUpdatePayload): any{
return this.http.put('/ma_loop/api/v1/session/', { payload: payload });
}
/**
* Resets the entries in the app-state slice for Loop Sessions
*
*
* @memberOf LoopSessionService
*/
resetEntries() {
this.loaded = false;
this.lastRequest = undefined;
this.store.dispatch(new SetEntriesAction([]));
}
}