blob: e9670519116f6c261dfacaae332a6bed2a1f16c9 [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 {IAPIProcessTask} from "../../../core/api/process";
import {arrayToEntities, deleteEntities, entitiesToArray, TStoreEntities} from "../../../util/store";
import {deleteTaskAction, setTasksAction, updateTaskAction} from "../actions";
export const tasksReducer = createReducer<TStoreEntities<IAPIProcessTask>>(
{},
on(setTasksAction, (state, payload) => {
const statementId = payload?.statementId;
if (typeof statementId !== "number") {
return state;
}
const oldTasks = entitiesToArray(state)
.filter((task) => task.statementId === statementId);
const newTasks = !Array.isArray(payload?.tasks) ? [] : payload.tasks
.filter((task) => task?.statementId === statementId && typeof task?.taskId === "string");
return {
...deleteEntities(state, oldTasks.map((task) => task.taskId)),
...arrayToEntities<IAPIProcessTask>(newTasks, (task) => task.taskId)
};
}),
on(deleteTaskAction, (state, payload) => {
if (typeof payload?.statementId !== "number" || typeof payload?.taskId !== "string") {
return state;
}
return deleteEntities(state, [payload.taskId]);
}),
on(updateTaskAction, (state, payload) => {
const taskId = payload?.task?.taskId;
if (typeof taskId !== "string" || state[taskId] == null) {
return state;
}
return {
...state,
[taskId]: {
...state[taskId],
...payload.task
}
};
})
);