Files
front_directorio/src/components/Card/CardProfesores/CardProfesor.js
T

118 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-07-15 18:40:28 -06:00
import React, { useState } from "react";
2024-07-17 01:01:13 -06:00
import "../Card.css";
import { PrivateRoutes, PublicRoutes } from "../../../models/routes";
2024-07-26 18:20:54 -06:00
import { useDeleteTeacher } from "../../../services/teacher.services";
2024-06-24 15:47:56 -06:00
2024-08-13 21:34:00 -06:00
const API_URL = process.env.REACT_APP_API_URL;
2024-06-26 15:50:34 -06:00
const CardProfesor = ({ profesor, edificioNombre, permissions }) => {
2024-06-24 15:47:56 -06:00
const [showDialog, setShowDialog] = useState(false);
2024-07-15 18:40:28 -06:00
const [confirmText, setConfirmText] = useState("");
2024-07-26 18:20:54 -06:00
const { deleteTeacher } = useDeleteTeacher();
2024-07-15 18:40:28 -06:00
const defaultImage =
"https://static.vecteezy.com/system/resources/thumbnails/020/765/399/small_2x/default-profile-account-unknown-icon-black-silhouette-free-vector.jpg";
2024-07-10 20:54:20 -06:00
const fotoProfesor = profesor.fotografia || defaultImage;
2024-07-10 17:59:23 -06:00
2024-06-24 15:47:56 -06:00
const handleDeleteClick = () => {
setShowDialog(true);
};
const handleConfirmDelete = async () => {
2024-07-15 18:40:28 -06:00
if (confirmText.toLowerCase() === "aceptar") {
2024-07-26 18:20:54 -06:00
await deleteTeacher(profesor.id_profesor);
2024-06-24 16:12:03 -06:00
setShowDialog(false);
2024-07-15 18:40:28 -06:00
setConfirmText("");
2024-06-24 15:47:56 -06:00
}
};
2024-06-24 16:12:03 -06:00
const handleEditClick = () => {
2024-06-26 15:50:34 -06:00
window.location.href = `${PrivateRoutes.EDITARPROFESOR}${profesor.id_profesor}`;
2024-06-24 16:12:03 -06:00
};
2024-07-15 18:40:28 -06:00
const handleViewProfesor = () => {
2024-06-27 15:50:44 -06:00
window.location.href = `${PublicRoutes.DATAPROFESOR}${profesor.id_profesor}`;
2024-07-15 18:40:28 -06:00
};
2024-06-27 15:50:44 -06:00
2024-06-19 15:37:58 -06:00
return (
2024-06-26 15:50:34 -06:00
<div className="card m-1">
2024-07-15 18:40:28 -06:00
<div className="image">
{fotoProfesor && fotoProfesor.startsWith("http") ? (
<img
src={fotoProfesor}
className="imgProfe"
2024-07-17 01:01:13 -06:00
alt={profesor.nombre}
2024-07-15 18:40:28 -06:00
onClick={handleViewProfesor}
/>
2024-08-13 21:34:00 -06:00
) : (
<img
src={`${API_URL}/imagenes/${profesor.fotografia}`}
className="imgProfe"
alt={profesor.nombre}
onClick={handleViewProfesor}
/>
)}
2024-07-08 15:05:41 -06:00
2024-07-15 18:40:28 -06:00
{permissions ? (
<div
className={`status-label ${
profesor.activo ? "active" : "inactive"
}`}
>
{profesor.activo ? "Activo" : "Inactivo"}
2024-07-11 01:32:59 -06:00
</div>
2024-07-15 18:40:28 -06:00
) : null}
2024-07-08 15:05:41 -06:00
</div>
2024-06-19 15:37:58 -06:00
<div className="card-content">
2024-07-15 18:40:28 -06:00
<h4 onClick={handleViewProfesor}>{profesor.nombre}</h4>
<h6 onClick={handleViewProfesor}>{edificioNombre}</h6>
2024-06-26 20:08:49 -06:00
2024-06-24 15:47:56 -06:00
<div className="button-group">
2024-06-26 15:50:34 -06:00
{permissions ? (
2024-06-25 17:08:29 -06:00
<>
2024-07-15 18:40:28 -06:00
<button
className="edit-button"
onClick={handleEditClick}
style={{ backgroundColor: "green" }}
>
2024-06-26 15:50:34 -06:00
Editar
</button>
2024-07-15 18:40:28 -06:00
<button
className="delete-boton"
onClick={handleDeleteClick}
style={{ backgroundColor: "red" }}
>
2024-06-26 15:50:34 -06:00
Borrar
</button>
2024-06-25 17:08:29 -06:00
</>
) : null}
2024-06-24 15:47:56 -06:00
</div>
2024-07-15 18:40:28 -06:00
</div>
2024-07-08 15:05:41 -06:00
2024-06-24 15:47:56 -06:00
{showDialog && (
2024-07-15 18:40:28 -06:00
<div className="dialog">
<p>
¿Deseas eliminar al profesor <strong>{profesor.nombre}</strong>?
</p>
<p>Para borrar al profesor, escribe "aceptar".</p>
<input
type="text"
value={confirmText}
onChange={(e) => setConfirmText(e.target.value)}
/>
<div className="button-group">
<button
onClick={handleConfirmDelete}
disabled={confirmText.toLowerCase() !== "aceptar"}
>
Confirmar
</button>
<button onClick={() => setShowDialog(false)}>Cancelar</button>
2024-06-26 15:50:34 -06:00
</div>
2024-07-15 18:40:28 -06:00
</div>
2024-06-24 15:47:56 -06:00
)}
2024-06-19 15:37:58 -06:00
</div>
);
};
2024-07-15 18:40:28 -06:00
export default CardProfesor;