Skip to content

Commit

Permalink
WEG-125: fix layout conditions evaluation && and || (#2005)
Browse files Browse the repository at this point in the history
* WEG-125: fix layout conditions evaluation && and ||
  • Loading branch information
fsantaniello-heigvd authored Jan 9, 2025
1 parent 0129489 commit 7905b59
Showing 1 changed file with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ export function Script({
const { mode, label, description } = view;
const splitter = isScriptCondition(mode) ? operator : ';';

function getCurrentOrDefaultOperator(currentValue: string): Operator {
return currentValue && currentValue.indexOf(operators[1]) !== -1 ? operators[1]: operators[0];
}
function isSingleOperatorUsed(newValue: string) {
return newValue && newValue.indexOf(operators[0]) === -1 || newValue.indexOf(operators[1]) === -1;
}

const testScript = React.useCallback(
(value: string | IScript) => {
try {
Expand Down Expand Up @@ -137,19 +144,6 @@ export function Script({
[mode, onCodeChange, splitter],
);

const onSelectOperator = React.useCallback(
(operator: Operator) => {
setOperator(operator);
if (!error && !srcMode) {
// TODO : Something could be done when in src mode
if (statements !== null) {
onStatementsChange(statements);
}
}
},
[error, onStatementsChange, srcMode, statements],
);

React.useEffect(() => {
setError(errorMessage);
}, [errorMessage]);
Expand All @@ -165,6 +159,7 @@ export function Script({
value == null ? '' : typeof value === 'string' ? value : value.content;
if (script.current !== newValue) {
script.current = newValue;
setOperator(getCurrentOrDefaultOperator(script.current));
}
}, [value]);

Expand All @@ -176,20 +171,37 @@ export function Script({
setStatements([]);
} else {
const newStatements = parseCodeIntoExpressions(newValue, mode);
setStatements(oldStatements => {
if (isEqual(oldStatements, newStatements)) {
return oldStatements;
} else {
return newStatements;
}
});
if(isSingleOperatorUsed(newValue)) {
setStatements(oldStatements => {
if (isEqual(oldStatements, newStatements)) {
return oldStatements;
} else {
return newStatements;
}
});
setError(undefined);
}
else {
setSrcMode(true);
}
}
setError(undefined);
} catch (e) {
setError([handleError(e)]);
}
}, [mode, value]);

React.useEffect(() => {
if (!error && !srcMode) {
// TODO : Something could be done when in src mode
if (statements !== null) {
onStatementsChange(statements);
}
else {
setSrcMode(true);
}
}
}, [operator, error, srcMode]);

return (
<CommonViewContainer view={view} errorMessage={error}>
<Labeled label={label} description={description}>
Expand All @@ -216,7 +228,7 @@ export function Script({
<DropMenu
label={operator}
items={operators.map(o => ({ label: o, value: o }))}
onSelect={({ label }) => onSelectOperator(label)}
onSelect={({ label }) => setOperator(label)}
buttonClassName={secondaryButtonStyle}
/>
)}
Expand Down

0 comments on commit 7905b59

Please sign in to comment.