| /******************************************************************************** |
| * 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, switchMap} from "rxjs/operators"; |
| import {queryParamsIdSelector} from "../../../../store/root/selectors/query-params.selectors"; |
| import {IStatementEntity} from "../../../../store/statements/model/IStatementEntity"; |
| import {fetchStatementDetailsAction} from "../../../../store/statements/statements.actions"; |
| import {statementSelector} from "../../../../store/statements/statements.selectors"; |
| |
| @Component({ |
| selector: "app-statement-details", |
| templateUrl: "./statement-details.component.html", |
| styleUrls: ["./statement-details.component.scss"] |
| }) |
| export class StatementDetailsComponent implements OnInit, OnDestroy { |
| |
| public id$: Observable<number>; |
| |
| public statement$: Observable<IStatementEntity>; |
| |
| private subsription: Subscription; |
| |
| public constructor(private readonly store: Store) { |
| this.id$ = this.store.pipe(select(queryParamsIdSelector)); |
| |
| this.statement$ = this.id$.pipe( |
| switchMap((id) => this.store.pipe(select(statementSelector, {id}))) |
| ); |
| } |
| |
| public ngOnInit(): void { |
| this.subsription = this.id$ |
| .pipe(filter((id) => id != null)) |
| .subscribe((id) => this.store.dispatch(fetchStatementDetailsAction({id}))); |
| } |
| |
| public ngOnDestroy(): void { |
| if (this.subsription != null) { |
| this.subsription.unsubscribe(); |
| } |
| } |
| |
| } |