Skip to content

Commit f75e1f5

Browse files
committed
parse filter
1 parent 9c71333 commit f75e1f5

File tree

1 file changed

+94
-26
lines changed

1 file changed

+94
-26
lines changed

UI/web-app/src/components/HRQuerySource/HRQuerySource.base.tsx

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,27 @@ export const HRQuerySourceBase: React.FunctionComponent<HRQuerySourceProps> = (p
8383
setFilteredOptions({});
8484
setFilteredValueOptions({});
8585
} else {
86-
let items: IFilterPart[] = children.map((child, index) => ({
87-
attribute: child.filter.split(' ')[0],
88-
equalityOperator: child.filter.split(' ')[1],
89-
value: child.filter.split(' ')[2],
90-
andOr: child.filter.split(' ')[3]
91-
}));
86+
let items: IFilterPart[] = children.map((child, index) => {
87+
const parts = child.filter.split(' ');
88+
let value = '';
89+
let andOr = '';
90+
for (let i = 2; i < parts.length; i++) {
91+
const part = parts[i].toLowerCase();
92+
if (part === 'and' || part === 'or') {
93+
andOr = parts[i];
94+
value = parts.slice(2, i).join(' ');
95+
break;
96+
}
97+
}
98+
if (andOr === '') { value = parts.slice(2).join(' '); }
99+
const filterPart: IFilterPart = {
100+
attribute: parts[0],
101+
equalityOperator: parts[1],
102+
value: value,
103+
andOr: andOr
104+
};
105+
return filterPart;
106+
});
92107
setItems(items);
93108
}
94109
}
@@ -326,8 +341,7 @@ const checkType = (value: string, type: string | undefined): string => {
326341
setFilterErrorMessage('');
327342
setSource(props.source);
328343

329-
if (groupingEnabled) {
330-
344+
if (groupingEnabled && groupIndex !== undefined) {
331345
const emptyItemIndex = items.findIndex(item =>
332346
item.attribute === "" &&
333347
item.equalityOperator === "" &&
@@ -382,13 +396,24 @@ const checkType = (value: string, type: string | undefined): string => {
382396
return;
383397
}
384398

385-
const regex = /(?<= And | Or )/;
399+
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
386400
let segments = props.source.filter?.split(regex);
387401
let result = true;
388402
if (segments) {
389403
for (let i = 0; i < segments.length; i++) {
390404
const parts = segments[i].trim().split(' ');
391-
if (parts.length < 4) {
405+
let value = '';
406+
let andOr = '';
407+
for (let i = 2; i < parts.length; i++) {
408+
const part = parts[i].toLowerCase();
409+
if (part === 'and' || part === 'or') {
410+
andOr = parts[i];
411+
value = parts.slice(2, i).join(' ');
412+
break;
413+
}
414+
}
415+
if (andOr === '') { value = parts.slice(2).join(' '); }
416+
if (parts[0] === "" || parts[1] === "" || value === "" || andOr === "") {
392417
result = false;
393418
break;
394419
}
@@ -698,7 +723,7 @@ const checkType = (value: string, type: string | undefined): string => {
698723
return;
699724
}
700725

701-
const regex = /(?<= And | Or )/;
726+
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
702727
let segments = props.source.filter?.split(regex);
703728
if (item && (props.source.filter?.length === 0 || (segments?.length == children.length - 1))) {
704729
const a = item.key.toString();
@@ -743,7 +768,7 @@ const checkType = (value: string, type: string | undefined): string => {
743768
updateGroupItem(updateParams, index);
744769
return;
745770
}
746-
const regex = /(?<= And | Or )/;
771+
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
747772
let segments = props.source.filter?.split(regex);
748773
if (item && (props.source.filter?.length === 0 || (segments?.length == children.length - 1))) {
749774
let a = item.text;
@@ -801,7 +826,7 @@ const checkType = (value: string, type: string | undefined): string => {
801826
return;
802827
}
803828

804-
const regex = /(?<= And | Or )/;
829+
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
805830
let segments = props.source.filter?.split(regex);
806831
if (item && (props.source.filter?.length === 0 || (segments?.length == children.length - 1))) {
807832
let filter: string;
@@ -822,8 +847,21 @@ const checkType = (value: string, type: string | undefined): string => {
822847
words = segments[index].trim().split(' ');
823848
}
824849
if (words.length > 0) {
825-
words[2] = selectedValueAfterConversion || selectedValue;
826-
}
850+
let value = '';
851+
let andOr = '';
852+
for (let i = 2; i < words.length; i++) {
853+
const part = words[i].toLowerCase();
854+
if (part === 'and' || part === 'or') {
855+
andOr = words[i];
856+
value = words.slice(2, i).join(' ');
857+
break;
858+
}
859+
}
860+
if (andOr === '') { value = words.slice(2).join(' '); }
861+
words.splice(2);
862+
words.splice(2, 0, selectedValueAfterConversion || selectedValue);
863+
if (andOr !== '') { words.push(andOr + ' '); }
864+
}
827865
segments[index] = words.join(' ');
828866
const updatedFilter = segments.join('');
829867
setSource(prevSource => {
@@ -837,8 +875,9 @@ const checkType = (value: string, type: string | undefined): string => {
837875
};
838876

839877
const handleTAttributeValueChange = (attribute: string, event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue: string = '', index: number) => {
878+
const selectedAttribute = attributes?.find(({ hasMapping, name }) => ((hasMapping && `${name}_Code` === attribute) || (!hasMapping && name === attribute)));
840879
const selectedValue = newValue;
841-
const selectedValueAfterConversion = attributeValues[attribute] ? checkType(selectedValue, attributeValues[attribute].type) : selectedValue;
880+
const selectedValueAfterConversion = selectedAttribute?.type ? checkType(selectedValue, selectedAttribute?.type) : selectedValue;
842881

843882
const updatedItems = items.map((it, idx) => {
844883
if (idx === index) {
@@ -864,9 +903,10 @@ const checkType = (value: string, type: string | undefined): string => {
864903
return;
865904
}
866905
var newValue = event.target.value.trim();
906+
const selectedAttribute = attributes?.find(({ hasMapping, name }) => ((hasMapping && `${name}_Code` === attribute) || (!hasMapping && name === attribute)));
867907
const selectedValue = newValue;
868-
const selectedValueAfterConversion = attributeValues[attribute] ? checkType(selectedValue, attributeValues[attribute].type) : selectedValue;
869-
const regex = /(?<= And | Or )/;
908+
const selectedValueAfterConversion = selectedAttribute?.type ? checkType(selectedValue, selectedAttribute.type) : selectedValue;
909+
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
870910
let segments = props.source.filter?.split(regex);
871911
if (selectedValueAfterConversion !== "" && (props.source.filter?.length === 0 || (segments?.length == children.length - 1))) {
872912
let filter: string;
@@ -887,8 +927,21 @@ const checkType = (value: string, type: string | undefined): string => {
887927
words = segments[index].trim().split(' ');
888928
}
889929
if (words.length > 0) {
890-
words[2] = selectedValueAfterConversion || selectedValue;
891-
}
930+
let value = '';
931+
let andOr = '';
932+
for (let i = 2; i < words.length; i++) {
933+
const part = words[i].toLowerCase();
934+
if (part === 'and' || part === 'or') {
935+
andOr = words[i];
936+
value = words.slice(2, i).join(' ');
937+
break;
938+
}
939+
}
940+
if (andOr === '') { value = words.slice(2).join(' '); }
941+
words.splice(2);
942+
words.splice(2, 0, selectedValueAfterConversion || selectedValue);
943+
if (andOr !== '') { words.push(andOr + ' '); }
944+
}
892945
segments[index] = words.join(' ');
893946
const updatedFilter = segments.join('');
894947
setSource(prevSource => {
@@ -923,12 +976,11 @@ const checkType = (value: string, type: string | undefined): string => {
923976
const regex = /(?<= [Aa][Nn][Dd] | [Oo][Rr] )/;
924977
let segments = props.source.filter?.split(regex);
925978
if (item && (props.source.filter?.length === 0 || (segments?.length == children.length - 1))) {
926-
const a = item.text;
927979
let filter: string;
928980
if (source.filter !== "") {
929-
filter = `${source.filter} ` + a;
981+
filter = `${source.filter} ` + item.text;
930982
} else {
931-
filter = a;
983+
filter = item.text;
932984
}
933985
setSource(prevSource => {
934986
const newSource = { ...prevSource, filter };
@@ -938,15 +990,31 @@ const checkType = (value: string, type: string | undefined): string => {
938990
}
939991
else if (segments && index !== undefined && segments[index] && item) {
940992
let words = segments[index].split(' ');
941-
if (words[0] === "") { words = segments[index].trim().split(' '); }
942993
if (words[0] === "") {
943994
words = segments[index].trim().split(' ');
944995
}
996+
if (words.length > 0 && words[words.length - 1] === "") {
997+
words.pop();
998+
}
945999
if (words.length > 0) {
946-
words[3] = item.text;
1000+
let value = '';
1001+
let andOr = '';
1002+
let startIndex = 2;
1003+
for (let i = startIndex; i < words.length; i++) {
1004+
const part = words[i].toLowerCase();
1005+
if (part === 'and' || part === 'or') {
1006+
andOr = words[i];
1007+
value = words.slice(startIndex, i).join(' ');
1008+
break;
1009+
}
1010+
}
1011+
if (andOr === '') { value = words.slice(startIndex).join(' '); }
1012+
const indexAfterValue = startIndex + value.split(' ').length;
1013+
words.splice(indexAfterValue);
1014+
words.splice(indexAfterValue, 0, item.text);
9471015
}
9481016
segments[index] = words.join(' ');
949-
const updatedFilter = segments.join('');
1017+
const updatedFilter = segments.join(' ');
9501018
setSource(prevSource => {
9511019
let filter = updatedFilter;
9521020
const newSource = { ...prevSource, filter };

0 commit comments

Comments
 (0)