blob: 956d938ab495ca14e42ebcadf63429d629774cb8 [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 {Actions, createEffect, ofType} from "@ngrx/effects";
import {Action} from "@ngrx/store";
import {EMPTY, merge, Observable} from "rxjs";
import {catchError, filter, map, retry, startWith, switchMap} from "rxjs/operators";
import {ProcessApiService, StatementsApiService} from "../../../core";
import {setTasksAction} from "../../process/actions";
import {addAttachmentsAction, addStatementsAction, fetchStatementDetailsAction, fetchStatementsAction} from "../actions";
@Injectable({providedIn: "root"})
export class FetchStatementsEffect {
public readonly fetchStatements$ = createEffect(() => this.actions.pipe(
ofType(fetchStatementsAction),
switchMap(() => this.fetchStatements())
));
public readonly fetchStatementDetails$ = createEffect(() => this.actions.pipe(
ofType(fetchStatementDetailsAction),
filter((action) => typeof action?.statementId === "number"),
switchMap((action) => this.fetchStatementDetails(action.statementId))
));
public constructor(
private readonly actions: Actions,
private readonly statementsApiService: StatementsApiService,
private readonly processApiService: ProcessApiService
) {
}
public fetchStatements(): Observable<Action> {
return this.statementsApiService.getStatements().pipe(
map((statements) => {
return addStatementsAction({statements});
}),
retry(2),
catchError(() => EMPTY)
);
}
public fetchStatementDetails(statementId: number): Observable<Action> {
return this.statementsApiService.getStatement(statementId).pipe(
retry(2),
switchMap((statement) => {
return merge(
this.fetchTasks(statementId),
this.fetchAttachments(statementId)
).pipe(
startWith(addStatementsAction({statements: [statement]}))
);
}),
catchError((error) => {
console.error(error);
return EMPTY;
})
);
}
public fetchTasks(statementId: number) {
return this.processApiService.getStatementTasks(statementId).pipe(
map((tasks) => setTasksAction({statementId, tasks})),
retry(2)
);
}
public fetchAttachments(statementId: number) {
return this.statementsApiService.getAllAttachments(statementId).pipe(
map((attachments) => addAttachmentsAction({statementId, attachments})),
retry(2)
);
}
}