Skip to content

Commit

Permalink
apply double parenthesis for filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ter1203 committed Oct 4, 2024
1 parent 87c36ad commit 0e333b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
13 changes: 12 additions & 1 deletion client/src/components/FilterContainer/FilterContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,18 @@ const FilterContainer = ({
logicErrors[0] ||
"Use AND, OR, and numbers to create logic (e.g., 1 AND 2 OR 3)"
}
onChange={(e) => handleLogicChange(e.target.value, onLogicChange)}
onChange={(e) => {
const value = e.target.value;

handleLogicChange(value, onLogicChange);
}}
onBlur={(e) => {
let value = e.target.value;
value = value.replace(/^\(\((.*)\)\)$/, "$1");
const formattedValue = `((${value}))`;

handleLogicChange(formattedValue, onLogicChange);
}}
/>
</Box>
</Box>
Expand Down
33 changes: 23 additions & 10 deletions client/src/components/FilterContainer/useFilterLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,29 @@ export const useFilterLogic = (initialFilterContainer, initialFilterFields) => {
);

const handleAddFilter = useCallback(() => {
setFilterContainer((prevContainer) => ({
...(prevContainer
? prevContainer
: { filters: [], filterLogic: "", name: "", direction: "" }),
filters: [
...prevContainer.filters,
{ field: "", operator: "", value: "", dataType: "string" },
],
filterLogic: prevContainer.filters.length === 0 ? "1" : `${prevContainer.filterLogic} AND ${prevContainer.filters.length + 1}`
}));
setFilterContainer((prevContainer) => {
const isInitialFilter = prevContainer.filters.length === 0;
const newFilterIndex = prevContainer.filters.length + 1;

const baseLogic = isInitialFilter
? "1"
: `${prevContainer.filterLogic} AND ${newFilterIndex}`;

const formattedLogic = prevContainer.filterLogic.startsWith("((") && prevContainer.filterLogic.endsWith("))")
? baseLogic
: `((${baseLogic}))`;

return {
...(prevContainer
? prevContainer
: { filters: [], filterLogic: "", name: "", direction: "" }),
filters: [
...prevContainer.filters,
{ field: "", operator: "", value: "", dataType: "string" },
],
filterLogic: formattedLogic
}
});
}, []);

const handleDeleteFilter = useCallback(
Expand Down

0 comments on commit 0e333b4

Please sign in to comment.