| /******************************************************************************** |
| * 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 {Injectable} from "@angular/core"; |
| import {Router} from "@angular/router"; |
| import {Actions, createEffect, ofType} from "@ngrx/effects"; |
| import {Action} from "@ngrx/store"; |
| import {EMPTY, Observable} from "rxjs"; |
| import {catchError, exhaustMap, filter, map} from "rxjs/operators"; |
| import {StatementsApiService} from "../../../core/api/statements/statements-api.service"; |
| import {arrayToEntities} from "../../../util/store.util"; |
| import {addNewStatementAction, setStatementEntitiesAction} from "../statements.actions"; |
| |
| @Injectable({providedIn: "root"}) |
| export class AddStatementEffect { |
| |
| public readonly addStatement$ = createEffect(() => this.actions.pipe( |
| ofType(addNewStatementAction), |
| filter((action) => typeof action?.title === "string" && typeof action?.dueDate === "string"), |
| exhaustMap((action) => this.addStatement(action.title, action.dueDate)) |
| )); |
| |
| public constructor( |
| private readonly actions: Actions, |
| private readonly statementsApiService: StatementsApiService, |
| private readonly router: Router) { |
| |
| } |
| |
| public addStatement(title: string, dueDate: string): Observable<Action> { |
| return this.statementsApiService.postStatement({title, dueDate}).pipe( |
| map((statement) => { |
| const entities = arrayToEntities([statement], () => statement.id); |
| this.router.navigate(["/details"], {queryParams: {id: statement.id}}); |
| return setStatementEntitiesAction({entities}); |
| }), |
| catchError(() => EMPTY) |
| ); |
| } |
| |
| } |