🐛 (conditions) Fix negative comparisons
This commit is contained in:
@@ -32,6 +32,7 @@ export const ButtonsItemSettings = ({ item, onSettingsChange }: Props) => {
|
|||||||
<Stack spacing={4}>
|
<Stack spacing={4}>
|
||||||
<SwitchWithRelatedSettings
|
<SwitchWithRelatedSettings
|
||||||
label="Display condition"
|
label="Display condition"
|
||||||
|
moreInfoContent="Only display this item if a condition is met."
|
||||||
initialValue={item.displayCondition?.isEnabled ?? false}
|
initialValue={item.displayCondition?.isEnabled ?? false}
|
||||||
onCheckChange={updateIsDisplayConditionEnabled}
|
onCheckChange={updateIsDisplayConditionEnabled}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { findUniqueVariableValue } from '@/features/variables/findUniqueVariableValue'
|
import { findUniqueVariableValue } from '@/features/variables/findUniqueVariableValue'
|
||||||
|
import { parseVariables } from '@/features/variables/parseVariables'
|
||||||
import { isNotDefined, isDefined } from '@typebot.io/lib'
|
import { isNotDefined, isDefined } from '@typebot.io/lib'
|
||||||
import {
|
import {
|
||||||
Comparison,
|
Comparison,
|
||||||
@@ -7,7 +8,6 @@ import {
|
|||||||
LogicalOperator,
|
LogicalOperator,
|
||||||
Variable,
|
Variable,
|
||||||
} from '@typebot.io/schemas'
|
} from '@typebot.io/schemas'
|
||||||
import { parseVariables } from 'bot-engine'
|
|
||||||
|
|
||||||
export const executeCondition =
|
export const executeCondition =
|
||||||
(variables: Variable[]) =>
|
(variables: Variable[]) =>
|
||||||
@@ -20,8 +20,9 @@ const executeComparison =
|
|||||||
(variables: Variable[]) =>
|
(variables: Variable[]) =>
|
||||||
(comparison: Comparison): boolean => {
|
(comparison: Comparison): boolean => {
|
||||||
if (!comparison?.variableId) return false
|
if (!comparison?.variableId) return false
|
||||||
const inputValue =
|
const inputValue = variables.find(
|
||||||
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
|
(v) => v.id === comparison.variableId
|
||||||
|
)?.value
|
||||||
const value =
|
const value =
|
||||||
findUniqueVariableValue(variables)(comparison.value) ??
|
findUniqueVariableValue(variables)(comparison.value) ??
|
||||||
parseVariables(variables)(comparison.value)
|
parseVariables(variables)(comparison.value)
|
||||||
@@ -37,18 +38,19 @@ const executeComparison =
|
|||||||
}
|
}
|
||||||
case ComparisonOperators.NOT_CONTAINS: {
|
case ComparisonOperators.NOT_CONTAINS: {
|
||||||
const notContains = (a: string | null, b: string | null) => {
|
const notContains = (a: string | null, b: string | null) => {
|
||||||
if (b === '' || !b || !a) return false
|
if (b === '' || !b || !a) return true
|
||||||
return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
|
return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
|
||||||
}
|
}
|
||||||
return compare(notContains, inputValue, value)
|
return compare(notContains, inputValue, value, true)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.EQUAL: {
|
case ComparisonOperators.EQUAL: {
|
||||||
return compare((a, b) => a === b, inputValue, value)
|
return compare((a, b) => a === b, inputValue, value)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.NOT_EQUAL: {
|
case ComparisonOperators.NOT_EQUAL: {
|
||||||
return compare((a, b) => a !== b, inputValue, value)
|
return compare((a, b) => a !== b, inputValue, value, true)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.GREATER: {
|
case ComparisonOperators.GREATER: {
|
||||||
|
if (isNotDefined(inputValue)) return false
|
||||||
if (typeof inputValue === 'string') {
|
if (typeof inputValue === 'string') {
|
||||||
if (typeof value === 'string')
|
if (typeof value === 'string')
|
||||||
return parseFloat(inputValue) > parseFloat(value)
|
return parseFloat(inputValue) > parseFloat(value)
|
||||||
@@ -59,6 +61,7 @@ const executeComparison =
|
|||||||
return inputValue.length > value.length
|
return inputValue.length > value.length
|
||||||
}
|
}
|
||||||
case ComparisonOperators.LESS: {
|
case ComparisonOperators.LESS: {
|
||||||
|
if (isNotDefined(inputValue)) return false
|
||||||
if (typeof inputValue === 'string') {
|
if (typeof inputValue === 'string') {
|
||||||
if (typeof value === 'string')
|
if (typeof value === 'string')
|
||||||
return parseFloat(inputValue) < parseFloat(value)
|
return parseFloat(inputValue) < parseFloat(value)
|
||||||
@@ -92,15 +95,24 @@ const executeComparison =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const compare = (
|
const compare = (
|
||||||
func: (a: string | null, b: string | null) => boolean,
|
compareStrings: (a: string | null, b: string | null) => boolean,
|
||||||
a: Variable['value'],
|
a: Variable['value'],
|
||||||
b: Variable['value']
|
b: Variable['value'],
|
||||||
|
defaultReturnValue = false
|
||||||
): boolean => {
|
): boolean => {
|
||||||
if (!a || !b) return false
|
if (!a || !b) return defaultReturnValue
|
||||||
if (typeof a === 'string') {
|
if (typeof a === 'string') {
|
||||||
if (typeof b === 'string') return func(a, b)
|
if (typeof b === 'string') return compareStrings(a, b)
|
||||||
return b.some((b) => func(a, b))
|
return defaultReturnValue === true
|
||||||
|
? b.every((b) => compareStrings(a, b))
|
||||||
|
: b.some((b) => compareStrings(a, b))
|
||||||
}
|
}
|
||||||
if (typeof b === 'string') return a.some((a) => func(a, b))
|
if (typeof b === 'string') {
|
||||||
return a.some((a) => b.some((b) => func(a, b)))
|
return defaultReturnValue === true
|
||||||
|
? a.every((a) => compareStrings(a, b))
|
||||||
|
: a.some((a) => compareStrings(a, b))
|
||||||
|
}
|
||||||
|
if (defaultReturnValue === true)
|
||||||
|
return a.every((a) => b.every((b) => compareStrings(a, b)))
|
||||||
|
return a.some((a) => b.some((b) => compareStrings(a, b)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user