import { useEffect, useState } from 'react' import ActionBar from 'components/interfaces/TableGridEditor/SidePanelEditor/ActionBar' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import ShimmeringLoader from 'components/ui/ShimmeringLoader' import { useSchemasQuery } from 'data/database/schemas-query' import { Form, IconDatabase, IconPlus, Input, Listbox, Modal, SidePanel } from 'ui' import WrapperDynamicColumns from './WrapperDynamicColumns' import type { Table, TableOption } from './Wrappers.types' import { makeValidateRequired } from './Wrappers.utils' 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 }) => { if (option.type === 'select') { return ( {[ ...(!option.required ? [ --- , ] : []), ...option.options.map((subOption) => ( {subOption.label} )), ]} ) } return ( ) } const TableForm = ({ table, onSubmit, initialData, }: { table: Table onSubmit: OnSubmitFn initialData: any }) => { const { project } = useProjectContext() const { data: schemas, isLoading, isSuccess, } = useSchemasQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) 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 ?? [], ...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 }, ...(table.availableColumns ? [] : [{ name: 'columns.name', required: true }]), ]) return (
{({ errors, values, setFieldValue }: any) => { return (
{isLoading && } {isSuccess && ( } > Create a new schema {(schemas ?? [])?.map((schema) => { return ( } > {schema.name} ) })} )} {values.schema === 'custom' && ( )} {requiredOptions.map((option) => (
) }}
) }