All checks were successful
Build and Deploy / remote-build-and-deploy (push) Successful in 11m4s
128 lines
4.0 KiB
TypeScript
128 lines
4.0 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { Modal, Box, Typography, TextField, Button, CircularProgress, Alert } from '@mui/material';
|
|
import type { OtroDestinoDto } from '../../../models/dtos/Distribucion/OtroDestinoDto';
|
|
import type { CreateOtroDestinoDto } from '../../../models/dtos/Distribucion/CreateOtroDestinoDto';
|
|
import type { UpdateOtroDestinoDto } from '../../../models/dtos/Distribucion/UpdateOtroDestinoDto';
|
|
|
|
const modalStyle = { /* ... (mismo estilo) ... */
|
|
position: 'absolute' as 'absolute',
|
|
top: '50%',
|
|
left: '50%',
|
|
transform: 'translate(-50%, -50%)',
|
|
width: 400,
|
|
bgcolor: 'background.paper',
|
|
border: '2px solid #000',
|
|
boxShadow: 24,
|
|
p: 4,
|
|
};
|
|
|
|
interface OtroDestinoFormModalProps {
|
|
open: boolean;
|
|
onClose: () => void;
|
|
onSubmit: (data: CreateOtroDestinoDto | (UpdateOtroDestinoDto & { idDestino: number })) => Promise<void>;
|
|
initialData?: OtroDestinoDto | null;
|
|
errorMessage?: string | null;
|
|
clearErrorMessage: () => void;
|
|
}
|
|
|
|
const OtroDestinoFormModal: React.FC<OtroDestinoFormModalProps> = ({
|
|
open,
|
|
onClose,
|
|
onSubmit,
|
|
initialData,
|
|
errorMessage,
|
|
clearErrorMessage
|
|
}) => {
|
|
const [nombre, setNombre] = useState('');
|
|
const [obs, setObs] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [localErrorNombre, setLocalErrorNombre] = useState<string | null>(null);
|
|
|
|
const isEditing = Boolean(initialData);
|
|
|
|
useEffect(() => {
|
|
if (open) {
|
|
setNombre(initialData?.nombre || '');
|
|
setObs(initialData?.obs || '');
|
|
setLocalErrorNombre(null);
|
|
clearErrorMessage();
|
|
}
|
|
}, [open, initialData, clearErrorMessage]);
|
|
|
|
const handleInputChange = () => {
|
|
if (localErrorNombre) setLocalErrorNombre(null);
|
|
if (errorMessage) clearErrorMessage();
|
|
};
|
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
setLocalErrorNombre(null);
|
|
clearErrorMessage();
|
|
|
|
if (!nombre.trim()) {
|
|
setLocalErrorNombre('El nombre es obligatorio.');
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
const dataToSubmit = { nombre, obs: obs || undefined };
|
|
|
|
if (isEditing && initialData) {
|
|
await onSubmit({ ...dataToSubmit, idDestino: initialData.idDestino });
|
|
} else {
|
|
await onSubmit(dataToSubmit as CreateOtroDestinoDto);
|
|
}
|
|
onClose();
|
|
} catch (error: any) {
|
|
console.error("Error en submit de OtroDestinoFormModal:", error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Modal open={open} onClose={onClose}>
|
|
<Box sx={modalStyle}>
|
|
<Typography variant="h6" component="h2">
|
|
{isEditing ? 'Editar Otro Destino' : 'Agregar Nuevo Destino'}
|
|
</Typography>
|
|
<Box component="form" onSubmit={handleSubmit} sx={{ mt: 2 }}>
|
|
<TextField
|
|
label="Nombre"
|
|
fullWidth
|
|
required
|
|
value={nombre}
|
|
onChange={(e) => { setNombre(e.target.value); handleInputChange(); }}
|
|
margin="normal"
|
|
error={!!localErrorNombre}
|
|
helperText={localErrorNombre || ''}
|
|
disabled={loading}
|
|
autoFocus
|
|
/>
|
|
<TextField
|
|
label="Observación (Opcional)"
|
|
fullWidth
|
|
value={obs}
|
|
onChange={(e) => setObs(e.target.value)}
|
|
margin="normal"
|
|
multiline
|
|
rows={3}
|
|
disabled={loading}
|
|
/>
|
|
{errorMessage && <Alert severity="error" sx={{ mt: 1 }}>{errorMessage}</Alert>}
|
|
<Box sx={{ mt: 3, display: 'flex', justifyContent: 'flex-end', gap: 1 }}>
|
|
<Button onClick={onClose} color="secondary" disabled={loading}>
|
|
Cancelar
|
|
</Button>
|
|
<Button type="submit" variant="contained" disabled={loading}>
|
|
{loading ? <CircularProgress size={24} /> : (isEditing ? 'Guardar Cambios' : 'Agregar')}
|
|
</Button>
|
|
</Box>
|
|
</Box>
|
|
</Box>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default OtroDestinoFormModal; |