blob: afd2dcbb854f2c252d9726f7d240f9dc70b9227a [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 {Injectable} from "@angular/core";
import {Router} from "@angular/router";
import {Actions, createEffect, ofType} from "@ngrx/effects";
import {Action} from "@ngrx/store";
import {Observable} from "rxjs";
import {catchError, filter, map, switchMap} from "rxjs/operators";
import {ProcessApiService} from "../../../core/api/process";
import {claimTaskAction, deleteTaskAction, updateTaskAction} from "../actions";
@Injectable({providedIn: "root"})
export class EditTaskEffect {
public readonly editTask$ = createEffect(() => this.actions$.pipe(
ofType(claimTaskAction),
filter((action) => typeof action.statementId === "number" && typeof action.taskId === "string"),
switchMap((action) => this.editTask(action.statementId, action.taskId, action.options))
));
public constructor(
private readonly actions$: Actions,
private readonly processApiService: ProcessApiService,
private readonly router: Router
) {
}
public editTask(statementId: number, taskId: string, queryParams?: any): Observable<Action> {
queryParams = {...queryParams, id: statementId, taskId};
return this.processApiService.claimStatementTask(statementId, taskId).pipe(
map((task) => updateTaskAction({task})),
catchError(async () => deleteTaskAction({statementId, taskId})),
switchMap(async (action) => {
await this.router.navigate(["/edit"], {queryParams});
return action;
}),
);
}
}