|
| 1 | +import { BlockButton } from '@/components/ui/button'; |
| 2 | +import { Form } from '@/components/ui/form'; |
| 3 | +import { Separator } from '@/components/ui/separator'; |
| 4 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 5 | +import { memo } from 'react'; |
| 6 | +import { useFieldArray, useForm } from 'react-hook-form'; |
| 7 | +import { useTranslation } from 'react-i18next'; |
| 8 | +import { z } from 'zod'; |
| 9 | +import { initialDataOperationsValues } from '../../constant'; |
| 10 | +import { useFormValues } from '../../hooks/use-form-values'; |
| 11 | +import { useWatchFormChange } from '../../hooks/use-watch-form-change'; |
| 12 | +import { INextOperatorForm } from '../../interface'; |
| 13 | +import { buildOutputList } from '../../utils/build-output-list'; |
| 14 | +import { FormWrapper } from '../components/form-wrapper'; |
| 15 | +import { Output } from '../components/output'; |
| 16 | +import { DynamicGroupVariable } from './dynamic-group-variable'; |
| 17 | + |
| 18 | +export const RetrievalPartialSchema = { |
| 19 | + groups: z.array( |
| 20 | + z.object({ |
| 21 | + group_name: z.string(), |
| 22 | + variables: z.array(z.object({ value: z.string().optional() })), |
| 23 | + }), |
| 24 | + ), |
| 25 | + operations: z.string(), |
| 26 | +}; |
| 27 | + |
| 28 | +export const FormSchema = z.object(RetrievalPartialSchema); |
| 29 | + |
| 30 | +export type DataOperationsFormSchemaType = z.infer<typeof FormSchema>; |
| 31 | + |
| 32 | +const outputList = buildOutputList(initialDataOperationsValues.outputs); |
| 33 | + |
| 34 | +function VariableAggregatorForm({ node }: INextOperatorForm) { |
| 35 | + const { t } = useTranslation(); |
| 36 | + |
| 37 | + const defaultValues = useFormValues(initialDataOperationsValues, node); |
| 38 | + |
| 39 | + const form = useForm<DataOperationsFormSchemaType>({ |
| 40 | + defaultValues: defaultValues, |
| 41 | + mode: 'onChange', |
| 42 | + resolver: zodResolver(FormSchema), |
| 43 | + shouldUnregister: true, |
| 44 | + }); |
| 45 | + |
| 46 | + const { fields, remove, append } = useFieldArray({ |
| 47 | + name: 'groups', |
| 48 | + control: form.control, |
| 49 | + }); |
| 50 | + |
| 51 | + useWatchFormChange(node?.id, form, true); |
| 52 | + |
| 53 | + return ( |
| 54 | + <Form {...form}> |
| 55 | + <FormWrapper> |
| 56 | + <section className="divide-y"> |
| 57 | + {fields.map((field, idx) => ( |
| 58 | + <DynamicGroupVariable |
| 59 | + key={field.id} |
| 60 | + name={`groups.${idx}`} |
| 61 | + parentIndex={idx} |
| 62 | + removeParent={remove} |
| 63 | + ></DynamicGroupVariable> |
| 64 | + ))} |
| 65 | + </section> |
| 66 | + <BlockButton |
| 67 | + onClick={() => |
| 68 | + append({ group_name: `Group ${fields.length}`, variables: [] }) |
| 69 | + } |
| 70 | + > |
| 71 | + {t('common.add')} |
| 72 | + </BlockButton> |
| 73 | + <Separator /> |
| 74 | + |
| 75 | + <Output list={outputList} isFormRequired></Output> |
| 76 | + </FormWrapper> |
| 77 | + </Form> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +export default memo(VariableAggregatorForm); |
0 commit comments