'use client'; import { useEffect, useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, } from '../../utils'; import { Typography } from '../../data-display'; import { Button, TextField, Select, Checkbox, FormControlLabel, } from '../../inputs'; export type ColumnInfo = { column_name: string; }; export type ColumnDialogMode = 'add' | 'modify' | 'drop'; export type ColumnDialogProps = { open: boolean; mode: ColumnDialogMode; tableName: string; columns?: ColumnInfo[]; onClose: () => void; onSubmit: (data: Record) => Promise; dataTypes: string[]; testId?: string; }; /** * ColumnDialog - A dialog for managing database columns. * Supports adding, modifying, and dropping columns. */ export function ColumnDialog({ open, mode, tableName, columns = [], onClose, onSubmit, dataTypes, testId, }: ColumnDialogProps) { const [columnName, setColumnName] = useState(''); const [columnType, setColumnType] = useState('VARCHAR'); const [nullable, setNullable] = useState(true); const [defaultValue, setDefaultValue] = useState(''); const [selectedColumn, setSelectedColumn] = useState(''); const [loading, setLoading] = useState(false); useEffect(() => { if (!open) { // Reset form when dialog closes setColumnName(''); setColumnType('VARCHAR'); setNullable(true); setDefaultValue(''); setSelectedColumn(''); } }, [open]); const handleSubmit = async () => { setLoading(true); try { const data: Record = {}; if (mode === 'add') { data.columnName = columnName; data.dataType = columnType; data.nullable = nullable; if (defaultValue) data.defaultValue = defaultValue; } else if (mode === 'modify') { data.columnName = selectedColumn; data.newType = columnType; data.nullable = nullable; } else if (mode === 'drop') { data.columnName = selectedColumn; } await onSubmit(data); onClose(); } finally { setLoading(false); } }; const getTitle = () => { switch (mode) { case 'add': return `Add Column to ${tableName}`; case 'modify': return `Modify Column in ${tableName}`; case 'drop': return `Drop Column from ${tableName}`; default: return 'Column Operation'; } }; const isFormValid = () => { if (mode === 'add') { return columnName.trim() && columnType; } return selectedColumn.trim(); }; return ( {getTitle()} {mode === 'drop' && ( Warning: This will permanently delete the column and all its data! )} {mode === 'add' ? ( <> setColumnName(e.target.value)} sx={{ mt: 2, mb: 2 }} /> setNullable(e.target.checked)} /> } label="Nullable" sx={{ mb: 2 }} /> setDefaultValue(e.target.value)} /> ) : ( <> {mode === 'modify' && selectedColumn && ( <> setNullable(e.target.checked)} /> } label="Nullable" /> )} )} ); } export default ColumnDialog;