feat: add check list filter value

This commit is contained in:
Joel 2024-09-04 11:19:09 +08:00
parent b863dd7de2
commit f6d0fd9848
4 changed files with 27 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import { BlockEnum } from '../../types' import { BlockEnum } from '../../types'
import type { NodeDefault } from '../../types' import type { NodeDefault } from '../../types'
import { comparisonOperatorNotRequireValue } from '../if-else/utils'
import { type ListFilterNodeType, OrderBy } from './types' import { type ListFilterNodeType, OrderBy } from './types'
import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants' import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/app/components/workflow/constants'
const i18nPrefix = 'workflow.errorMsg' const i18nPrefix = 'workflow.errorMsg'
@ -30,10 +31,19 @@ const nodeDefault: NodeDefault<ListFilterNodeType> = {
}, },
checkValid(payload: ListFilterNodeType, t: any) { checkValid(payload: ListFilterNodeType, t: any) {
let errorMessages = '' let errorMessages = ''
const { variable } = payload const { variable, filter_by } = payload
if (!errorMessages && !variable?.length) if (!errorMessages && !variable?.length)
errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.assignedVariable') }) errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.inputVar') })
// Check filter condition
if (!errorMessages) {
if (!filter_by[0]?.comparison_operator)
errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.filterConditionComparisonOperator') })
if (!errorMessages && !comparisonOperatorNotRequireValue(filter_by[0]?.comparison_operator) && !filter_by[0]?.value)
errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.listFilter.filterConditionComparisonValue') })
}
return { return {
isValid: !errorMessages, isValid: !errorMessages,

View File

@ -4,7 +4,8 @@ import { useStoreApi } from 'reactflow'
import type { ValueSelector, Var } from '../../types' import type { ValueSelector, Var } from '../../types'
import { VarType } from '../../types' import { VarType } from '../../types'
import { getOperators } from '../if-else/utils' import { getOperators } from '../if-else/utils'
import type { Condition, Limit, ListFilterNodeType, OrderBy } from './types' import { OrderBy } from './types'
import type { Condition, Limit, ListFilterNodeType } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import { import {
useIsChatMode, useIsChatMode,
@ -80,10 +81,12 @@ const useConfig = (id: string, payload: ListFilterNodeType) => {
draft.var_type = varType draft.var_type = varType
draft.item_var_type = itemVarType draft.item_var_type = itemVarType
draft.filter_by = [{ draft.filter_by = [{
key: isFileArray ? 'name' : '', key: (isFileArray && !draft.filter_by[0].key) ? 'name' : '',
comparison_operator: getOperators(itemVarType, isFileArray ? { key: 'name' } : undefined)[0], comparison_operator: getOperators(itemVarType, isFileArray ? { key: 'name' } : undefined)[0],
value: '', value: '',
}] }]
if (isFileArray && draft.order_by.enabled && !draft.order_by.key)
draft.order_by.key = 'name'
}) })
setInputs(newInputs) setInputs(newInputs)
}, [getType, inputs, setInputs]) }, [getType, inputs, setInputs])
@ -110,9 +113,14 @@ const useConfig = (id: string, payload: ListFilterNodeType) => {
const handleOrderByEnabledChange = useCallback((enabled: boolean) => { const handleOrderByEnabledChange = useCallback((enabled: boolean) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
draft.order_by.enabled = enabled draft.order_by.enabled = enabled
if (enabled) {
draft.order_by.value = OrderBy.ASC
if (hasSubVariable && !draft.order_by.key)
draft.order_by.key = 'name'
}
}) })
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [hasSubVariable, inputs, setInputs])
const handleOrderByKeyChange = useCallback((key: string) => { const handleOrderByKeyChange = useCallback((key: string) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {

View File

@ -562,6 +562,8 @@ const translation = {
listFilter: { listFilter: {
inputVar: 'Input Variable', inputVar: 'Input Variable',
filterCondition: 'Filter Condition', filterCondition: 'Filter Condition',
filterConditionComparisonOperator: 'Filter Condition Comparison Operator',
filterConditionComparisonValue: 'Filter Condition value',
selectVariableKeyPlaceholder: 'Select sub variable key', selectVariableKeyPlaceholder: 'Select sub variable key',
limit: 'Limit', limit: 'Limit',
orderBy: 'Order by', orderBy: 'Order by',

View File

@ -562,6 +562,8 @@ const translation = {
listFilter: { listFilter: {
inputVar: '输入变量', inputVar: '输入变量',
filterCondition: '过滤条件', filterCondition: '过滤条件',
filterConditionComparisonOperator: '过滤条件比较操作符',
filterConditionComparisonValue: '过滤条件比较值',
selectVariableKeyPlaceholder: '选择子变量的 Key', selectVariableKeyPlaceholder: '选择子变量的 Key',
limit: '限制', limit: '限制',
orderBy: '排序', orderBy: '排序',