|
import * as React from 'react'; |
|
import { useFormContext } from 'react-hook-form'; |
|
import set from 'lodash/set'; |
|
import isEqual from 'lodash/isEqual'; |
|
import { useQuery } from '@tanstack/react-query'; |
|
|
|
import api from 'helpers/api'; |
|
|
|
const variableRegExp = /({.*?})/; |
|
|
|
function computeArguments(args, getValues) { |
|
const initialValue = {}; |
|
|
|
return args.reduce((result, { name, value }) => { |
|
const isVariable = variableRegExp.test(value); |
|
|
|
if (isVariable) { |
|
const sanitizedFieldPath = value.replace(/{|}/g, ''); |
|
const computedValue = getValues(sanitizedFieldPath); |
|
|
|
if (computedValue === undefined || computedValue === '') |
|
throw new Error(`The ${sanitizedFieldPath} field is required.`); |
|
|
|
set(result, name, computedValue); |
|
return result; |
|
} |
|
|
|
set(result, name, value); |
|
return result; |
|
}, initialValue); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function useDynamicFields(stepId, schema) { |
|
const lastComputedVariables = React.useRef({}); |
|
const { getValues } = useFormContext(); |
|
const formValues = getValues(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const computedVariables = React.useMemo(() => { |
|
if (schema.type === 'dropdown' && schema.additionalFields) { |
|
try { |
|
const variables = computeArguments( |
|
schema.additionalFields.arguments, |
|
getValues, |
|
); |
|
|
|
if (isEqual(variables, lastComputedVariables.current)) { |
|
return lastComputedVariables.current; |
|
} |
|
lastComputedVariables.current = variables; |
|
return variables; |
|
} catch (err) { |
|
return null; |
|
} |
|
} |
|
|
|
return null; |
|
|
|
|
|
|
|
|
|
}, [schema, formValues, getValues]); |
|
|
|
const query = useQuery({ |
|
queryKey: ['steps', stepId, 'dynamicFields', computedVariables], |
|
queryFn: async ({ signal }) => { |
|
const { data } = await api.post( |
|
`/v1/steps/${stepId}/dynamic-fields`, |
|
{ |
|
dynamicFieldsKey: computedVariables.key, |
|
parameters: computedVariables.parameters, |
|
}, |
|
{ signal }, |
|
); |
|
|
|
return data; |
|
}, |
|
enabled: !!stepId && !!computedVariables, |
|
}); |
|
|
|
return query; |
|
} |
|
|
|
export default useDynamicFields; |
|
|