UNPKG

fabric8-planner

Version:
362 lines 19.5 kB
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; }; 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); }; import { Injectable } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { select, Store } from '@ngrx/store'; import { Notifications, NotificationType } from 'ngx-base'; import { empty, Observable, of } from 'rxjs'; import { catchError, map, mergeMap, switchMap } from 'rxjs/operators'; import { cleanObject } from '../models/common.model'; import { FilterService } from '../services/filter.service'; import { AND, EQUAL } from '../services/query-keys'; import * as BoardUIActions from './../actions/board-ui.actions'; import * as ColumnWorkItemActions from './../actions/column-workitem.action'; import * as WorkItemActions from './../actions/work-item.actions'; import { WorkItemMapper } from './../models/work-item'; import { WorkItemService as WIService } from './../services/work-item.service'; import * as util from './work-item-utils'; var WorkItemEffects = /** @class */ (function () { function WorkItemEffects(actions$, workItemService, store, router, route, filterService, errHandler, notifications) { var _this = this; this.actions$ = actions$; this.workItemService = workItemService; this.store = store; this.router = router; this.route = route; this.filterService = filterService; this.errHandler = errHandler; this.notifications = notifications; this.workItemMapper = new WorkItemMapper(); this.addWorkItems$ = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.ADD, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (op) { var payload = op.payload; var state = op.state; var createID = payload.createId; var workItem = payload.workItem; var parentId = payload.parentId; return _this.workItemService.create(state.space.links.self + '/workitems', workItem) .pipe(map(function (item) { var itemUI = _this.workItemMapper.toUIModel(item); var wid = _this.workItemMapper.toDynamicUIModel(item, state.workItemTypes.entities[itemUI.type].dynamicfields); itemUI.createId = createID; return __assign({}, itemUI, wid); }), switchMap(function (w) { return util.workitemMatchesFilter(_this.route.snapshot, _this.filterService, _this.workItemService, w, state.space.id); }), mergeMap(function (wItem) { // If a child item is created if (parentId) { wItem.parentID = parentId; // TODO : solve the hack :: link the item var linkPayload = util.createLinkObject(parentId, wItem.id, '25c326a7-6d03-4f5a-b23b-86a9ee4171e9'); return _this.workItemService.createLink(state.space.links.self.split('space')[0] + 'workitemlinks', { data: linkPayload }) .pipe(map(function () { // for a normal (not a child) work item creation // Add item success notification var parent = state.workItems.entities[parentId]; if (!parent.childrenLoaded && parent.hasChildren) { return new WorkItemActions.GetChildren(parent); } else { if (payload.openDetailPage) { _this.router.navigateByUrl(document.location.pathname + '/detail/' + wItem.number, { relativeTo: _this.route }); } return new WorkItemActions.AddSuccess(wItem); } })); } else { var currentURL = document.location.pathname; var detailURL = currentURL.indexOf('/plan/query') > -1 ? currentURL.split('/plan/query')[0] : null; var routeURL = detailURL ? detailURL + '/plan/detail/' + wItem.number : currentURL + '/detail/' + wItem.number; if (payload.openDetailPage) { _this.router.navigateByUrl(routeURL, { relativeTo: _this.route }); } else if (currentURL.indexOf('/plan/query') > -1) { try { _this.notifications.message({ message: "New Work Item #" + wItem.number + " created.", type: NotificationType.SUCCESS }); } catch (e) { console.log('Work item is added.'); } return empty(); } return of(new WorkItemActions.AddSuccess(wItem)); } }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem adding work item.", new WorkItemActions.AddError()); })); })); this.getWorkItems$ = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.GET, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (wp) { var payload = wp.payload; var state = wp.state; var spaceQuery = _this.filterService.queryBuilder('space', EQUAL, state.space.id); var finalQuery = _this.filterService.queryJoiner(payload.filters, AND, spaceQuery); return _this.workItemService.getWorkItems(payload.pageSize, { expression: finalQuery }) .pipe(map(function (data) { var wis = []; var nextLink = data.nextLink ? data.nextLink : ''; if (payload.isShowTree) { var ancestors = data.ancestorIDs; wis = _this.resolveWorkItems(data.workItems, state, payload.isShowTree, ancestors); var wiIncludes = _this.resolveWorkItems(data.included, state, false, ancestors); return { workItems: wis.concat(wiIncludes), nextLink: nextLink }; } else { wis = _this.resolveWorkItems(data.workItems, state, payload.isShowTree); } return { workItems: wis.slice(), nextLink: nextLink }; }), map(function (d) { return new WorkItemActions.GetSuccess(d); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem loading workitems.", new WorkItemActions.GetError()); })); })); this.getWorkItemChildren$ = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.GET_CHILDREN, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (wp) { var parent = wp.payload; var state = wp.state; return _this.workItemService .getChildren(parent.childrenLink) .pipe(map(function (data) { var wis = _this.resolveWorkItems(data, state) // resolve parent ID .map(function (w) { w.parentID = parent.id; return w; }); return wis.slice(); }), map(function (workItems) { return new WorkItemActions.GetChildrenSuccess({ parent: parent, children: workItems }); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem loading children.", new WorkItemActions.GetChildrenError(parent)); })); })); this.updateWorkItem$ = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.UPDATE, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (wp) { var payload; if (wp.payload.type) { // This order must be followed // because baseType is needed for dynamic fields var dynamicPayload = _this.workItemMapper.toDyanmicServiceModel(wp.payload, wp.state.workItemTypes.entities[wp.payload.type].dynamicfields); var staticPayload = _this.workItemMapper.toServiceModel(wp.payload); payload = cleanObject(__assign({}, staticPayload, { attributes: __assign({}, staticPayload.attributes, dynamicPayload.attributes) })); } else { payload = _this.workItemMapper.toServiceModel(wp.payload); } var state = wp.state; return _this.workItemService.update(payload) .pipe(map(function (w) { return _this.resolveWorkItems([w], state)[0]; }), switchMap(function (w) { return util.workitemMatchesFilter(_this.route.snapshot, _this.filterService, _this.workItemService, w, state.space.id); }), map(function (w) { var item = state.workItems.entities[w.id]; if (item) { w.treeStatus = item.treeStatus; w.childrenLoaded = item.childrenLoaded; w.parentID = item.parentID; } return w; }), map(function (workItem) { return new WorkItemActions.UpdateSuccess(workItem); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem in update Workitem.", new WorkItemActions.UpdateError()); })); })); this.Reorder = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.REORDER, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (op) { var workitem = _this.workItemMapper.toServiceModel(op.payload.workitem); return _this.workItemService.reOrderWorkItem(op.state.space.links.self, workitem, op.payload.destinationWorkitemID, op.payload.direction) .pipe(map(function (w) { return _this.resolveWorkItems([w], op.state)[0]; }), map(function (w) { w.treeStatus = op.payload.workitem.treeStatus; w.bold = op.payload.workitem.bold; w.childrenLoaded = op.payload.workitem.childrenLoaded; w.parentID = op.state.workItems.entities[w.id].parentID; return w; }), map(function (w) { return new WorkItemActions.UpdateSuccess(w); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem in reorder Workitem.", new WorkItemActions.UpdateError()); })); })); this.updateWorkItemFromBoard = this.actions$ .pipe(util.filterTypeWithSpace(ColumnWorkItemActions.UPDATE, this.store.pipe(select('planner'))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (wp) { var staticPayload = _this.workItemMapper.toServiceModel(wp.payload.workItem); var payload = cleanObject(staticPayload, ['baseType']); return _this.workItemService.update(payload) .pipe(switchMap(function (workitem) { var reorderPayload = wp.payload.reorder; reorderPayload.workitem.version = workitem.attributes['version']; var workItem = _this.workItemMapper.toServiceModel(reorderPayload.workitem); return reorderPayload.direction ? _this.workItemService.reOrderWorkItem(wp.state.space.links.self, workItem, reorderPayload.destinationWorkitemID, reorderPayload.direction) : of(workitem); }), map(function (w) { var wi = _this.resolveWorkItems([w], wp.state)[0]; return wi; }), switchMap(function (w) { return [ new WorkItemActions.UpdateSuccess(w), new ColumnWorkItemActions.UpdateSuccess({ workItemId: w.id, prevColumnId: wp.payload.prevColumnId, newColumnIds: w.columnIds }), new BoardUIActions.UnlockBoard() ]; }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem in updating WorkItem", [ new ColumnWorkItemActions.UpdateError({ prevColumnId: wp.payload.prevColumnId, newColumnIds: wp.payload.workItem.columnIds }), new BoardUIActions.UnlockBoard() ]); })); })); this.getMoreWorkItems$ = this.actions$ .pipe(util.filterTypeWithSpace(WorkItemActions.GET_MORE_WORKITEMS, this.store.pipe(select(function (state) { return state.planner; }))), map(function (_a) { var action = _a[0], state = _a[1]; return { payload: action.payload, state: state }; }), switchMap(function (wp) { var payload = wp.payload; var state = wp.state; if (state.workItems.nextLink == '') { return of(new WorkItemActions.GetMoreWorkItemsSuccess({ workItems: [], nextLink: '' })); } return _this.workItemService.getMoreWorkItems(state.workItems.nextLink) .pipe(map(function (data) { var wis = []; var nextLink = data.nextLink ? data.nextLink : ''; if (payload.isShowTree) { var ancestors = data.ancestorIDs; wis = _this.resolveWorkItems(data.workItems, state, payload.isShowTree, ancestors); var wiIncludes = _this.resolveWorkItems(data.included, state, false, ancestors); return { workItems: wis.concat(wiIncludes), nextLink: nextLink }; } else { wis = _this.resolveWorkItems(data.workItems, state, payload.isShowTree); } return { workItems: wis.slice(), nextLink: nextLink }; }), map(function (d) { return new WorkItemActions.GetMoreWorkItemsSuccess(d); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem in fetching more workitems.", new WorkItemActions.GetError()); })); })); this.deleteWorkItem$ = this.actions$ .pipe(ofType(WorkItemActions.DELETE), switchMap(function (action) { var workItem = _this.workItemMapper.toServiceModel(action.payload); return _this.workItemService.delete(workItem) .pipe(map(function () { return new WorkItemActions.DeleteSuccess(action.payload); }), catchError(function (err) { return _this.errHandler.handleError(err, "Problem in Deleting work item.", new WorkItemActions.DeleteError()); })); })); } WorkItemEffects.prototype.resolveWorkItems = function (workItems, state, matchingQuery, ancestors) { var _this = this; if (matchingQuery === void 0) { matchingQuery = false; } if (ancestors === void 0) { ancestors = []; } var hasAncestors = !!ancestors.length; return workItems.map(function (wi) { var workItemUI = _this.workItemMapper.toUIModel(wi); workItemUI.bold = matchingQuery; if (hasAncestors) { workItemUI.treeStatus = ancestors.findIndex(function (a) { return a === workItemUI.id; }) > -1 ? 'expanded' : workItemUI.treeStatus; if (workItemUI.treeStatus === 'expanded') { workItemUI.childrenLoaded = true; } } var wid = _this.workItemMapper.toDynamicUIModel(wi, state.workItemTypes.entities[workItemUI.type].dynamicfields); return __assign({}, workItemUI, wid); }); }; WorkItemEffects.decorators = [ { type: Injectable }, ]; /** @nocollapse */ WorkItemEffects.ctorParameters = function () { return [ { type: Actions, }, { type: WIService, }, { type: Store, }, { type: Router, }, { type: ActivatedRoute, }, { type: FilterService, }, { type: util.ErrorHandler, }, { type: Notifications, }, ]; }; __decorate([ Effect(), __metadata("design:type", Object) ], WorkItemEffects.prototype, "addWorkItems$", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "getWorkItems$", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "getWorkItemChildren$", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "updateWorkItem$", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "Reorder", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "updateWorkItemFromBoard", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "getMoreWorkItems$", void 0); __decorate([ Effect(), __metadata("design:type", Observable) ], WorkItemEffects.prototype, "deleteWorkItem$", void 0); return WorkItemEffects; }()); export { WorkItemEffects }; //# sourceMappingURL=work-item.effects.js.map