diff --git a/packages/editor-ui/src/components/FilterConditions/Condition.vue b/packages/editor-ui/src/components/FilterConditions/Condition.vue index 7f6978c3a0..42057b6b00 100644 --- a/packages/editor-ui/src/components/FilterConditions/Condition.vue +++ b/packages/editor-ui/src/components/FilterConditions/Condition.vue @@ -4,26 +4,21 @@ import InputTriple from '@/components/InputTriple/InputTriple.vue'; import ParameterInputFull from '@/components/ParameterInputFull.vue'; import ParameterIssues from '@/components/ParameterIssues.vue'; import { useI18n } from '@/composables/useI18n'; -import { resolveParameter } from '@/composables/useWorkflowHelpers'; import { DateTime } from 'luxon'; import { - FilterError, - executeFilterCondition, - validateFieldType, type FilterConditionValue, - type FilterOperatorType, type FilterOptionsValue, type INodeProperties, - type NodeParameterValue, } from 'n8n-workflow'; import { computed, ref } from 'vue'; import OperatorSelect from './OperatorSelect.vue'; -import { OPERATORS_BY_ID, type FilterOperatorId } from './constants'; -import type { FilterOperator } from './types'; -type ConditionResult = - | { status: 'resolve_error' } - | { status: 'validation_error'; error: string } - | { status: 'success'; result: boolean }; +import { type FilterOperatorId } from './constants'; +import { + getFilterOperator, + handleOperatorChange, + operatorTypeToNodeProperty, + resolveCondition, +} from './utils'; interface Props { path: string; @@ -41,6 +36,7 @@ const props = withDefaults(defineProps(), { canRemove: true, fixedLeftValue: false, readOnly: false, + index: 0, }); const emit = defineEmits<{ @@ -56,59 +52,11 @@ const operatorId = computed(() => { const { type, operation } = props.condition.operator; return `${type}:${operation}` as FilterOperatorId; }); -const operator = computed(() => OPERATORS_BY_ID[operatorId.value] as FilterOperator); +const operator = computed(() => getFilterOperator(operatorId.value)); -const operatorTypeToNodeProperty = ( - operatorType: FilterOperatorType, -): Pick => { - switch (operatorType) { - case 'boolean': - return { - type: 'options', - options: [ - { name: 'true', value: true }, - { name: 'false', value: false }, - ], - }; - case 'array': - case 'object': - case 'any': - return { type: 'string' }; - default: - return { type: operatorType }; - } -}; - -const conditionResult = computed(() => { - try { - const resolved = resolveParameter( - condition.value as unknown as NodeParameterValue, - ) as FilterConditionValue; - - if (resolved.leftValue === undefined || resolved.rightValue === undefined) { - return { status: 'resolve_error' }; - } - try { - const result = executeFilterCondition(resolved, props.options, { - index: props.index ?? 0, - errorFormat: 'inline', - }); - return { status: 'success', result }; - } catch (error) { - let errorMessage = i18n.baseText('parameterInput.error'); - - if (error instanceof FilterError) { - errorMessage = `${error.message}.\n${error.description}`; - } - return { - status: 'validation_error', - error: errorMessage, - }; - } - } catch (error) { - return { status: 'resolve_error' }; - } -}); +const conditionResult = computed(() => + resolveCondition({ condition: condition.value, options: props.options }), +); const allIssues = computed(() => { if (conditionResult.value.status === 'validation_error') { @@ -150,38 +98,14 @@ const onRightValueChange = (update: IUpdateInformation): void => { condition.value.rightValue = update.value; }; -const convertToType = (value: unknown, type: FilterOperatorType): unknown => { - if (type === 'any') return value; - - const fallback = type === 'boolean' ? false : value; - - return ( - validateFieldType('filter', condition.value.leftValue, type, { parseStrings: true }).newValue ?? - fallback - ); -}; - const onOperatorChange = (value: string): void => { - const newOperator = OPERATORS_BY_ID[value as FilterOperatorId] as FilterOperator; - const rightType = operator.value.rightType ?? operator.value.type; - const newRightType = newOperator.rightType ?? newOperator.type; - const leftTypeChanged = operator.value.type !== newOperator.type; - const rightTypeChanged = rightType !== newRightType; + const newOperator = getFilterOperator(value); - // Try to convert left & right values to operator type - if (leftTypeChanged) { - condition.value.leftValue = convertToType(condition.value.leftValue, newOperator.type); - } - if (rightTypeChanged && !newOperator.singleValue) { - condition.value.rightValue = convertToType(condition.value.rightValue, newRightType); - } + condition.value = handleOperatorChange({ + condition: condition.value, + newOperator, + }); - condition.value.operator = { - type: newOperator.type, - operation: newOperator.operation, - rightType: newOperator.rightType, - singleValue: newOperator.singleValue, - }; emit('update', condition.value); }; diff --git a/packages/editor-ui/src/components/FilterConditions/OperatorSelect.vue b/packages/editor-ui/src/components/FilterConditions/OperatorSelect.vue index 0b1cfd70e9..fe238fa5fb 100644 --- a/packages/editor-ui/src/components/FilterConditions/OperatorSelect.vue +++ b/packages/editor-ui/src/components/FilterConditions/OperatorSelect.vue @@ -1,8 +1,9 @@