blob: 87e471126b0649217bde4fa2e276f2d6473519c1 [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 {Action, createReducer, on} from "@ngrx/store";
import {arrayJoin, filterDistinctValues, TStoreEntities} from "../../../util";
import {addAttachmentsAction, addStatementsAction} from "../actions";
import {IStatementEntity} from "../model";
export const statementsEntitiesReducer = createReducer<TStoreEntities<IStatementEntity>, Action>(
{},
on(addStatementsAction, (state, payload) => {
const statements = (Array.isArray(payload?.statements) ? payload.statements : [])
.filter((statement) => typeof statement?.id === "number");
if (statements.length === 0) {
return state;
}
state = state == null ? {} : {...state};
statements.forEach((statement) => {
const attachments = filterDistinctValues(arrayJoin(state[statement.id]?.attachments, statement?.attachments));
state[statement.id] = {
...state[statement.id],
...statement,
attachments: attachments.length > 0 ? attachments : undefined
};
});
return state;
}),
on(addAttachmentsAction, (state, payload) => {
const id = payload?.statementId;
const statement = id == null || state == null ? undefined : state[id];
const attachments = (Array.isArray(payload?.attachments) ? payload.attachments : [])
.map((attachment) => attachment.id);
return statement == null || attachments.length === 0 ? state : {
...state,
[id]: {
...statement,
attachments: filterDistinctValues(arrayJoin(statement?.attachments, attachments))
}
};
})
);