blob: d8a9ce8f356eb3855e5ead747ab5d33b6e71c133 [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 {createReducer, on} from "@ngrx/store";
import {arrayJoin, filterDistinctValues, TStoreEntities} from "../../../util/store";
import {deleteTaskAction, setTasksAction} from "../actions";
export const statementTaskReducer = createReducer<TStoreEntities<string[]>>(
{},
on(setTasksAction, (state, payload) => {
const statementId = payload?.statementId;
if (typeof statementId !== "number") {
return state;
}
const taskIds = !Array.isArray(payload?.tasks) ? [] : payload.tasks
.filter((task) => task?.statementId === statementId && typeof task?.taskId === "string")
.map((task) => task.taskId);
return {
...state,
[statementId]: taskIds.length === 0 ? undefined : filterDistinctValues(taskIds)
};
}),
on(deleteTaskAction, (state, payload) => {
if (typeof payload?.statementId !== "number") {
return state;
}
const statementId = payload.statementId;
const taskId = payload.taskId;
const tasks = taskId == null ? [] : arrayJoin(state[statementId]).filter((t) => t !== taskId);
return {
...state,
[statementId]: tasks.length > 0 ? tasks : undefined
};
})
);