blob: 1c792c9148c4e63db0b7209ef3db7d5e96b438bf [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 {createSelector} from "@ngrx/store";
import {ISelectOption, ISelectOptionGroup} from "../../../../shared/controls/select";
import {arrayJoin, objectToArray} from "../../../../util/store";
import {IDepartmentOptionValue, IWorkflowFormValue} from "../../model";
import {getStatementDepartmentConfigurationSelector} from "../statement-configuration.selectors";
import {statementParentIdsSelector, statementWorkflowSelector} from "../statement.selectors";
export const departmentGroupsObjectSelector = createSelector(
getStatementDepartmentConfigurationSelector,
statementWorkflowSelector,
(configuration, workflow) => {
return configuration?.allDepartments != null ? configuration.allDepartments : workflow?.selectedDepartments;
}
);
export const selectedDepartmentGroupsObjectSelector = createSelector(
getStatementDepartmentConfigurationSelector,
statementWorkflowSelector,
(configuration, workflow) => {
return workflow?.selectedDepartments == null ? configuration?.suggestedDepartments : workflow.selectedDepartments;
}
);
export const departmentGroupsSelector = createSelector(
departmentGroupsObjectSelector,
(departmentGroupsObject): ISelectOptionGroup<IDepartmentOptionValue>[] => {
return objectToArray(departmentGroupsObject)
.filter((obj) => obj.key != null)
.sort((a, b) => a.key.localeCompare(b.key))
.map<ISelectOptionGroup>((obj) => {
return {
label: obj.key,
options: [...obj.value].sort().map((name) => ({groupName: obj.key, name}))
};
});
}
);
export const departmentOptionsSelector = createSelector(
departmentGroupsSelector,
(groups): ISelectOption<IDepartmentOptionValue>[] => {
const groupOptions = groups.map<IDepartmentOptionValue[]>((group) => group.options);
return arrayJoin(...groupOptions).map((value) => ({label: value.name, value}));
}
);
export const selectedDepartmentSelector = createSelector(
selectedDepartmentGroupsObjectSelector,
departmentGroupsSelector,
(selectedGroupObject, groups): IDepartmentOptionValue[] => {
const groupOptions = (Array.isArray(groups) && selectedGroupObject != null ? groups : [])
.filter((group) => Array.isArray(selectedGroupObject[group.label]))
.map<IDepartmentOptionValue[]>((group) => {
return group.options.filter((option) => {
return selectedGroupObject[group.label].find((name) => name === option.name) != null;
});
});
return arrayJoin(...groupOptions);
}
);
export const workflowFormValueSelector = createSelector(
statementWorkflowSelector,
selectedDepartmentSelector,
statementParentIdsSelector,
(workflow, departments, parentIds): IWorkflowFormValue => {
return {
departments: arrayJoin(departments),
geographicPosition: workflow?.geoPosition,
parentIds: arrayJoin(parentIds)
};
}
);