blob: f119b47a0bc0b766a279410436333efa66c9c1ef [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} from "rxjs";
import {distinctUntilChanged, take, takeUntil} from "rxjs/operators";
import {IAPISearchOptions} from "../../../../core/api";
import {
createWorkflowForm,
departmentGroupsSelector,
departmentOptionsSelector,
getSearchContentStatementsSelector,
getStatementErrorSelector,
IStatementErrorEntity,
IWorkflowFormValue,
queryParamsIdSelector,
setErrorAction,
startStatementSearchAction,
statementLoadingSelector,
statementTypesSelector,
submitWorkflowDataFormAction,
taskSelector,
workflowFormValueSelector
} from "../../../../store";
import {AbstractReactiveFormComponent} from "../../abstract";
@Component({
selector: "app-workflow-data-form",
templateUrl: "./workflow-data-form.component.html",
styleUrls: ["./workflow-data-form.component.scss"]
})
export class WorkflowDataFormComponent extends AbstractReactiveFormComponent<IWorkflowFormValue> implements OnInit, OnDestroy {
public task$ = this.store.pipe(select(taskSelector));
public statementId$ = this.store.pipe(select(queryParamsIdSelector));
public statementTypes$ = this.store.pipe(select(statementTypesSelector));
public searchContent$ = this.store.pipe(select(getSearchContentStatementsSelector));
public departmentOptions$ = this.store.pipe(select(departmentOptionsSelector));
public departmentGroups$ = this.store.pipe(select(departmentGroupsSelector));
public isStatementLoading$ = this.store.pipe(select(statementLoadingSelector));
public appFormGroup = createWorkflowForm();
public appErrorMessage$: Observable<IStatementErrorEntity> = this.store.pipe(select(getStatementErrorSelector));
private form$ = this.store.pipe(select(workflowFormValueSelector));
public constructor(public store: Store) {
super();
}
public async ngOnInit() {
this.updateForm();
this.task$.pipe(takeUntil(this.destroy$)).subscribe(() => this.search({q: ""}));
}
public async ngOnDestroy() {
super.ngOnDestroy();
return this.clearErrors();
}
public async submit(completeTask?: boolean) {
await this.clearErrors();
const task = await this.task$.pipe(take(1)).toPromise();
this.store.dispatch(submitWorkflowDataFormAction({
statementId: task.statementId,
taskId: task.taskId,
data: this.getValue(),
completeTask
}));
}
public search(options: IAPISearchOptions) {
this.store.dispatch(startStatementSearchAction({options}));
}
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}));
}
}
private updateForm() {
this.isStatementLoading$.pipe(takeUntil(this.destroy$), distinctUntilChanged())
.subscribe((loading) => loading ? this.appFormGroup.disable() : this.appFormGroup.enable());
this.form$.pipe(takeUntil(this.destroy$)).subscribe((value) => this.patchValue(value));
}
}