my-test123
Version:
A planner front-end for Fabric8.
158 lines • 5.88 kB
JavaScript
import { Observable } from 'rxjs/Observable';
import { GlobalSettings } from '../shared/globals';
import { Injectable } from '@angular/core';
import { Logger } from 'ngx-base';
import { AuthenticationService } from 'ngx-login-client';
import { HttpService } from './http-service';
import { Spaces } from 'ngx-fabric8-wit';
var AreaService = /** @class */ (function () {
function AreaService(logger, http, auth, globalSettings, spaces) {
var _this = this;
this.logger = logger;
this.http = http;
this.auth = auth;
this.globalSettings = globalSettings;
this.spaces = spaces;
this.areas = [];
this.headers = new Headers({ 'Content-Type': 'application/json' });
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)
.map(function (response) {
if (/^[5, 4][0-9]/.test(response.status.toString())) {
throw new Error('API error occured');
}
return response.json().data;
})
.map(function (data) {
/*
//Need to fix the Area data and service for inmemory mode
//If the area has a parent, append it to the area's name
data.forEach((area) => {
if (area.attributes.parent_path_resolved !== '/'){
area.attributes.name = (area.attributes.parent_path_resolved).substring(1) + '/' + area.attributes.name;
}
});
*/
_this.areas = data;
return _this.areas;
})
.catch(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 Observable.of([]);
}
}
else {
return Observable.throw('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)
.map(function (response) {
if (/^[5, 4][0-9]/.test(response.status.toString())) {
throw new Error('API error occured');
}
return response.json().data;
})
.map(function (data) {
_this.areas = data;
return _this.areas;
})
.catch(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 Observable.of([]);
}
};
AreaService.prototype.getArea = function (area) {
if (Object.keys(area).length) {
var areaLink = area.data.links.self;
return this.http.get(areaLink)
.map(function (arearesp) { return arearesp.json().data; });
}
else {
return Observable.of(area);
}
};
AreaService.prototype.getAreaById = function (areaId) {
return this.getAreas().first()
.map(function (resultAreas) {
for (var i = 0; i < resultAreas.length; i++) {
if (resultAreas[i].id === areaId)
return resultAreas[i];
}
})
.catch(function (err) {
return Observable.throw(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: HttpService, },
{ type: AuthenticationService, },
{ type: GlobalSettings, },
{ type: Spaces, },
]; };
return AreaService;
}());
export { AreaService };
//# sourceMappingURL=area.service.js.map