fabric8-planner
Version:
A planner front-end for Fabric8.
146 lines • 5.78 kB
JavaScript
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
import { Injectable } from '@angular/core';
import { createFeatureSelector, createSelector, select, Store } from '@ngrx/store';
import { isEmpty } from 'lodash';
import { UserService as UserServiceClass } from 'ngx-login-client';
import { combineLatest, of as ObservableOf } from 'rxjs';
import { filter, map, startWith, switchMap, tap } from 'rxjs/operators';
import { Get as GetUserAction } from './../actions/user.actions';
import { switchModel } from './common.model';
var UserMapper = /** @class */ (function () {
function UserMapper() {
this.serviceToUiMapTree = [{
fromPath: ['id'],
toPath: ['id']
}, {
fromPath: ['attributes', 'fullName'],
toPath: ['name']
}, {
fromPath: ['attributes', 'imageURL'],
toPath: ['avatar']
}, {
fromPath: ['attributes', 'username'],
toPath: ['username']
}, {
toPath: ['currentUser'],
toValue: false
}];
this.uiToServiceMapTree = [{
toPath: ['id'],
fromPath: ['id']
}, {
toPath: ['attributes', 'fullName'],
fromPath: ['name']
}, {
toPath: ['attributes', 'imageURL'],
fromPath: ['avatar']
}, {
toPath: ['attributes', 'username'],
fromPath: ['username']
}, {
toPath: ['type'],
toValue: 'identities'
}];
}
UserMapper.prototype.toUIModel = function (arg) {
return switchModel(arg, this.serviceToUiMapTree);
};
UserMapper.prototype.toServiceModel = function (arg) {
return switchModel(arg, this.uiToServiceMapTree);
};
UserMapper.decorators = [
{ type: Injectable },
];
/** @nocollapse */
UserMapper.ctorParameters = function () { return []; };
return UserMapper;
}());
export { UserMapper };
var UserQuery = /** @class */ (function () {
function UserQuery(store, userService) {
this.store = store;
this.userService = userService;
this.plannerSelector = createFeatureSelector('planner');
this.userSelector = createSelector(this.plannerSelector, function (state) { return state.users; });
this.userSource = this.store.pipe(select(this.userSelector));
this.collaboratorIdsSelector = createSelector(this.plannerSelector, function (state) { return state.collaborators; });
this.collaboratorSelector = createSelector(this.userSelector, this.collaboratorIdsSelector, function (users, collabs) { return isEmpty(users) ? [] : collabs.map(function (c) { return users[c]; }); });
this.collaboratorSource = this.store.pipe(select(this.collaboratorSelector));
}
UserQuery.prototype.getUserObservableById = function (id) {
var _this = this;
return this.userSource
// If the desired user doesn't exist then fetch it
.pipe(select(function (users) { return users[id]; }), tap(function (user) {
if (!user) {
_this.store.dispatch(new GetUserAction(id));
}
}), filter(function (user) { return !!user; }) // filter the pipe based on availability of the user
);
};
UserQuery.prototype.getUserObservablesByIds = function (ids) {
var _this = this;
if (ids === void 0) { ids = []; }
if (!ids.length) {
return ObservableOf([]);
}
return combineLatest(ids.map(function (id) { return _this.getUserObservableById(id); }))
.pipe(
// When a user is not there in the collaborator list
// it fetches that particular user from API service
// meanwhile the combine observables returns null if async pipe is used
// We should return empty array instead
startWith([]));
};
UserQuery.prototype.getCollaborators = function () {
var _this = this;
return this.collaboratorSource
.pipe(filter(function (c) { return !!c.length; }), switchMap(function (collaborators) {
return _this.userService.loggedInUser
.pipe(map(function (u) {
return collaborators.map(function (c) {
return __assign({}, c, { currentUser: u ? c.id === u.id : false });
});
}));
}));
};
Object.defineProperty(UserQuery.prototype, "getCollaboratorIds", {
/**
* getCollaboratorAndLoggedInUserIds this function returns an
* array of all the collaborators IDs and loggedIn user IDs
*/
get: function () {
return this.store.pipe(select(this.collaboratorIdsSelector));
},
enumerable: true,
configurable: true
});
Object.defineProperty(UserQuery.prototype, "getLoggedInUser", {
/**
* This function returns the loggedInUser subject
*/
get: function () {
return this.userService.loggedInUser;
},
enumerable: true,
configurable: true
});
UserQuery.decorators = [
{ type: Injectable },
];
/** @nocollapse */
UserQuery.ctorParameters = function () { return [
{ type: Store, },
{ type: UserServiceClass, },
]; };
return UserQuery;
}());
export { UserQuery };
//# sourceMappingURL=user.js.map