diff --git a/frontend/src/_components/DynamicForm.jsx b/frontend/src/_components/DynamicForm.jsx index 32e8324036..504ab83cc2 100644 --- a/frontend/src/_components/DynamicForm.jsx +++ b/frontend/src/_components/DynamicForm.jsx @@ -27,6 +27,8 @@ const DynamicForm = ({ optionsChanged, queryName, }) => { + const [computedProps, setComputedProps] = React.useState({}); + // if(schema.properties) todo add empty check React.useLayoutEffect(() => { if (!isEditMode || isEmpty(options)) { @@ -35,6 +37,34 @@ const DynamicForm = ({ // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + React.useEffect(() => { + const { properties } = schema; + if (isEmpty(properties)) return null; + + let fields = {}; + let encrpytedFieldsProps = {}; + const flipComponentDropdown = find(properties, ['type', 'dropdown-component-flip']); + + if (flipComponentDropdown) { + const selector = options?.[flipComponentDropdown?.key]?.value; + fields = { ...flipComponentDropdown?.commonFields, ...properties[selector] }; + } else { + fields = { ...properties }; + } + + Object.keys(fields).map((key) => { + const { type, encrypted } = fields[key]; + if ((type === 'password' || encrypted) && !(key in computedProps)) { + //Editable encrypted fields only if datasource doesn't exists + encrpytedFieldsProps[key] = { + disabled: !!selectedDataSource?.id, + }; + } + }); + setComputedProps({ ...computedProps, ...encrpytedFieldsProps }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [options]); + const getElement = (type) => { switch (type) { case 'password': @@ -97,7 +127,7 @@ const DynamicForm = ({ case 'textarea': return { type, - placeholder: description, + placeholder: options?.[key]?.encrypted ? '**************' : description, className: `form-control${handleToggle(controller)}`, value: options?.[key]?.value, ...(type === 'textarea' && { rows: rows }), @@ -207,6 +237,26 @@ const DynamicForm = ({ return flipComponentDropdown; } + const handleEncryptedFieldsToggle = (event, field) => { + const isEditing = computedProps[field]['disabled']; + setComputedProps({ + ...computedProps, + [field]: { + ...computedProps[field], + disabled: !isEditing, + }, + }); + + if (isEditing) { + optionchanged(field, ''); + } else { + //Send old field value if editing mode disabled for encrypted fields + const newOptions = { ...options }; + const oldFieldValue = selectedDataSource?.['options']?.[field]; + optionsChanged({ ...newOptions, [field]: oldFieldValue }); + } + }; + return (
{Object.keys(obj).map((key) => { @@ -216,14 +266,30 @@ const DynamicForm = ({ return (
- {label && ( -
diff --git a/frontend/src/_ui/Toggle/index.js b/frontend/src/_ui/Toggle/index.js index f059593713..ac6f3f32db 100644 --- a/frontend/src/_ui/Toggle/index.js +++ b/frontend/src/_ui/Toggle/index.js @@ -1,7 +1,7 @@ import React from 'react'; -export default ({ defaultChecked, onChange, checked = false }) => { +export default ({ defaultChecked, onChange, checked = false, classes = {} }) => { return ( -