blob: 322432abcd8d5e0be8de549caf7c3f21088fd55c [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {Component, OnDestroy, OnInit} from "@angular/core";
import {select, Store} from "@ngrx/store";
import {Observable, Subscription} from "rxjs";
import {filter, take} from "rxjs/operators";
import {IAPIProcessTask} from "../../../../core/api/process";
import {
addCommentAction,
claimTaskAction,
currentActivityIds,
deleteCommentAction,
fetchStatementDetailsAction,
getStatementErrorSelector,
historySelector,
IStatementErrorEntity,
processDiagramSelector,
processNameSelector,
processVersionSelector,
queryParamsIdSelector,
setErrorAction,
statementLoadingSelector,
statementSelector,
statementTasksSelector,
statementTitleSelector,
userNameSelector,
userRolesSelector
} from "../../../../store";
@Component({
selector: "app-statement-details",
templateUrl: "./statement-details.component.html",
styleUrls: ["./statement-details.component.scss"]
})
export class StatementDetailsComponent implements OnInit, OnDestroy {
public statementId$ = this.store.pipe(select(queryParamsIdSelector));
public statement$ = this.store.pipe(select(statementSelector));
public title$ = this.store.pipe(select(statementTitleSelector));
public processName$ = this.store.pipe(select(processNameSelector));
public processVersion$ = this.store.pipe(select(processVersionSelector));
public processHistory$ = this.store.pipe(select(historySelector));
public processDiagram$ = this.store.pipe(select(processDiagramSelector));
public processCurrentActivityIds$ = this.store.pipe(select(currentActivityIds));
public userRoles$ = this.store.pipe(select(userRolesSelector));
public userName$ = this.store.pipe(select(userNameSelector));
public tasks$ = this.store.pipe(select(statementTasksSelector));
public isStatementLoading$ = this.store.pipe(select(statementLoadingSelector));
public appError$: Observable<IStatementErrorEntity> = this.store.pipe(select(getStatementErrorSelector));
private subscription: Subscription;
public constructor(public readonly store: Store) {
}
public ngOnInit(): void {
this.subscription = this.statementId$
.pipe(filter((statementId) => statementId != null))
.subscribe((statementId) => this.store.dispatch(fetchStatementDetailsAction({statementId})));
}
public async ngOnDestroy() {
if (this.subscription != null) {
this.subscription.unsubscribe();
this.subscription = null;
}
await this.clearErrors();
}
public editTask(task: IAPIProcessTask, options?: any) {
this.store.dispatch(claimTaskAction({
statementId: task?.statementId,
taskId: task?.taskId,
options
}));
}
public async addComment(text: string) {
const statementId = await this.statementId$.pipe(take(1)).toPromise();
this.store.dispatch(addCommentAction({statementId, text}));
}
public async deleteComment(commentId: number) {
const statementId = await this.statementId$.pipe(take(1)).toPromise();
this.store.dispatch(deleteCommentAction({statementId, commentId}));
}
private async clearErrors() {
const statementId = await this.statementId$.pipe(take(1)).toPromise();
const loading = await this.isStatementLoading$.pipe(take(1)).toPromise();
if (statementId != null && !loading) {
this.store.dispatch(setErrorAction({statementId, error: null}));
}
}
}