@hitachivantara/uikit-react-core
Version:
UI Kit Core React components.
88 lines (87 loc) • 2.28 kB
JavaScript
import { emptyGroup, emptyRule, findNodeById, findParentById } from "./index.js";
//#region src/QueryBuilder/utils/reducer.tsx
var reducer = (state, action) => {
const query = structuredClone(state);
switch (action.type) {
case "reset-query": return emptyGroup();
case "set-query": return action.query;
case "reset-group": {
const group = findNodeById(action.id, query);
if (group && "rules" in group) {
group.rules = [emptyRule()];
return query;
}
break;
}
case "add-rule": {
const group = findNodeById(action.id, query);
if (group && "rules" in group) {
const rule = emptyRule();
group.rules.push(rule);
return query;
}
break;
}
case "add-group": {
const group = findNodeById(action.id, query);
if (group && "rules" in group) {
group.rules.push(emptyGroup(true));
return query;
}
break;
}
case "remove-node": {
const parent = findParentById(action.id, query);
if (parent) {
parent.rules = parent.rules.filter((rule) => rule.id !== action.id);
return query;
}
return emptyGroup();
}
case "set-combinator": {
const node = findNodeById(action.id, query);
if (node && "combinator" in node) {
if (node.combinator !== action.combinator) {
node.combinator = action.combinator;
return query;
}
}
break;
}
case "set-attribute": {
const node = findNodeById(action.id, query);
if (node && node.attribute !== action.attribute) {
node.attribute = action.attribute ?? void 0;
if (action.operator !== void 0) {
node.operator = action.operator ?? void 0;
node.value = action.value ?? void 0;
}
return query;
}
break;
}
case "set-operator": {
const node = findNodeById(action.id, query);
if (node && "attribute" in node) {
if (node.operator !== action.operator) {
node.operator = action.operator;
if (action.value !== void 0) node.value = action.value ?? void 0;
return query;
}
}
break;
}
case "set-value": {
const node = findNodeById(action.id, query);
if (node && "operator" in node) {
if ("value" in node) node.value = action.value ?? void 0;
return query;
}
break;
}
default:
}
return query;
};
//#endregion
export { reducer as default };