import * as React from 'react'; import Hook from '../../hook'; import { dataMessagesEN as en } from '../(translation)/en'; import { dataMessagesES as es } from '../(translation)/es'; import { Button, Typography } from '@mui/material'; import { DataColumn } from '../types/column.type'; import { DataColumnExtra } from '../types/table.type'; import { DataFilterType, DataOption } from '../types/filter.type'; import { NumberEditor } from './body.number'; import { DateEditor } from './body.date'; import { SetEditor } from './body.auto'; import { BooleanEditor } from './body.boolean'; import { TextEditor } from './body.text'; import { Dialog } from '../../dialogs/dialog'; export interface DataFromDialogInput { caption: string, columns: DataColumn[], changes?: any, options?: (column: DataColumn, inputValue: string, currentValue: any) => Promise, } export interface DataFromDialogOutput { changes?: any, } export const DataFromDialog = Hook.Dialog(({ data, open, onClose }) => { const [located] = Hook.useLocalization({ en, es }); const [columns, setColumns] = React.useState([]); const [changes, setChanges] = React.useState<{ [key: string]: any }>({}); React.useEffect(() => { const newColumns: DataColumnExtra[] = []; const newChanges: { [key: string]: any } = {}; if (data && data.columns) { data.columns.forEach((column: any) => { if (!column.const) { newColumns.push(column); } }) newColumns.forEach(column => { newChanges[column.name] = null; }) setColumns(newColumns); setChanges(newChanges); } }, [data]); const handleLoadOptions = (column: DataColumn) => async ([inputValue, currentValue]: [any, any]) => { return data && data.options ? await data.options(column, inputValue, currentValue) : [] } const handleTextChanged = (column: DataColumn) => (value: string) => { setChanges({ ...changes, [column.name]: !value ? null : value }); } const handleNumberChanged = (column: DataColumn) => (value: number) => { setChanges({ ...changes, [column.name]: value }); } const handleDateChanged = (column: DataColumn) => (value: Date) => { setChanges({ ...changes, [column.name]: value }); } const handleSetChanged = (column: DataColumn) => (value: DataFilterType) => { setChanges({ ...changes, [column.name]: value }); } const handleBoolChanged = (column: DataColumn) => (value: boolean) => { setChanges({ ...changes, [column.name]: value }); } const handleClose = (accept: boolean) => { const newChanges: any = {}; Object.keys(changes).forEach(key => { if (changes[key] !== null && changes[key] !== undefined) { newChanges[key] = changes[key]; } }) if (onClose) { onClose({ data: { changes: newChanges }, rejected: !accept }); } } const editors = columns.filter(x => x.type !== 'none').map(column => { if (column.type === 'number') { return ( {column.label ? column.label : column.name} { }} onChange={handleNumberChanged(column)} /> ) } if (column.type === 'date') { return ( {column.label ? column.label : column.name} { }} onChange={handleDateChanged(column)} /> ) } if (column.type === 'set') { return ( {column.label ? column.label : column.name} { }} options={handleLoadOptions(column)} onChange={handleSetChanged(column)} /> ) } if (column.type === 'boolean') { return ( {column.label ? column.label : column.name} { }} onChange={handleBoolChanged(column)} /> ) } return ( {column.label ? column.label : column.name} { }} onChange={handleTextChanged(column)} /> ) }); return ( handleClose(false)} actions={ }> {editors}
); })