Skip to content

Commit 71b9e80

Browse files
committed
changes to render group based on advanced query - part 2
1 parent 06458ac commit 71b9e80

File tree

1 file changed

+66
-87
lines changed

1 file changed

+66
-87
lines changed

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

Lines changed: 66 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const HRQuerySourceBase: React.FunctionComponent<HRQuerySourceProps> = (p
146146
}
147147
}, [children]);
148148

149-
function stringifyGroup(group: Group): string {
149+
function stringifyGroup(group: Group, isChild?: boolean, childIndex?: number, childrenLength?: number): string {
150150
let result = '(';
151151

152152
result += `${group.items.map((item, index) => {
@@ -161,20 +161,23 @@ export const HRQuerySourceBase: React.FunctionComponent<HRQuerySourceProps> = (p
161161
result += ')';
162162
}
163163

164-
if (group.andOr)
165-
{
164+
if (isChild) {
165+
if (childrenLength && childIndex !== childrenLength-1) result += ` ${group.andOr} `;
166+
}
167+
else {
166168
result += ` ${group.andOr} `;
167169
}
168170

169171
group.children.forEach((child, index) => {
170-
result += stringifyGroup(child);
172+
result += stringifyGroup(child, true, index, group.children.length);
171173
if (index < group.children.length - 1) {
172174
result += ' ';
173175
}
174176
});
175177

176178
if (group.children.length > 0) {
177179
result += ')';
180+
result += ` ${group.children[group.children.length-1].andOr} `;
178181
}
179182

180183
return result;
@@ -258,22 +261,66 @@ function findPartsOfString(string: string, substringArray: { currentSegment: str
258261
return output;
259262
}
260263

264+
function appendAndOr(a: { currentSegment: string; start: number; end: number; andOr: string; }[]) {
265+
a.forEach((segment, index) => {
266+
let modifiedSegment = segment.currentSegment.trim();
267+
let startWord = '';
268+
let endWord = '';
269+
270+
const lowerCaseSegment = modifiedSegment.toLowerCase();
271+
272+
if (lowerCaseSegment.startsWith('and ')) {
273+
startWord = 'And';
274+
modifiedSegment = modifiedSegment.substring(4).trim();
275+
} else if (lowerCaseSegment.startsWith('or ')) {
276+
startWord = 'Or';
277+
modifiedSegment = modifiedSegment.substring(3).trim();
278+
}
279+
280+
if (lowerCaseSegment.endsWith(' and')) {
281+
endWord = 'And';
282+
modifiedSegment = modifiedSegment.substring(0, modifiedSegment.length - 4).trim();
283+
} else if (lowerCaseSegment.endsWith(' or')) {
284+
endWord = 'Or';
285+
modifiedSegment = modifiedSegment.substring(0, modifiedSegment.length - 3).trim();
286+
}
287+
288+
if (lowerCaseSegment === 'and') {
289+
startWord = 'And';
290+
modifiedSegment = '';
291+
} else if (lowerCaseSegment === 'or') {
292+
startWord = 'Or';
293+
modifiedSegment = '';
294+
}
295+
296+
if (startWord !== '') {
297+
a[index-1].andOr = startWord;
298+
}
299+
if (endWord !== '') {
300+
a[index].andOr = endWord;
301+
}
302+
303+
a[index].currentSegment = modifiedSegment;
304+
305+
if (modifiedSegment === '') {
306+
a.splice(index, 1);
307+
} else {
308+
a[index].currentSegment = modifiedSegment;
309+
}
310+
});
311+
return a;
312+
}
261313

262314
function parseGroup(input: string): Group[] {
263315
const groups: Group[] = [];
264-
const subStrings: string[] = [];
265-
let subStringsWithMoreDetails: { currentSegment: string, start: number; end: number}[] = [];
316+
let subStrings: { currentSegment: string, start: number; end: number}[] = [];
266317
let depth = 0;
267318
let currentSegment = '';
268-
// let start: number;
269-
// let end: number;
270319
let operators: string[] = [];
271320

272321
input = input.trim();
273322
let start: number = 0;
274323
let end: number = input.length - 1;
275-
console.log("start", start);
276-
console.log("end", end);
277324

278325
for (let i = 0; i < input.length; i++) {
279326
const char = input[i];
@@ -288,10 +335,7 @@ function findPartsOfString(string: string, substringArray: { currentSegment: str
288335
depth--;
289336
if (depth === 0) {
290337
end = i;
291-
//groups.push(parseSegment(currentSegment));
292-
subStrings.push(currentSegment);
293-
// const index: number = input.indexOf(currentSegment);
294-
subStringsWithMoreDetails.push({ currentSegment, start, end});
338+
subStrings.push({ currentSegment, start, end});
295339
currentSegment = '';
296340
} else {
297341
currentSegment += char;
@@ -304,82 +348,17 @@ function findPartsOfString(string: string, substringArray: { currentSegment: str
304348
}
305349
}
306350

307-
// for (let i = 0; i < groups.length - 1; i++) {
308-
// groups[i].andOr = operators[i] || '';
309-
// }
310-
311-
// console.log("subStrings", subStrings);
312-
// console.log("subStringsWithMoreDetails", subStringsWithMoreDetails);
313-
// console.log("NEW", input.substr(63, 140));
314-
var a = findPartsOfString(input, subStringsWithMoreDetails);
315-
// console.log("a", a);
316-
// console.log("operators", operators);
317-
318-
319-
a.forEach((segment, index) => {
320-
let modifiedSegment = segment.currentSegment.trim();
321-
console.log(`Modified Segment before: ${modifiedSegment}`);
322-
let startWord = '';
323-
let endWord = '';
351+
var allParts = findPartsOfString(input, subStrings);
352+
var allPartsWithAndOr = appendAndOr(allParts);
324353

325-
const lowerCaseSegment = modifiedSegment.toLowerCase();
326-
327-
if (lowerCaseSegment.startsWith('and ')) {
328-
startWord = 'And';
329-
modifiedSegment = modifiedSegment.substring(4).trim();
330-
} else if (lowerCaseSegment.startsWith('or ')) {
331-
startWord = 'Or';
332-
modifiedSegment = modifiedSegment.substring(3).trim();
333-
}
334-
335-
if (lowerCaseSegment.endsWith(' and')) {
336-
endWord = 'And';
337-
modifiedSegment = modifiedSegment.substring(0, modifiedSegment.length - 4).trim();
338-
} else if (lowerCaseSegment.endsWith(' or')) {
339-
endWord = 'Or';
340-
modifiedSegment = modifiedSegment.substring(0, modifiedSegment.length - 3).trim();
341-
}
342-
343-
if (lowerCaseSegment === 'and') { // Additional condition for exact match
344-
startWord = 'And';
345-
modifiedSegment = '';
346-
} else if (lowerCaseSegment === 'or') { // Additional condition for exact match
347-
startWord = 'Or';
348-
modifiedSegment = '';
349-
}
350-
351-
if (startWord !== '') {
352-
console.log(`Start word: ${startWord}`);
353-
a[index-1].andOr = startWord;
354-
}
355-
if (endWord !== '') {
356-
console.log(`End word: ${endWord}`);
357-
a[index].andOr = endWord;
358-
}
359-
360-
console.log(`Modified Segment after: ${modifiedSegment}`);
361-
a[index].currentSegment = modifiedSegment;
362-
363-
if (modifiedSegment === '') {
364-
a.splice(index, 1);
365-
} else {
366-
a[index].currentSegment = modifiedSegment;
354+
allPartsWithAndOr.forEach((currentSegment, i) => {
355+
groups.push(parseSegment(currentSegment.currentSegment));
356+
if (groups[i].children.length > 0 && groups[i].children && groups[i].children[groups[i].children.length-1].andOr !== null) {
357+
groups[i].children[groups[i].children.length-1].andOr = allPartsWithAndOr[i].andOr;
367358
}
359+
else if (allPartsWithAndOr[i].andOr !== '') groups[i].andOr = allPartsWithAndOr[i].andOr;
368360

369361
});
370-
371-
// console.log("A", a);
372-
373-
a.forEach((currentSegment) => {
374-
groups.push(parseSegment(currentSegment.currentSegment));
375-
});
376-
377-
for (let i = 0; i < groups.length - 1; i++) {
378-
groups[i].andOr = a[i].andOr || '';
379-
}
380-
381-
console.log("groups", groups);
382-
383362
return groups;
384363
}
385364

@@ -398,7 +377,7 @@ function parseSegment(segment: string, groupOperator?: string): Group {
398377
let start = segment.indexOf('(');
399378
let end = segment.lastIndexOf(')');
400379
let remainingSegment = segment.substring(0, start) + segment.substring(end + 1);
401-
var match = remainingSegment.match(/\s*(Or|And)\s*$/i);
380+
var match = remainingSegment.match(/\b(Or|And)\b/i);
402381
var operator = match ? match[1] : null;
403382
remainingSegment = remainingSegment.replace(/\s*(Or|And)\s*/gi, '').trim();
404383

0 commit comments

Comments
 (0)