blob: 703360ee3c1af9aff9db8c327390a64a0dc4b414 [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 {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" || typeof payload?.taskId !== "string") {
return state;
}
const {statementId, taskId} = payload;
const tasks = Array.isArray(state[statementId]) ? state[statementId].filter((t) => t !== taskId) : [];
return {
...state,
[statementId]: tasks.length > 0 ? tasks : undefined
};
})
);