| /******************************************************************************** |
| * 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, OnInit} from "@angular/core"; |
| import {select, Store} from "@ngrx/store"; |
| import {take} from "rxjs/operators"; |
| import {IPageHeaderAction} from "../../../../shared/layout/page-header"; |
| import { |
| IStatementInfoFormValue, |
| newStatementFormErrorSelector, |
| newStatementFormLoadingSelector, |
| newStatementFormValueSelector, |
| statementTypesSelector, |
| submitNewStatementAction |
| } from "../../../../store"; |
| |
| @Component({ |
| selector: "app-new-statement", |
| templateUrl: "./new-statement.component.html", |
| styleUrls: ["./new-statement.component.scss"] |
| }) |
| export class NewStatementComponent implements OnInit { |
| |
| public readonly pageHeaderActions: IPageHeaderAction[] = [ |
| { |
| name: "core.actions.backToDashboard", |
| icon: "arrow_back", |
| routerLink: "/" |
| } |
| ]; |
| |
| public typeOptions$ = this.store.pipe(select(statementTypesSelector)); |
| |
| public value$ = this.store.pipe(select(newStatementFormValueSelector)); |
| |
| public isLoading$ = this.store.pipe(select(newStatementFormLoadingSelector)); |
| |
| public error$ = this.store.pipe(select(newStatementFormErrorSelector)); |
| |
| public form: IStatementInfoFormValue; |
| |
| public constructor(private readonly store: Store) { |
| |
| } |
| |
| /** |
| * Shows the saved form-data if there was an error or the form was previously submitted (isLoading) |
| * so the input data is still present and doesn't have to be repeated. |
| */ |
| public async ngOnInit() { |
| const isLoading = await this.isLoading$.pipe(take(1)).toPromise(); |
| const error = await this.error$.pipe(take(1)).toPromise(); |
| |
| if (isLoading || error != null) { |
| this.form = await this.value$.pipe(take(1)).toPromise(); |
| } |
| } |
| |
| public submit(value: IStatementInfoFormValue) { |
| this.store.dispatch(submitNewStatementAction({value})); |
| } |
| |
| } |