UNPKG

loop-modules

Version:

Shared modules for the Loop product suite.

107 lines (106 loc) 4.49 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; // 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'; // app import { QueryAction, SetEntriesAction } from '../actions/loop-activity-message.action'; import { APIDispatcher } from '../utils/index'; var LoopActivityMessageService = (function (_super) { __extends(LoopActivityMessageService, _super); function LoopActivityMessageService(http, store) { var _this = _super.call(this, http, store) || this; _this.http = http; _this.store = store; _this.lastRequest = { currentPage: 0, requestSize: 10 }; return _this; } LoopActivityMessageService.prototype.query = function (page, size, options) { var _this = this; if (page === void 0) { page = 0; } if (size === void 0) { size = 10; } if (options === void 0) { options = { archived: false }; } this.get('ma_loop/api/v1/activity_message/fetch', page, size, options) .map(function (res) { return res.json(); }) .subscribe(function (response) { _this.loaded = true; _this.lastRequest = response; _this.lastRequest.currentPage = page; _this.lastRequest.requestSize = size; _this.store.dispatch(new QueryAction(response.content)); }); }; LoopActivityMessageService.prototype.acknowledge = function (message) { var _this = this; if (!message.acknowledged_at) { console.log('updating acknowledge'); this.http.put('ma_loop/api/v1/activity_message/' + message.recipient_identity, { payload: { acknowledgedAt: new Date().getTime() } }).subscribe(function (response) { _this.resetEntries(); _this.lastRequest = response; _this.lastRequest.currentPage = 1; _this.query(); }); } }; LoopActivityMessageService.prototype.archive = function (message) { if (!message.archived_at) { console.log('updating archive'); this.http.put('ma_loop/api/v1/activity_message/' + message.recipient_identity, { payload: { acknowledgedAt: message.acknowledged_at || new Date().getTime(), archivedAt: new Date().getTime() } }).subscribe(); } }; /** * Queries the next page from the back-end service, based on the previous request * * * @memberOf LoopActivityMessageService */ LoopActivityMessageService.prototype.getNextPage = function () { if (this.hasNext) { this.query(this.lastRequest.currentPage + 1, this.lastRequest.requestSize); } }; /** * Resets the entries in the app-state slice for LoopActivityMessage * * * @memberOf LoopActivityMessageService */ LoopActivityMessageService.prototype.resetEntries = function () { this.loaded = false; this.lastRequest = undefined; this.store.dispatch(new SetEntriesAction([])); }; return LoopActivityMessageService; }(APIDispatcher)); LoopActivityMessageService = __decorate([ Injectable(), __metadata("design:paramtypes", [Http, Store]) ], LoopActivityMessageService); export { LoopActivityMessageService };