feat: enhance AlumnosVista and DiplomasVista with new data handling for inyecciones and pildoras, including editing capabilities and improved UI for displaying types and formations
This commit is contained in:
parent
010ac8fa6d
commit
a744fb083d
|
@ -33,6 +33,10 @@ export default function AlumnosVista() {
|
||||||
const [modalMensaje, setModalMensaje] = useState("");
|
const [modalMensaje, setModalMensaje] = useState("");
|
||||||
const [nuevoCurso, setNuevoCurso] = useState("");
|
const [nuevoCurso, setNuevoCurso] = useState("");
|
||||||
const [cursos, setCursos] = useState([]);
|
const [cursos, setCursos] = useState([]);
|
||||||
|
const [nuevoTipo, setNuevoTipo] = useState("");
|
||||||
|
const [nuevaFormacion, setNuevaFormacion] = useState("");
|
||||||
|
const [inyecciones, setInyecciones] = useState([]);
|
||||||
|
const [pildoras, setPildoras] = useState([]);
|
||||||
|
|
||||||
// Estado para confirmación de eliminación
|
// Estado para confirmación de eliminación
|
||||||
const [confirmarEliminar, setConfirmarEliminar] = useState(false);
|
const [confirmarEliminar, setConfirmarEliminar] = useState(false);
|
||||||
|
@ -41,6 +45,14 @@ export default function AlumnosVista() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
cargarAlumnos();
|
cargarAlumnos();
|
||||||
cargarCursos();
|
cargarCursos();
|
||||||
|
supabaseClient
|
||||||
|
.from("pildoras")
|
||||||
|
.select("id, nombre")
|
||||||
|
.then(({ data }) => setPildoras(data || []));
|
||||||
|
supabaseClient
|
||||||
|
.from("inyeccion")
|
||||||
|
.select("id, nombre")
|
||||||
|
.then(({ data }) => setInyecciones(data || []));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const cargarCursos = async () => {
|
const cargarCursos = async () => {
|
||||||
|
@ -55,13 +67,21 @@ export default function AlumnosVista() {
|
||||||
const cargarAlumnos = async () => {
|
const cargarAlumnos = async () => {
|
||||||
const { data, error } = await supabaseClient
|
const { data, error } = await supabaseClient
|
||||||
.from("alumno")
|
.from("alumno")
|
||||||
.select("id, nombre, correo, telefono, curso_id, curso(nombre)")
|
.select(`
|
||||||
|
id,
|
||||||
|
nombre,
|
||||||
|
correo,
|
||||||
|
telefono,
|
||||||
|
tipo_formacion,
|
||||||
|
curso_id,
|
||||||
|
curso(id, nombre),
|
||||||
|
inyeccion_id,
|
||||||
|
inyeccion(id, nombre),
|
||||||
|
pildoras_id,
|
||||||
|
pildoras(id, nombre)
|
||||||
|
`)
|
||||||
.order("id", { ascending: true });
|
.order("id", { ascending: true });
|
||||||
if (error) {
|
setAlumnos(data || []);
|
||||||
console.error("Error al cargar alumnos:", error.message);
|
|
||||||
} else {
|
|
||||||
setAlumnos(data);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
@ -83,10 +103,15 @@ export default function AlumnosVista() {
|
||||||
// Iniciar edición
|
// Iniciar edición
|
||||||
const iniciarEdicion = (alumno) => {
|
const iniciarEdicion = (alumno) => {
|
||||||
setAlumnoEditando(alumno.id);
|
setAlumnoEditando(alumno.id);
|
||||||
setValue("nombre", alumno.nombre);
|
setNuevoNombre(alumno.nombre || "");
|
||||||
setValue("correo", alumno.correo);
|
setNuevoCorreo(alumno.correo || "");
|
||||||
setValue("telefono", alumno.telefono);
|
setNuevoNumero(alumno.telefono || "");
|
||||||
setValue("cursoSeleccionado", alumno.curso_id?.toString() || "");
|
setNuevoTipo(alumno.tipo_formacion || "");
|
||||||
|
// Detecta la formación actual según el tipo
|
||||||
|
if (alumno.tipo_formacion === "curso") setNuevaFormacion(alumno.curso_id ? String(alumno.curso_id) : "");
|
||||||
|
else if (alumno.tipo_formacion === "inyeccion") setNuevaFormacion(alumno.inyeccion_id ? String(alumno.inyeccion_id) : "");
|
||||||
|
else if (alumno.tipo_formacion === "pildora") setNuevaFormacion(alumno.pildoras_id ? String(alumno.pildoras_id) : "");
|
||||||
|
else setNuevaFormacion("");
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cancelar edición
|
// Cancelar edición
|
||||||
|
@ -96,25 +121,36 @@ export default function AlumnosVista() {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Guardar cambios
|
// Guardar cambios
|
||||||
const guardarEdicion = async (data) => {
|
const guardarEdicion = async (id) => {
|
||||||
|
// Prepara el objeto de actualización
|
||||||
|
let updateObj = {
|
||||||
|
tipo_formacion: nuevoTipo,
|
||||||
|
curso_id: null,
|
||||||
|
inyeccion_id: null,
|
||||||
|
pildoras_id: null,
|
||||||
|
nombre: nuevoNombre,
|
||||||
|
correo: nuevoCorreo,
|
||||||
|
telefono: nuevoNumero,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (nuevoTipo === "curso") updateObj.curso_id = Number(nuevaFormacion);
|
||||||
|
if (nuevoTipo === "inyeccion") updateObj.inyeccion_id = Number(nuevaFormacion);
|
||||||
|
if (nuevoTipo === "pildora") updateObj.pildoras_id = Number(nuevaFormacion);
|
||||||
|
|
||||||
const { error } = await supabaseClient
|
const { error } = await supabaseClient
|
||||||
.from("alumno")
|
.from("alumno")
|
||||||
.update({
|
.update(updateObj)
|
||||||
nombre: data.nombre,
|
.eq("id", id);
|
||||||
correo: data.correo,
|
|
||||||
telefono: data.telefono,
|
|
||||||
curso_id: data.cursoSeleccionado,
|
|
||||||
})
|
|
||||||
.eq("id", alumnoEditando);
|
|
||||||
|
|
||||||
if (error) {
|
if (!error) {
|
||||||
setModalMensaje("Error al actualizar el alumno");
|
setModalMensaje("Alumno actualizado correctamente");
|
||||||
|
setMostrarModal(true);
|
||||||
|
setAlumnoEditando(null);
|
||||||
|
await cargarAlumnos(); // <--- Refresca la tabla
|
||||||
} else {
|
} else {
|
||||||
setModalMensaje("Alumno actualizado exitosamente");
|
setModalMensaje("Error al actualizar el alumno");
|
||||||
await cargarAlumnos();
|
setMostrarModal(true);
|
||||||
cancelarEdicion();
|
|
||||||
}
|
}
|
||||||
setMostrarModal(true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Confirmar eliminación
|
// Confirmar eliminación
|
||||||
|
@ -154,111 +190,45 @@ export default function AlumnosVista() {
|
||||||
<th className="py-2 border-b">Nombre</th>
|
<th className="py-2 border-b">Nombre</th>
|
||||||
<th className="py-2 border-b">Correo</th>
|
<th className="py-2 border-b">Correo</th>
|
||||||
<th className="py-2 border-b">Teléfono</th>
|
<th className="py-2 border-b">Teléfono</th>
|
||||||
<th className="py-2 border-b">Curso</th>
|
<th className="py-2 border-b">Tipo</th>
|
||||||
|
<th className="py-2 border-b">Formación</th>
|
||||||
<th className="py-2 border-b">Acciones</th>
|
<th className="py-2 border-b">Acciones</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{alumnos.map((alumno) =>
|
{alumnos.map((alumno) => (
|
||||||
alumnoEditando === alumno.id ? (
|
<tr key={alumno.id}>
|
||||||
<tr key={alumno.id}>
|
<td className="py-2 px-4 border-b">{alumno.id}</td>
|
||||||
<td className="py-2 px-4 border-b text-center">
|
<td className="py-2 px-4 border-b">{alumno.nombre}</td>
|
||||||
{alumno.id}
|
<td className="py-2 px-4 border-b">{alumno.correo}</td>
|
||||||
</td>
|
<td className="py-2 px-4 border-b">{alumno.telefono}</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">
|
||||||
<Input type="text" {...register("nombre")} />
|
{alumno.tipo_formacion === "curso"
|
||||||
{errors.nombre && (
|
? "Curso"
|
||||||
<span className="text-red-500 text-xs">
|
: alumno.tipo_formacion === "inyeccion"
|
||||||
{errors.nombre.message}
|
? "Inyección"
|
||||||
</span>
|
: alumno.tipo_formacion === "pildora"
|
||||||
)}
|
? "Píldora"
|
||||||
</td>
|
: ""}
|
||||||
<td className="py-2 px-4 border-b">
|
</td>
|
||||||
<Input type="email" {...register("correo")} />
|
<td className="py-2 px-4 border-b">
|
||||||
{errors.correo && (
|
{alumno.tipo_formacion === "curso" && alumno.curso?.nombre}
|
||||||
<span className="text-red-500 text-xs">
|
{alumno.tipo_formacion === "inyeccion" && alumno.inyeccion?.nombre}
|
||||||
{errors.correo.message}
|
{alumno.tipo_formacion === "pildora" && alumno.pildoras?.nombre}
|
||||||
</span>
|
</td>
|
||||||
)}
|
<td className="py-2 px-4 border-b">
|
||||||
</td>
|
<Button
|
||||||
<td className="py-2 px-4 border-b">
|
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 rounded"
|
||||||
<Input type="text" {...register("telefono")} />
|
onClick={() => {
|
||||||
{errors.telefono && (
|
setAlumnoSeleccionado(alumno);
|
||||||
<span className="text-red-500 text-xs">
|
setMostrarDialog(true);
|
||||||
{errors.telefono.message}
|
}}
|
||||||
</span>
|
>
|
||||||
)}
|
Crear Diploma
|
||||||
</td>
|
</Button>
|
||||||
<td className="py-2 px-4 border-b">
|
</td>
|
||||||
<Select
|
</tr>
|
||||||
value={(alumno.curso_id || "").toString()}
|
))}
|
||||||
onValueChange={(value) =>
|
|
||||||
setValue("cursoSeleccionado", value)
|
|
||||||
}
|
|
||||||
{...register("cursoSeleccionado")}
|
|
||||||
>
|
|
||||||
<SelectTrigger>
|
|
||||||
<SelectValue placeholder="Selecciona un curso" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
{cursos.map((curso) => (
|
|
||||||
<SelectItem
|
|
||||||
key={curso.id}
|
|
||||||
value={curso.id.toString()}
|
|
||||||
>
|
|
||||||
{curso.nombre}
|
|
||||||
</SelectItem>
|
|
||||||
))}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
{errors.cursoSeleccionado && (
|
|
||||||
<span className="text-red-500 text-xs">
|
|
||||||
{errors.cursoSeleccionado.message}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</td>
|
|
||||||
<td className="py-2 px-4 border-b flex justify-center">
|
|
||||||
<Button
|
|
||||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 m-1 rounded"
|
|
||||||
onClick={handleSubmit(guardarEdicion)}
|
|
||||||
>
|
|
||||||
Guardar
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
className="bg-gray-400 hover:bg-gray-600 text-white font-bold py-1 px-3 m-1 rounded"
|
|
||||||
onClick={cancelarEdicion}
|
|
||||||
>
|
|
||||||
Cancelar
|
|
||||||
</Button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
) : (
|
|
||||||
<tr key={alumno.id}>
|
|
||||||
<td className="py-2 px-4 border-b">{alumno.id}</td>
|
|
||||||
<td className="py-2 px-4 border-b">{alumno.nombre}</td>
|
|
||||||
<td className="py-2 px-4 border-b">{alumno.correo}</td>
|
|
||||||
<td className="py-2 px-4 border-b">{alumno.telefono}</td>
|
|
||||||
<td className="py-2 px-4 border-b">
|
|
||||||
{alumno.curso?.nombre || "Sin curso"}
|
|
||||||
</td>
|
|
||||||
<td className="py-2 px-4 border-b flex justify-center">
|
|
||||||
<Button
|
|
||||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 m-1 rounded"
|
|
||||||
onClick={() => iniciarEdicion(alumno)}
|
|
||||||
>
|
|
||||||
Editar
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-3 m-1 rounded"
|
|
||||||
onClick={() => confirmarEliminacion(alumno.id)}
|
|
||||||
>
|
|
||||||
Eliminar
|
|
||||||
</Button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
)
|
|
||||||
)}
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,9 +18,21 @@ export default function DiplomasVista() {
|
||||||
const cargarAlumnos = async () => {
|
const cargarAlumnos = async () => {
|
||||||
const { data, error } = await supabaseClient
|
const { data, error } = await supabaseClient
|
||||||
.from("alumno")
|
.from("alumno")
|
||||||
.select("id, nombre, correo, telefono, curso(id, nombre)")
|
.select(`
|
||||||
|
id,
|
||||||
|
nombre,
|
||||||
|
correo,
|
||||||
|
telefono,
|
||||||
|
tipo_formacion,
|
||||||
|
curso_id,
|
||||||
|
curso(id, nombre),
|
||||||
|
inyeccion_id,
|
||||||
|
inyeccion(id, nombre),
|
||||||
|
pildoras_id,
|
||||||
|
pildoras(id, nombre)
|
||||||
|
`)
|
||||||
.order("id", { ascending: true });
|
.order("id", { ascending: true });
|
||||||
if (!error) setAlumnos(data);
|
setAlumnos(data || []);
|
||||||
};
|
};
|
||||||
cargarAlumnos();
|
cargarAlumnos();
|
||||||
}, []);
|
}, []);
|
||||||
|
@ -62,7 +74,8 @@ export default function DiplomasVista() {
|
||||||
<th className="py-2 border-b">Nombre</th>
|
<th className="py-2 border-b">Nombre</th>
|
||||||
<th className="py-2 border-b">Correo</th>
|
<th className="py-2 border-b">Correo</th>
|
||||||
<th className="py-2 border-b">Teléfono</th>
|
<th className="py-2 border-b">Teléfono</th>
|
||||||
<th className="py-2 border-b">Curso</th>
|
<th className="py-2 border-b">Tipo</th>
|
||||||
|
<th className="py-2 border-b">Formación</th>
|
||||||
<th className="py-2 border-b">Acciones</th>
|
<th className="py-2 border-b">Acciones</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
@ -74,7 +87,18 @@ export default function DiplomasVista() {
|
||||||
<td className="py-2 px-4 border-b">{alumno.correo}</td>
|
<td className="py-2 px-4 border-b">{alumno.correo}</td>
|
||||||
<td className="py-2 px-4 border-b">{alumno.telefono}</td>
|
<td className="py-2 px-4 border-b">{alumno.telefono}</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">
|
||||||
{alumno.curso?.nombre || "Sin curso"}
|
{alumno.tipo_formacion === "curso"
|
||||||
|
? "Curso"
|
||||||
|
: alumno.tipo_formacion === "inyeccion"
|
||||||
|
? "Inyección"
|
||||||
|
: alumno.tipo_formacion === "pildora"
|
||||||
|
? "Píldora"
|
||||||
|
: ""}
|
||||||
|
</td>
|
||||||
|
<td className="py-2 px-4 border-b">
|
||||||
|
{alumno.tipo_formacion === "curso" && alumno.curso?.nombre}
|
||||||
|
{alumno.tipo_formacion === "inyeccion" && alumno.inyeccion?.nombre}
|
||||||
|
{alumno.tipo_formacion === "pildora" && alumno.pildoras?.nombre}
|
||||||
</td>
|
</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">
|
||||||
<Button
|
<Button
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { Schema } from "@/schemas/Schema";
|
|
||||||
import Layout from "@/components/layout/Layout";
|
import Layout from "@/components/layout/Layout";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
|
@ -16,35 +15,99 @@ import {
|
||||||
DialogFooter,
|
DialogFooter,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
|
|
||||||
|
// Puedes usar tu propio esquema de validación
|
||||||
|
const schema = {/* ...tu esquema Zod aquí... */};
|
||||||
|
|
||||||
export default function InyeccionesManual() {
|
export default function InyeccionesManual() {
|
||||||
|
const [competencias, setCompetencias] = useState([]); // [{id, descripcion}]
|
||||||
const [showDialog, setShowDialog] = useState(false);
|
const [showDialog, setShowDialog] = useState(false);
|
||||||
const [dialogMsg, setDialogMsg] = useState("");
|
const [dialogMsg, setDialogMsg] = useState("");
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [addCompetencia, setAddCompetencia] = useState(false);
|
||||||
|
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
resolver: zodResolver(Schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
nombre: "",
|
nombre: "",
|
||||||
descripcion: "",
|
descripcion: "",
|
||||||
horas: 0,
|
horas: 0,
|
||||||
|
nuevaCompetencia: "",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
setValue,
|
||||||
|
getValues,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
reset,
|
reset,
|
||||||
} = form;
|
} = form;
|
||||||
|
|
||||||
|
// Añadir competencia (busca o crea en BD)
|
||||||
|
const handleSaveCompetencia = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const desc = getValues("nuevaCompetencia").trim();
|
||||||
|
if (!desc) return;
|
||||||
|
if (competencias.some((c) => c.descripcion === desc)) {
|
||||||
|
setDialogMsg("La competencia ya fue agregada.");
|
||||||
|
setShowDialog(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
let { data: existente } = await supabaseClient
|
||||||
|
.from("competencia_inyeccion")
|
||||||
|
.select("id")
|
||||||
|
.eq("descripcion", desc)
|
||||||
|
.maybeSingle();
|
||||||
|
let id = existente?.id;
|
||||||
|
if (!id) {
|
||||||
|
const { data: insertada, error } = await supabaseClient
|
||||||
|
.from("competencia_inyeccion")
|
||||||
|
.insert([{ descripcion: desc }])
|
||||||
|
.select("id")
|
||||||
|
.single();
|
||||||
|
if (error) throw error;
|
||||||
|
id = insertada.id;
|
||||||
|
}
|
||||||
|
setCompetencias([...competencias, { id, descripcion: desc }]);
|
||||||
|
setAddCompetencia(false);
|
||||||
|
setValue("nuevaCompetencia", "");
|
||||||
|
setDialogMsg("¡La competencia fue agregada exitosamente!");
|
||||||
|
setShowDialog(true);
|
||||||
|
} catch (err) {
|
||||||
|
setDialogMsg("Error al guardar la competencia: " + (err.message || err));
|
||||||
|
setShowDialog(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Eliminar competencia
|
||||||
|
const handleDeleteCompetencia = (index) => {
|
||||||
|
setCompetencias(competencias.filter((_, i) => i !== index));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Guardar inyección y asociar competencias
|
||||||
const onSubmit = async (data) => {
|
const onSubmit = async (data) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const { nombre, descripcion, horas } = data;
|
const { nombre, descripcion, horas } = data;
|
||||||
const { error } = await supabaseClient
|
const { data: inyeccion, error: errorIny } = await supabaseClient
|
||||||
.from("inyeccion")
|
.from("inyeccion")
|
||||||
.insert([{ nombre, descripcion, horas: parseInt(horas, 10) }]);
|
.insert([{ nombre, descripcion, horas: parseInt(horas, 10) }])
|
||||||
if (error) throw error;
|
.select("id")
|
||||||
|
.single();
|
||||||
|
if (errorIny) throw errorIny;
|
||||||
|
if (competencias.length) {
|
||||||
|
const relaciones = competencias.map((c) => ({
|
||||||
|
inyeccion_id: inyeccion.id,
|
||||||
|
competencia_inyeccion_id: c.id,
|
||||||
|
}));
|
||||||
|
const { error: errorPivote } = await supabaseClient
|
||||||
|
.from("inyeccion_competencia_inyeccion")
|
||||||
|
.insert(relaciones);
|
||||||
|
if (errorPivote) throw errorPivote;
|
||||||
|
}
|
||||||
setDialogMsg("Inyección guardada exitosamente");
|
setDialogMsg("Inyección guardada exitosamente");
|
||||||
|
setCompetencias([]);
|
||||||
reset();
|
reset();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setDialogMsg("Error: " + (err.message || err));
|
setDialogMsg("Error: " + (err.message || err));
|
||||||
|
@ -57,9 +120,7 @@ export default function InyeccionesManual() {
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<div className="w-full bg-white pt-5 font-sans text-center md:w-[80%] flex flex-col items-center justify-start text-black">
|
<div className="w-full bg-white pt-5 font-sans text-center md:w-[80%] flex flex-col items-center justify-start text-black">
|
||||||
<h1 className="text-xl font-semibold mb-10 text-black">
|
<h1 className="text-xl font-semibold mb-10 text-black">Nueva inyección</h1>
|
||||||
Nueva inyección
|
|
||||||
</h1>
|
|
||||||
<form onSubmit={handleSubmit(onSubmit)} className="w-full">
|
<form onSubmit={handleSubmit(onSubmit)} className="w-full">
|
||||||
<Input
|
<Input
|
||||||
type="text"
|
type="text"
|
||||||
|
@ -90,6 +151,78 @@ export default function InyeccionesManual() {
|
||||||
<p className="text-red-500 text-sm">{errors.horas.message}</p>
|
<p className="text-red-500 text-sm">{errors.horas.message}</p>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<h2 className="text-lg font-semibold mb-3 text-black">
|
||||||
|
Competencias de Inyección
|
||||||
|
</h2>
|
||||||
|
<p className="text-xs text-gray-500 mb-2">
|
||||||
|
Puedes agregar competencias nuevas exclusivas para inyecciones.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{competencias.length > 0 && (
|
||||||
|
<div className="mt-5 w-full flex-wrap">
|
||||||
|
{competencias.map((c, i) => (
|
||||||
|
<div
|
||||||
|
key={i}
|
||||||
|
className="w-full flex justify-between items-center px-2 mb-2"
|
||||||
|
>
|
||||||
|
<span className="text-black">{c.descripcion}</span>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => handleDeleteCompetencia(i)}
|
||||||
|
className="bg-red-400 hover:bg-red-500 text-white font-bold py-1 px-3 rounded-md"
|
||||||
|
>
|
||||||
|
X
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{addCompetencia && (
|
||||||
|
<div className="w-full flex flex-col md:flex-row mt-5">
|
||||||
|
<div className="flex flex-col">
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
placeholder="Nueva competencia"
|
||||||
|
{...register("nuevaCompetencia")}
|
||||||
|
className="w-80 px-3 py-2 border border-gray-300 rounded-md mb-3 text-black"
|
||||||
|
/>
|
||||||
|
{errors.nuevaCompetencia && (
|
||||||
|
<p className="text-red-500 text-sm">
|
||||||
|
{errors.nuevaCompetencia.message}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-row">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSaveCompetencia}
|
||||||
|
className="bg-green-400 hover:bg-green-500 text-white font-bold py-2 px-4 rounded-md mr-2"
|
||||||
|
>
|
||||||
|
Guardar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setAddCompetencia(false);
|
||||||
|
setValue("nuevaCompetencia", "");
|
||||||
|
}}
|
||||||
|
className="bg-gray-400 hover:bg-gray-500 text-white font-bold py-2 px-4 rounded-md"
|
||||||
|
>
|
||||||
|
Cancelar
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setAddCompetencia(true)}
|
||||||
|
className="w-full bg-blue-400 hover:bg-blue-500 text-white font-bold py-2 px-4 rounded-md mt-5"
|
||||||
|
>
|
||||||
|
Agregar competencia
|
||||||
|
</Button>
|
||||||
|
|
||||||
<div className="flex justify-center w-full mt-5">
|
<div className="flex justify-center w-full mt-5">
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
|
|
|
@ -22,6 +22,8 @@ export default function InyeccionesVista() {
|
||||||
const [modalMensaje, setModalMensaje] = useState("");
|
const [modalMensaje, setModalMensaje] = useState("");
|
||||||
const [confirmarEliminar, setConfirmarEliminar] = useState(false);
|
const [confirmarEliminar, setConfirmarEliminar] = useState(false);
|
||||||
const [inyeccionAEliminar, setInyeccionAEliminar] = useState(null);
|
const [inyeccionAEliminar, setInyeccionAEliminar] = useState(null);
|
||||||
|
const [competenciasDisponibles, setCompetenciasDisponibles] = useState([]);
|
||||||
|
const [competenciasEditando, setCompetenciasEditando] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
cargarInyecciones();
|
cargarInyecciones();
|
||||||
|
@ -30,7 +32,18 @@ export default function InyeccionesVista() {
|
||||||
const cargarInyecciones = async () => {
|
const cargarInyecciones = async () => {
|
||||||
const { data, error } = await supabaseClient
|
const { data, error } = await supabaseClient
|
||||||
.from("inyeccion")
|
.from("inyeccion")
|
||||||
.select("*")
|
.select(`
|
||||||
|
id,
|
||||||
|
nombre,
|
||||||
|
descripcion,
|
||||||
|
horas,
|
||||||
|
inyeccion_competencia_inyeccion (
|
||||||
|
competencia_inyeccion (
|
||||||
|
id,
|
||||||
|
descripcion
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`)
|
||||||
.order("id", { ascending: true });
|
.order("id", { ascending: true });
|
||||||
if (error) {
|
if (error) {
|
||||||
setModalMensaje("Error al cargar inyecciones: " + error.message);
|
setModalMensaje("Error al cargar inyecciones: " + error.message);
|
||||||
|
@ -40,11 +53,24 @@ export default function InyeccionesVista() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const iniciarEdicion = (inyeccion) => {
|
const iniciarEdicion = async (inyeccion) => {
|
||||||
setInyeccionEditando(inyeccion.id);
|
setInyeccionEditando(inyeccion.id);
|
||||||
setNuevoNombre(inyeccion.nombre);
|
setNuevoNombre(inyeccion.nombre);
|
||||||
setNuevaDescripcion(inyeccion.descripcion);
|
setNuevaDescripcion(inyeccion.descripcion);
|
||||||
setNuevaHoras(inyeccion.horas);
|
setNuevaHoras(inyeccion.horas);
|
||||||
|
|
||||||
|
// Cargar todas las competencias posibles
|
||||||
|
const { data: todas, error: errorTodas } = await supabaseClient
|
||||||
|
.from("competencia_inyeccion")
|
||||||
|
.select("*");
|
||||||
|
setCompetenciasDisponibles(todas || []);
|
||||||
|
|
||||||
|
// Cargar las competencias ya asociadas a la inyección
|
||||||
|
setCompetenciasEditando(
|
||||||
|
(inyeccion.inyeccion_competencia_inyeccion || []).map(
|
||||||
|
(ic) => ic.competencia_inyeccion?.id
|
||||||
|
)
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const cancelarEdicion = () => {
|
const cancelarEdicion = () => {
|
||||||
|
@ -52,6 +78,7 @@ export default function InyeccionesVista() {
|
||||||
setNuevoNombre("");
|
setNuevoNombre("");
|
||||||
setNuevaDescripcion("");
|
setNuevaDescripcion("");
|
||||||
setNuevaHoras("");
|
setNuevaHoras("");
|
||||||
|
setCompetenciasEditando([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const guardarEdicion = async (id) => {
|
const guardarEdicion = async (id) => {
|
||||||
|
@ -64,12 +91,28 @@ export default function InyeccionesVista() {
|
||||||
})
|
})
|
||||||
.eq("id", id);
|
.eq("id", id);
|
||||||
|
|
||||||
if (error) {
|
if (!error) {
|
||||||
setModalMensaje("Error al actualizar la inyección");
|
// Elimina relaciones viejas
|
||||||
} else {
|
await supabaseClient
|
||||||
|
.from("inyeccion_competencia_inyeccion")
|
||||||
|
.delete()
|
||||||
|
.eq("inyeccion_id", id);
|
||||||
|
|
||||||
|
// Inserta las nuevas
|
||||||
|
if (competenciasEditando.length) {
|
||||||
|
const relaciones = competenciasEditando.map((cid) => ({
|
||||||
|
inyeccion_id: id,
|
||||||
|
competencia_inyeccion_id: cid,
|
||||||
|
}));
|
||||||
|
await supabaseClient
|
||||||
|
.from("inyeccion_competencia_inyeccion")
|
||||||
|
.insert(relaciones);
|
||||||
|
}
|
||||||
setModalMensaje("Inyección actualizada exitosamente");
|
setModalMensaje("Inyección actualizada exitosamente");
|
||||||
await cargarInyecciones();
|
await cargarInyecciones();
|
||||||
cancelarEdicion();
|
cancelarEdicion();
|
||||||
|
} else {
|
||||||
|
setModalMensaje("Error al actualizar la inyección");
|
||||||
}
|
}
|
||||||
setMostrarModal(true);
|
setMostrarModal(true);
|
||||||
};
|
};
|
||||||
|
@ -98,22 +141,25 @@ export default function InyeccionesVista() {
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<div className="w-full pt-10 flex flex-col items-center text-black">
|
<div className="w-full pt-10 flex flex-col items-center text-black">
|
||||||
<h1 className="text-2xl font-semibold mb-6">Lista de Inyecciones</h1>
|
<h1 className="text-2xl font-semibold mb-6 text-black">
|
||||||
|
Lista de Inyecciones
|
||||||
|
</h1>
|
||||||
<div className="overflow-x-auto w-full">
|
<div className="overflow-x-auto w-full">
|
||||||
<table className="min-w-full bg-white border">
|
<table className="min-w-full bg-white border text-black">
|
||||||
<thead>
|
<thead>
|
||||||
<tr className="bg-gray-100">
|
<tr className="bg-gray-100 text-black">
|
||||||
<th className="py-2 border-b">ID</th>
|
<th className="py-2 border-b">ID</th>
|
||||||
<th className="py-2 border-b">Nombre</th>
|
<th className="py-2 border-b">Nombre</th>
|
||||||
<th className="py-2 border-b">Descripción</th>
|
<th className="py-2 border-b">Descripción</th>
|
||||||
<th className="py-2 border-b">Horas</th>
|
<th className="py-2 border-b">Horas</th>
|
||||||
|
<th className="py-2 border-b">Competencias</th>
|
||||||
<th className="py-2 border-b">Acciones</th>
|
<th className="py-2 border-b">Acciones</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{inyecciones.map((inyeccion) =>
|
{inyecciones.map((inyeccion) =>
|
||||||
inyeccionEditando === inyeccion.id ? (
|
inyeccionEditando === inyeccion.id ? (
|
||||||
<tr key={inyeccion.id}>
|
<tr key={inyeccion.id} className="text-black">
|
||||||
<td className="py-2 px-4 border-b text-center">
|
<td className="py-2 px-4 border-b text-center">
|
||||||
{inyeccion.id}
|
{inyeccion.id}
|
||||||
</td>
|
</td>
|
||||||
|
@ -121,12 +167,14 @@ export default function InyeccionesVista() {
|
||||||
<Input
|
<Input
|
||||||
value={nuevoNombre}
|
value={nuevoNombre}
|
||||||
onChange={(e) => setNuevoNombre(e.target.value)}
|
onChange={(e) => setNuevoNombre(e.target.value)}
|
||||||
|
className="text-black"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">
|
||||||
<Input
|
<Input
|
||||||
value={nuevaDescripcion}
|
value={nuevaDescripcion}
|
||||||
onChange={(e) => setNuevaDescripcion(e.target.value)}
|
onChange={(e) => setNuevaDescripcion(e.target.value)}
|
||||||
|
className="text-black"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">
|
||||||
|
@ -134,8 +182,71 @@ export default function InyeccionesVista() {
|
||||||
type="number"
|
type="number"
|
||||||
value={nuevaHoras}
|
value={nuevaHoras}
|
||||||
onChange={(e) => setNuevaHoras(e.target.value)}
|
onChange={(e) => setNuevaHoras(e.target.value)}
|
||||||
|
className="text-black"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
|
<td className="py-2 px-4 border-b align-top">
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
{competenciasEditando.map((compId, idx) => (
|
||||||
|
<div key={idx} className="flex items-center gap-2 mb-1">
|
||||||
|
<select
|
||||||
|
value={compId}
|
||||||
|
onChange={(e) => {
|
||||||
|
const nuevaLista = [...competenciasEditando];
|
||||||
|
nuevaLista[idx] = Number(e.target.value);
|
||||||
|
setCompetenciasEditando(nuevaLista);
|
||||||
|
}}
|
||||||
|
className="border rounded px-2 py-1 text-black"
|
||||||
|
>
|
||||||
|
{competenciasDisponibles.map((comp) => (
|
||||||
|
<option key={comp.id} value={comp.id}>
|
||||||
|
{comp.descripcion}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="bg-red-500 hover:bg-red-700 text-white px-3 py-1 rounded"
|
||||||
|
onClick={() => {
|
||||||
|
setCompetenciasEditando(
|
||||||
|
competenciasEditando.filter((_, i) => i !== idx)
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Quitar
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="bg-blue-500 hover:bg-blue-700 text-white px-4 py-2 rounded mt-2"
|
||||||
|
onClick={() => {
|
||||||
|
if (competenciasEditando.length >= 3) {
|
||||||
|
setModalMensaje(
|
||||||
|
"Ya llegaste al límite de 3 competencias para esta inyección."
|
||||||
|
);
|
||||||
|
setMostrarModal(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Agrega la primera competencia disponible que no esté ya seleccionada
|
||||||
|
const disponibles = competenciasDisponibles
|
||||||
|
.map((c) => c.id)
|
||||||
|
.filter((id) => !competenciasEditando.includes(id));
|
||||||
|
if (disponibles.length > 0) {
|
||||||
|
setCompetenciasEditando([
|
||||||
|
...competenciasEditando,
|
||||||
|
disponibles[0],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={
|
||||||
|
competenciasEditando.length >= competenciasDisponibles.length
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Agregar competencia
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
<td className="py-2 px-4 border-b flex justify-center">
|
<td className="py-2 px-4 border-b flex justify-center">
|
||||||
<Button
|
<Button
|
||||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 m-2 rounded"
|
className="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 m-2 rounded"
|
||||||
|
@ -152,13 +263,21 @@ export default function InyeccionesVista() {
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
<tr key={inyeccion.id}>
|
<tr key={inyeccion.id} className="text-black">
|
||||||
<td className="py-2 px-4 border-b">{inyeccion.id}</td>
|
<td className="py-2 px-4 border-b">{inyeccion.id}</td>
|
||||||
<td className="py-2 px-4 border-b">{inyeccion.nombre}</td>
|
<td className="py-2 px-4 border-b">{inyeccion.nombre}</td>
|
||||||
<td className="py-2 px-4 border-b">
|
<td className="py-2 px-4 border-b">{inyeccion.descripcion}</td>
|
||||||
{inyeccion.descripcion}
|
|
||||||
</td>
|
|
||||||
<td className="py-2 px-4 border-b">{inyeccion.horas}</td>
|
<td className="py-2 px-4 border-b">{inyeccion.horas}</td>
|
||||||
|
<td className="py-2 px-4 border-b">
|
||||||
|
<ul className="list-disc ml-5">
|
||||||
|
{(inyeccion.inyeccion_competencia_inyeccion || [])
|
||||||
|
.map((ic) => ic.competencia_inyeccion?.descripcion)
|
||||||
|
.filter(Boolean)
|
||||||
|
.map((desc, idx) => (
|
||||||
|
<li key={idx}>{desc}</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</td>
|
||||||
<td className="py-2 px-4 border-b flex justify-center">
|
<td className="py-2 px-4 border-b flex justify-center">
|
||||||
<Button
|
<Button
|
||||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 m-1 rounded"
|
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 m-1 rounded"
|
||||||
|
@ -188,7 +307,7 @@ export default function InyeccionesVista() {
|
||||||
<DialogTitle className="text-black">
|
<DialogTitle className="text-black">
|
||||||
Confirmar eliminación
|
Confirmar eliminación
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogDescription>
|
<DialogDescription className="text-black">
|
||||||
¿Estás seguro de que deseas eliminar esta inyección? Esta acción
|
¿Estás seguro de que deseas eliminar esta inyección? Esta acción
|
||||||
no se puede deshacer.
|
no se puede deshacer.
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
|
@ -214,10 +333,8 @@ export default function InyeccionesVista() {
|
||||||
<Dialog open={mostrarModal} onOpenChange={setMostrarModal}>
|
<Dialog open={mostrarModal} onOpenChange={setMostrarModal}>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle className="text-black">
|
<DialogTitle className="text-black">Aviso</DialogTitle>
|
||||||
Resultado de la operación
|
<DialogDescription className="text-black">{modalMensaje}</DialogDescription>
|
||||||
</DialogTitle>
|
|
||||||
<DialogDescription>{modalMensaje}</DialogDescription>
|
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button onClick={() => setMostrarModal(false)}>Cerrar</Button>
|
<Button onClick={() => setMostrarModal(false)}>Cerrar</Button>
|
||||||
|
|
Loading…
Reference in New Issue