import { useEffect, useState } from 'react' import { Form, IconDatabase, Input, Listbox, SidePanel, Modal, IconPlus } from 'ui' import { useStore } from 'hooks' import { Table, TableOption } from './Wrappers.types' import { makeValidateRequired } from './Wrappers.utils' import ActionBar from 'components/interfaces/TableGridEditor/SidePanelEditor/ActionBar' export type WrapperTableEditorProps = { visible: boolean onCancel: () => void onSave: (values: any) => void tables: Table[] initialData: any } type OnSubmitFn = (values: any, { resetForm }: { resetForm: () => void }) => void const WrapperTableEditor = ({ visible, onCancel, onSave, tables, initialData, }: WrapperTableEditorProps) => { const [selectedTableIndex, setSelectedTableIndex] = useState('') useEffect(() => { if (initialData) { setSelectedTableIndex(String(initialData.index)) } }, [initialData]) const selectedTable = selectedTableIndex === '' ? undefined : tables[parseInt(selectedTableIndex)] const handleCancel = () => { setSelectedTableIndex('') onCancel() } const onSubmit: OnSubmitFn = (values, { resetForm }) => { onSave({ ...values, index: parseInt(selectedTableIndex), schema_name: values.schema === 'custom' ? values.schema_name : values.schema, is_new_schema: values.schema === 'custom', }) resetForm() setSelectedTableIndex('') } return ( Edit foreign table} customFooter={ } >
setSelectedTableIndex(value)} > --- {tables.map((table, i) => { return (

{table.label}

{table.description}

) })}
{selectedTable && ( )}
) } export default WrapperTableEditor const Option = ({ option }: { option: TableOption }) => { return ( ) } const TableForm = ({ table, onSubmit, initialData, }: { table: Table onSubmit: OnSubmitFn initialData: any }) => { const { meta } = useStore() const schemas = meta.schemas.list() const requiredOptions = table.options.filter((option) => option.editable && option.required && !option.defaultValue) ?? [] const optionalOptions = table.options.filter( (option) => option.editable && (!option.required || option.defaultValue) ) ?? [] const initialValues = initialData ?? { table_name: '', columns: table.availableColumns.map((column) => column.name), ...Object.fromEntries(table.options.map((option) => [option.name, option.defaultValue ?? ''])), schema: 'public', schema_name: '', } const validate = makeValidateRequired([ ...table.options, { name: 'table_name', required: true }, { name: 'columns', required: true }, ]) return (
{({ errors, values, resetForm }: any) => { return (
} > Create a new schema {/* @ts-ignore */} {schemas.map((schema: PostgresSchema) => { return ( } > {schema.name} ) })} {values.schema === 'custom' && ( )} {requiredOptions.map((option) => (
) }}
) }