loop-modules
Version:
Shared modules for the Loop product suite.
148 lines (147 loc) • 6.62 kB
JavaScript
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';
import { Observable } from 'rxjs/Observable';
// app
import { QueryAction, SetEntriesAction, UnreadAction } from '../actions/loop-assignment.action';
import { APIDispatcher } from '../utils/index';
var LoopAssignmentService = (function (_super) {
__extends(LoopAssignmentService, _super);
function LoopAssignmentService(http, store) {
var _this = _super.call(this, http, store) || this;
_this.http = http;
_this.store = store;
return _this;
}
LoopAssignmentService.prototype.fetch = function (identities, page, size, options) {
var _this = this;
if (page === void 0) { page = 0; }
if (size === void 0) { size = 10; }
return Observable.create(function (observer) {
_this.post('ma_loop/api/v1/assignments/fetch', identities, page, size, options).map(function (res) { return res.json(); })
.catch(_this.handleError)
.subscribe(function (response) {
_this.loaded = true;
_this.lastRequest = response;
_this.lastRequest.payload = identities;
_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();
});
});
};
LoopAssignmentService.prototype.fetchForUsers = function (userIdentities, page, size, options, forStore) {
var _this = this;
if (page === void 0) { page = 0; }
if (size === void 0) { size = 10; }
if (forStore === void 0) { forStore = true; }
return Observable.create(function (observer) {
_this.post('ma_loop/api/v1/assignments/users', userIdentities, page, size, options).map(function (res) { return res.json(); })
.catch(_this.handleError)
.subscribe(function (response) {
if (forStore) {
_this.loaded = true;
_this.lastRequest = response;
_this.lastRequest.payload = userIdentities;
_this.lastRequest.currentPage = page;
_this.lastRequest.requestSize = size;
_this.lastRequest.options = options;
_this.store.dispatch(new QueryAction(response.content));
}
observer.next(response);
observer.complete();
});
});
};
LoopAssignmentService.prototype.checkUnread = function (userIdentities) {
var _this = this;
return Observable.create(function (observer) {
_this.post('ma_loop/api/v1/assignments/users', userIdentities, 0, 1, {
unread: 'unread',
sortBy: 'due_at'
})
.map(function (res) { return res.json(); })
.catch(_this.handleError)
.subscribe(function (response) {
_this.store.dispatch(new UnreadAction(response.totalElements));
observer.next(response);
observer.complete();
});
});
};
LoopAssignmentService.prototype.create = function (payload) {
return this.http.post('ma_loop/api/v1/assignments', payload)
.catch(this.handleError);
};
LoopAssignmentService.prototype.update = function (payload) {
return this.http.put('ma_loop/api/v1/assignments', payload)
.catch(this.handleError);
};
LoopAssignmentService.prototype.submit = function (identity, payload) {
return this.http.post('ma_loop/api/v1/assignment_submission/' + identity, payload)
.catch(this.handleError);
};
/**
* Resets the entries in the app-state slice for Loop Assignments
*
*
* @memberOf LoopAssignmentService
*/
LoopAssignmentService.prototype.resetEntries = function () {
this.loaded = false;
this.lastRequest = undefined;
this.store.dispatch(new SetEntriesAction([]));
};
/**
* Fetches the next available page for assignments, if any
*/
LoopAssignmentService.prototype.getNextFetchPage = function () {
var _this = this;
if (this.hasNext && !this.fetchingNext) {
this.fetchingNext = true;
this.fetch(this.lastRequest.payload, this.lastRequest.currentPage + 1, this.lastRequest.requestSize, this.lastRequest.options).subscribe(function () {
_this.fetchingNext = false;
});
}
};
/**
* Fetches the next available page for assignments, if any
*/
LoopAssignmentService.prototype.getNextFetchUsersPage = function () {
var _this = this;
if (this.hasNext && !this.fetchingNext) {
this.fetchingNext = true;
this.fetchForUsers(this.lastRequest.payload, this.lastRequest.currentPage + 1, this.lastRequest.requestSize, this.lastRequest.options).subscribe(function () {
_this.fetchingNext = false;
});
}
};
return LoopAssignmentService;
}(APIDispatcher));
LoopAssignmentService = __decorate([
Injectable(),
__metadata("design:paramtypes", [Http, Store])
], LoopAssignmentService);
export { LoopAssignmentService };