fabric8-planner
Version:
A planner front-end for Fabric8.
132 lines • 4.75 kB
JavaScript
import { of as ObservableOf, throwError } from 'rxjs';
import { catchError, first, map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Logger } from 'ngx-base';
import { Spaces } from 'ngx-fabric8-wit';
import { HttpClientService } from '../shared/http-module/http.service';
var AreaService = /** @class */ (function () {
function AreaService(logger, http, spaces) {
var _this = this;
this.logger = logger;
this.http = http;
this.spaces = spaces;
this.areas = [];
this.spaces.current.subscribe(function (val) { return _this._currentSpace = val; });
}
/**
* getAreas - We call this service method to fetch
* @param areaUrl - The url to get all the areas
* @return Promise of AreaModel[] - Array of areas
*/
AreaService.prototype.getAreas = function () {
var _this = this;
// get the current iteration url from the space service
if (this._currentSpace) {
var areasUrl = this._currentSpace.relationships.areas.links.related;
if (this.checkValidUrl(areasUrl)) {
return this.http
.get(areasUrl)
.pipe(map(function (response) {
return response.data;
}), map(function (data) {
_this.areas = data;
return _this.areas;
}), catchError(function (error) {
if (error.status === 401) {
//this.auth.logout(true);
}
else {
console.log('Fetch area API returned some error - ', error.message);
return Promise.reject([]);
}
}));
}
else {
this.logger.log('URL not matched');
return ObservableOf([]);
}
}
else {
return throwError('nospace');
}
};
/**
* getAreas - We call this service method to fetch
* @param areaUrl - The url to get all the areas
* @return Observable of AreaModel[] - Array of areas
*/
AreaService.prototype.getAreas2 = function (areaUrl) {
var _this = this;
if (this.checkValidUrl(areaUrl)) {
return this.http
.get(areaUrl)
.pipe(map(function (response) {
return response.data;
}), map(function (data) {
_this.areas = data;
return _this.areas;
}), catchError(function (error) {
if (error.status === 401) {
//this.auth.logout(true);
}
else {
console.log('Fetch area API returned some error - ', error.message);
return Promise.reject([]);
}
}));
}
else {
this.logger.log('URL not matched');
return ObservableOf([]);
}
};
AreaService.prototype.getArea = function (area) {
if (Object.keys(area).length) {
var areaLink = area.data.links.self;
return this.http.get(areaLink)
.pipe(map(function (arearesp) { return arearesp.data; }));
}
else {
return ObservableOf(area);
}
};
AreaService.prototype.getAreaById = function (areaId) {
return this.getAreas().pipe(first(), map(function (resultAreas) {
for (var i = 0; i < resultAreas.length; i++) {
if (resultAreas[i].id === areaId) {
return resultAreas[i];
}
}
}), catchError(function (err) {
return throwError(new Error(err.message));
}));
};
/**
* checkValidUrl checks if the API url for
* iterations is valid or not
* sample url -
* http://localhost:8080/api/spaces/d7d98b45-415a-4cfc-add2-ec7b7aee7dd5/areas
*
* @param URL
* @return Boolean
*/
AreaService.prototype.checkValidUrl = function (url) {
var urlArr = url.split('/');
var uuidRegExpPattern = new RegExp('[^/]+');
return (urlArr[urlArr.length - 1] === 'areas' &&
uuidRegExpPattern.test(urlArr[urlArr.length - 2]) &&
urlArr[urlArr.length - 3] === 'spaces');
};
AreaService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
AreaService.ctorParameters = function () { return [
{ type: Logger, },
{ type: HttpClientService, },
{ type: Spaces, },
]; };
return AreaService;
}());
export { AreaService };
//# sourceMappingURL=area.service.js.map