diff --git a/diplomas/package-lock.json b/diplomas/package-lock.json index e1816d0..c90d0ae 100644 --- a/diplomas/package-lock.json +++ b/diplomas/package-lock.json @@ -29,6 +29,7 @@ "mysql2": "^3.14.1", "next": "15.3.0", "papaparse": "^5.5.2", + "qrcode": "^1.5.4", "react": "^19.0.0", "react-dom": "^19.0.0", "react-hook-form": "^7.56.2", @@ -3084,11 +3085,18 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "engines": { + "node": ">=8" + } + }, "node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, "dependencies": { "color-convert": "^2.0.1" }, @@ -3509,6 +3517,14 @@ "node": ">=6" } }, + "node_modules/camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "engines": { + "node": ">=6" + } + }, "node_modules/caniuse-lite": { "version": "1.0.30001714", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001714.tgz", @@ -3572,6 +3588,16 @@ "resolved": "https://registry.npmjs.org/client-only/-/client-only-0.0.1.tgz", "integrity": "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==" }, + "node_modules/cliui": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", + "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^6.2.0" + } + }, "node_modules/clone": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", @@ -3613,7 +3639,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "devOptional": true, "dependencies": { "color-name": "~1.1.4" }, @@ -3802,6 +3827,14 @@ } } }, + "node_modules/decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/deep-is": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", @@ -3893,6 +3926,11 @@ "resolved": "https://registry.npmjs.org/dfa/-/dfa-1.2.0.tgz", "integrity": "sha512-ED3jP8saaweFTjeGX8HQPjeC1YYyZs98jGNZx6IiBvxW7JG5v492kamAQB3m2wop07CvU/RQmzcKr6bgcC5D/Q==" }, + "node_modules/dijkstrajs": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/dijkstrajs/-/dijkstrajs-1.0.3.tgz", + "integrity": "sha512-qiSlmBq9+BCdCA/L46dw8Uy93mloxsPSbwnm5yrKn2vMPiy8KyAskTF6zuV/j5BMsmOGZDPs7KjU+mjb670kfA==" + }, "node_modules/diplomas": { "resolved": "", "link": true @@ -4883,6 +4921,14 @@ "is-property": "^1.0.2" } }, + "node_modules/get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "engines": { + "node": "6.* || 8.* || >= 10.*" + } + }, "node_modules/get-intrinsic": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", @@ -5372,6 +5418,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-generator-function": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.1.0.tgz", @@ -6494,6 +6548,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "engines": { + "node": ">=6" + } + }, "node_modules/pako": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", @@ -6533,7 +6595,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", - "dev": true, "engines": { "node": ">=8" } @@ -6578,6 +6639,14 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/pngjs": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/pngjs/-/pngjs-5.0.0.tgz", + "integrity": "sha512-40QW5YalBNfQo5yRYmiw7Yz6TKKVr3h6970B2YE+3fQpsWcrbj1PzJgxeJ19DRQjhMbKPIuMY8rFaXc8moolVw==", + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/possible-typed-array-names": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz", @@ -6665,6 +6734,22 @@ "node": ">=6" } }, + "node_modules/qrcode": { + "version": "1.5.4", + "resolved": "https://registry.npmjs.org/qrcode/-/qrcode-1.5.4.tgz", + "integrity": "sha512-1ca71Zgiu6ORjHqFBDpnSMTR2ReToX4l1Au1VFLyVeBTFavzQnv5JxMFr3ukHVKpSrSA2MCk0lNJSykjUfz7Zg==", + "dependencies": { + "dijkstrajs": "^1.0.1", + "pngjs": "^5.0.0", + "yargs": "^15.3.1" + }, + "bin": { + "qrcode": "bin/qrcode" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/qs": { "version": "6.14.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.14.0.tgz", @@ -6876,6 +6961,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", @@ -6884,6 +6977,11 @@ "node": ">=0.10.0" } }, + "node_modules/require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" + }, "node_modules/resolve": { "version": "1.22.10", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.10.tgz", @@ -7108,6 +7206,11 @@ "node": ">= 18" } }, + "node_modules/set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==" + }, "node_modules/set-function-length": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz", @@ -7353,6 +7456,24 @@ "safe-buffer": "~5.2.0" } }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/string-width/node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" + }, "node_modules/string.prototype.includes": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/string.prototype.includes/-/string.prototype.includes-2.0.1.tgz", @@ -7460,6 +7581,17 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", @@ -8028,6 +8160,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/which-module": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.1.tgz", + "integrity": "sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==" + }, "node_modules/which-typed-array": { "version": "1.1.19", "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.19.tgz", @@ -8074,6 +8211,19 @@ "node": ">=0.10.0" } }, + "node_modules/wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", @@ -8119,6 +8269,92 @@ "node": ">=0.8" } }, + "node_modules/y18n": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz", + "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==" + }, + "node_modules/yargs": { + "version": "15.4.1", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", + "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==", + "dependencies": { + "cliui": "^6.0.0", + "decamelize": "^1.2.0", + "find-up": "^4.1.0", + "get-caller-file": "^2.0.1", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^4.2.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^18.1.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/yargs-parser": { + "version": "18.1.3", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", + "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", + "dependencies": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/yargs/node_modules/find-up": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", + "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "dependencies": { + "locate-path": "^5.0.0", + "path-exists": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/yargs/node_modules/locate-path": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", + "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", + "dependencies": { + "p-locate": "^4.1.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/yargs/node_modules/p-limit": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", + "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "dependencies": { + "p-try": "^2.0.0" + }, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/yargs/node_modules/p-locate": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", + "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", + "dependencies": { + "p-limit": "^2.2.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", diff --git a/diplomas/package.json b/diplomas/package.json index efa34c8..c322137 100644 --- a/diplomas/package.json +++ b/diplomas/package.json @@ -30,6 +30,7 @@ "mysql2": "^3.14.1", "next": "15.3.0", "papaparse": "^5.5.2", + "qrcode": "^1.5.4", "react": "^19.0.0", "react-dom": "^19.0.0", "react-hook-form": "^7.56.2", diff --git a/diplomas/src/components/Diploma.jsx b/diplomas/src/components/Diploma.jsx index 5c2af03..7e14251 100644 --- a/diplomas/src/components/Diploma.jsx +++ b/diplomas/src/components/Diploma.jsx @@ -33,9 +33,21 @@ const styles = StyleSheet.create({ fontSize: 10, color: "#888", }, + qr: { + marginTop: 30, + alignSelf: "center", + width: 100, + height: 100, + }, }); -export default function Diploma({ alumno, curso, competencias = [], fecha }) { +export default function Diploma({ alumno, formacion, fecha, qr }) { + // formacion: { tipo, nombre, competencias } + let tipoTexto = "formación"; + if (formacion?.tipo === "curso") tipoTexto = "curso"; + else if (formacion?.tipo === "inyeccion") tipoTexto = "inyección"; + else if (formacion?.tipo === "pildora") tipoTexto = "píldora educativa"; + return ( @@ -45,15 +57,36 @@ export default function Diploma({ alumno, curso, competencias = [], fecha }) { a: {alumno?.nombre} - Por su asistencia a la píldora educativa - - {curso?.nombre || "Sin curso"} - - con duración de 2 horas, modalidad remota + Por su asistencia{" "} + {formacion?.tipo === "curso" + ? "al curso" + : formacion?.tipo === "inyeccion" + ? "a la inyección" + : formacion?.tipo === "pildora" + ? "a la píldora educativa" + : "a la formación"} + {formacion?.nombre || "Sin formación"} + {(formacion?.tipo === "curso" || formacion?.tipo === "inyeccion") && + formacion?.competencias?.length > 0 && ( + + + Competencias acreditadas: + + {formacion.competencias.map((comp) => ( + + - {comp.descripcion} + + ))} + + )} Se expide en la ciudad de Xalapa, Ver., {fecha} + {qr && } + + Verifica este diploma en: http://localhost:3000/alumno/{alumno?.id} + ); diff --git a/diplomas/src/components/dialogs/vistaPreviaDiplomaDialog.jsx b/diplomas/src/components/dialogs/vistaPreviaDiplomaDialog.jsx index 936cfcc..25aa7cc 100644 --- a/diplomas/src/components/dialogs/vistaPreviaDiplomaDialog.jsx +++ b/diplomas/src/components/dialogs/vistaPreviaDiplomaDialog.jsx @@ -12,6 +12,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { mensajesSchema } from "@/schemas/mensajesSchema"; import { Textarea } from "../ui/textarea"; +import QRCode from "qrcode"; function VistaPreviaDiplomaDialog({ open, @@ -27,6 +28,7 @@ function VistaPreviaDiplomaDialog({ const [mensaje, setMensaje] = useState(""); const [loadingMensajes, setLoadingMensajes] = useState(false); const [competencias, setCompetencias] = useState([]); + const [qrDataUrl, setQrDataUrl] = useState(""); const form = useForm({ resolver: zodResolver(mensajesSchema), @@ -44,6 +46,15 @@ function VistaPreviaDiplomaDialog({ formState: { errors }, } = form; + useEffect(() => { + if (alumno?.id) { + const url = `http://localhost:3000/alumno/${alumno.id}`; + QRCode.toDataURL(url, { width: 200 }, (err, url) => { + if (!err) setQrDataUrl(url); + }); + } + }, [alumno]); + // 🔄 Cargar mensajes al abrir el modal useEffect(() => { if (open) { @@ -97,23 +108,63 @@ function VistaPreviaDiplomaDialog({ }; useEffect(() => { - if (alumno && alumno.curso?.id) { - supabaseClient - .from("curso_competencia") - .select("competencia(id, descripcion)") - .eq("curso_id", alumno.curso.id) - .then(({ data }) => { - const comps = data?.map((c) => c.competencia).filter(Boolean) || []; - setCompetencias(comps); - }); + if (alumno) { + if (alumno.tipo_formacion === "curso" && alumno.curso?.id) { + supabaseClient + .from("curso_competencia") + .select("competencia(id, descripcion)") + .eq("curso_id", alumno.curso.id) + .then(({ data }) => { + const comps = data?.map((c) => c.competencia).filter(Boolean) || []; + setCompetencias(comps); + }); + } else if ( + alumno.tipo_formacion === "inyeccion" && + alumno.inyeccion?.id + ) { + supabaseClient + .from("inyeccion_competencia_inyeccion") + .select("competencia_inyeccion(id, descripcion)") + .eq("inyeccion_id", alumno.inyeccion.id) + .then(({ data }) => { + const comps = + data?.map((c) => c.competencia_inyeccion).filter(Boolean) || []; + setCompetencias(comps); + }); + } else { + setCompetencias([]); + } } }, [alumno]); if (!alumno) return null; - const competenciasMostradas = competenciasAcreditadas - ? competencias.filter((comp) => competenciasAcreditadas.includes(comp.id)) - : competencias; + // Mostrar solo competencias acreditadas si corresponde + const competenciasMostradas = + alumno.tipo_formacion === "curso" || alumno.tipo_formacion === "inyeccion" + ? competenciasAcreditadas + ? competencias.filter((comp) => + competenciasAcreditadas.includes(comp.id) + ) + : competencias + : []; + + // Obtener nombre de la formación según tipo + let nombreFormacion = ""; + if (alumno.tipo_formacion === "curso") { + nombreFormacion = alumno.curso?.nombre || "Sin curso"; + } else if (alumno.tipo_formacion === "inyeccion") { + nombreFormacion = alumno.inyeccion?.nombre || "Sin inyección"; + } else if (alumno.tipo_formacion === "pildora") { + nombreFormacion = alumno.pildoras?.nombre || "Sin píldora"; + } + + // Para el PDF, pasar el nombre y tipo de formación + const datosFormacion = { + tipo: alumno.tipo_formacion, + nombre: nombreFormacion, + competencias: competenciasMostradas, + }; const handleEnviar = async () => { setEnviando(true); @@ -165,7 +216,7 @@ function VistaPreviaDiplomaDialog({ return ( - + Diploma @@ -173,16 +224,29 @@ function VistaPreviaDiplomaDialog({ Alumno: {alumno.nombre}
- Curso: {alumno.curso?.nombre || "Sin curso"} -
-
- Competencias Acreditadas: -
    - {competenciasMostradas.map((comp) => ( -
  • {comp.descripcion}
  • - ))} -
+ + {alumno.tipo_formacion === "curso" + ? "Curso" + : alumno.tipo_formacion === "inyeccion" + ? "Inyección" + : alumno.tipo_formacion === "pildora" + ? "Píldora" + : "Formación"} + : + {" "} + {nombreFormacion}
+ {(alumno.tipo_formacion === "curso" || + alumno.tipo_formacion === "inyeccion") && ( +
+ Competencias Acreditadas: +
    + {competenciasMostradas.map((comp) => ( +
  • {comp.descripcion}
  • + ))} +
+
+ )}
Fecha: {fecha || new Date().toLocaleDateString()}
@@ -214,9 +278,9 @@ function VistaPreviaDiplomaDialog({ document={ } fileName={`Diploma_${alumno.nombre}.pdf`} @@ -268,13 +332,13 @@ function VistaPreviaDiplomaDialog({ {mostrarVistaPrevia && (
-
+
diff --git a/diplomas/src/pages/alumno/[id].jsx b/diplomas/src/pages/alumno/[id].jsx new file mode 100644 index 0000000..adb8fba --- /dev/null +++ b/diplomas/src/pages/alumno/[id].jsx @@ -0,0 +1,132 @@ +import { useRouter } from "next/router"; +import { useEffect, useState } from "react"; +import { supabaseClient } from "@/utils/supabase"; +import { Card } from "@/components/ui/card"; + +export default function AlumnoId() { + const router = useRouter(); + const { id } = router.query; + const [alumno, setAlumno] = useState(null); + const [formacionNombre, setFormacionNombre] = useState(""); + const [competencias, setCompetencias] = useState([]); + const [loading, setLoading] = useState(true); + + useEffect(() => { + if (!id) return; + const fetchAlumno = async () => { + // Traer datos del alumno y su formación + const { data, error } = await supabaseClient + .from("alumno") + .select( + ` + id, nombre, correo, tipo_formacion, + curso(id, nombre), + inyeccion(id, nombre), + pildoras(id, nombre) + ` + ) + .eq("id", id) + .single(); + + if (data) { + setAlumno(data); + let nombreFormacion = ""; + if (data.tipo_formacion === "curso") + nombreFormacion = data.curso?.nombre || ""; + if (data.tipo_formacion === "inyeccion") + nombreFormacion = data.inyeccion?.nombre || ""; + if (data.tipo_formacion === "pildora") + nombreFormacion = data.pildoras?.nombre || ""; + setFormacionNombre(nombreFormacion); + + // Traer competencias según tipo + if (data.tipo_formacion === "curso" && data.curso?.id) { + const { data: comps } = await supabaseClient + .from("curso_competencia") + .select("competencia(id, descripcion)") + .eq("curso_id", data.curso.id); + setCompetencias(comps?.map((c) => c.competencia) || []); + } else if (data.tipo_formacion === "inyeccion" && data.inyeccion?.id) { + const { data: comps } = await supabaseClient + .from("inyeccion_competencia_inyeccion") + .select("competencia_inyeccion(id, descripcion)") + .eq("inyeccion_id", data.inyeccion.id); + setCompetencias(comps?.map((c) => c.competencia_inyeccion) || []); + } else { + setCompetencias([]); + } + } + setLoading(false); + }; + fetchAlumno(); + }, [id]); + + if (loading) return
Cargando...
; + if (!alumno) + return
Alumno no encontrado
; + + return ( + + Encabezado +

+ Información de {alumno.nombre} +

+

+ Nombre: {alumno.nombre} +

+

+ Correo: {alumno.correo} +

+

+ Tipo de formación:{" "} + {alumno.tipo_formacion === "curso" + ? "Curso" + : alumno.tipo_formacion === "inyeccion" + ? "Inyección" + : alumno.tipo_formacion === "pildora" + ? "Píldora" + : ""} +

+

+ + {alumno.tipo_formacion === "curso" + ? "Curso" + : alumno.tipo_formacion === "inyeccion" + ? "Inyección" + : alumno.tipo_formacion === "pildora" + ? "Píldora" + : "Formación"} + : + {" "} + {formacionNombre} +

+ {(alumno.tipo_formacion === "curso" || + alumno.tipo_formacion === "inyeccion") && ( +
+ Competencias: +
    + {competencias.map((comp) => ( +
  • {comp.descripcion}
  • + ))} +
+
+ )} +
+ Felicidades {alumno.nombre} por haber concluido tu{" "} + {alumno.tipo_formacion === "curso" + ? "curso" + : alumno.tipo_formacion === "inyeccion" + ? "inyección" + : alumno.tipo_formacion === "pildora" + ? "píldora" + : "formación"}{" "} + {formacionNombre}, agradecemos tu participación, te esperamos pronto por + aquí para seguir formando tu camino. +
+
+ ); +} diff --git a/diplomas/src/pages/alumnosManual.jsx b/diplomas/src/pages/alumnosManual.jsx index 9bccdd0..ab26b84 100644 --- a/diplomas/src/pages/alumnosManual.jsx +++ b/diplomas/src/pages/alumnosManual.jsx @@ -11,7 +11,6 @@ import { } from "@/components/ui/select"; import { Dialog, - DialogTrigger, DialogContent, DialogHeader, DialogTitle, @@ -24,61 +23,99 @@ import { alumnoSchema } from "@/schemas/AlumnosSchema"; export default function AlumnosManual() { const [cursos, setCursos] = useState([]); - const [isDialogOpen, setIsDialogOpen] = useState(false); // Estado para controlar el diálogo + const [inyecciones, setInyecciones] = useState([]); + const [pildoras, setPildoras] = useState([]); + const [isDialogOpen, setIsDialogOpen] = useState(false); - // Configurar React Hook Form con zod const { register, handleSubmit, setValue, + watch, formState: { errors }, reset, + clearErrors, } = useForm({ resolver: zodResolver(alumnoSchema), defaultValues: { nombre: "", correo: "", telefono: "", + tipo: "", cursoSeleccionado: "", }, }); - // Cargar cursos al iniciar el componente + const tipo = watch("tipo"); + const cursoSeleccionado = watch("cursoSeleccionado"); + + // Cargar opciones según tipo seleccionado useEffect(() => { - const cargarCursos = async () => { - const { data, error } = await supabaseClient - .from("curso") - .select("id, nombre"); - if (error) { - console.error("Error al cargar cursos:", error.message); - } else { - setCursos(data); - } - }; - cargarCursos(); - }, []); + if (!tipo) { + setValue("cursoSeleccionado", ""); + clearErrors("cursoSeleccionado"); + return; + } + setValue("cursoSeleccionado", ""); + clearErrors("cursoSeleccionado"); + if (tipo === "curso") { + cargarCursos(); + } else if (tipo === "inyeccion") { + cargarInyecciones(); + } else if (tipo === "pildora") { + cargarPildoras(); + } + // eslint-disable-next-line + }, [tipo]); + + const cargarCursos = async () => { + const { data, error } = await supabaseClient + .from("curso") + .select("id, nombre"); + if (!error) setCursos(data || []); + }; + const cargarInyecciones = async () => { + const { data, error } = await supabaseClient + .from("inyeccion") + .select("id, nombre"); + if (!error) setInyecciones(data || []); + }; + const cargarPildoras = async () => { + const { data, error } = await supabaseClient + .from("pildoras") + .select("id, nombre"); + if (!error) setPildoras(data || []); + }; // Guardar alumno const manejarGuardar = async (data) => { try { - const { error } = await supabaseClient.from("alumno").insert([ - { - nombre: data.nombre, - correo: data.correo, - telefono: data.telefono, - curso_id: Number(data.cursoSeleccionado), // Guardar el ID del curso - }, - ]); + let campos = { + nombre: data.nombre, + correo: data.correo, + telefono: data.telefono, + tipo_formacion: data.tipo, + curso_id: null, + inyeccion_id: null, + pildoras_id: null, + }; + if (data.tipo === "curso") + campos.curso_id = Number(data.cursoSeleccionado); + if (data.tipo === "inyeccion") + campos.inyeccion_id = Number(data.cursoSeleccionado); + if (data.tipo === "pildora") + campos.pildoras_id = Number(data.cursoSeleccionado); + + const { error } = await supabaseClient.from("alumno").insert([campos]); if (error) { - console.error("Error al guardar:", error.message); - setIsDialogOpen(false); // Asegurarse de cerrar el diálogo en caso de error + setIsDialogOpen(false); } else { - setIsDialogOpen(true); // Mostrar el diálogo al guardar exitosamente - reset(); // Reiniciar el formulario + setIsDialogOpen(true); + reset(); } } catch (err) { - console.error("Error inesperado:", err); + // Manejo de error inesperado } }; @@ -118,20 +155,77 @@ export default function AlumnosManual() { {errors.telefono.message}

)} + + {/* Select para tipo de formación */} + {errors.tipo && ( +

{errors.tipo.message}

+ )} + + {/* Select para la opción según tipo */} + {tipo === "curso" && ( + + )} + {tipo === "inyeccion" && ( + + )} + {tipo === "pildora" && ( + + )} {errors.cursoSeleccionado && (

{errors.cursoSeleccionado.message} diff --git a/diplomas/src/pages/alumnosVista.jsx b/diplomas/src/pages/alumnosVista.jsx index 2951e9d..c7f607b 100644 --- a/diplomas/src/pages/alumnosVista.jsx +++ b/diplomas/src/pages/alumnosVista.jsx @@ -33,6 +33,10 @@ export default function AlumnosVista() { const [modalMensaje, setModalMensaje] = useState(""); const [nuevoCurso, setNuevoCurso] = 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 const [confirmarEliminar, setConfirmarEliminar] = useState(false); @@ -40,7 +44,10 @@ export default function AlumnosVista() { useEffect(() => { cargarAlumnos(); + // ...cargar cursos, inyecciones, pildoras... cargarCursos(); + cargarInyecciones(); + cargarPildoras(); }, []); const cargarCursos = async () => { @@ -52,16 +59,36 @@ export default function AlumnosVista() { } }; + const cargarInyecciones = async () => { + const { data, error } = await supabaseClient.from("inyeccion").select("*"); + if (!error) setInyecciones(data || []); + }; + + const cargarPildoras = async () => { + const { data, error } = await supabaseClient.from("pildoras").select("*"); + if (!error) setPildoras(data || []); + }; + const cargarAlumnos = async () => { const { data, error } = await supabaseClient .from("alumno") - .select("id, nombre, correo, telefono, curso_id, curso(nombre)") + .select( + ` + id, + nombre, + correo, + telefono, + tipo_formacion, + curso_id, + curso(nombre), + inyeccion_id, + inyeccion(nombre), + pildoras_id, + pildoras(nombre) + ` + ) .order("id", { ascending: true }); - if (error) { - console.error("Error al cargar alumnos:", error.message); - } else { - setAlumnos(data); - } + setAlumnos(data || []); }; const { @@ -83,10 +110,18 @@ export default function AlumnosVista() { // Iniciar edición const iniciarEdicion = (alumno) => { setAlumnoEditando(alumno.id); - setValue("nombre", alumno.nombre); - setValue("correo", alumno.correo); - setValue("telefono", alumno.telefono); - setValue("cursoSeleccionado", alumno.curso_id?.toString() || ""); + setNuevoNombre(alumno.nombre || ""); + setNuevoCorreo(alumno.correo || ""); + setNuevoNumero(alumno.telefono || ""); + 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 @@ -96,25 +131,37 @@ export default function AlumnosVista() { }; // 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 .from("alumno") - .update({ - nombre: data.nombre, - correo: data.correo, - telefono: data.telefono, - curso_id: data.cursoSeleccionado, - }) - .eq("id", alumnoEditando); + .update(updateObj) + .eq("id", id); - if (error) { - setModalMensaje("Error al actualizar el alumno"); + if (!error) { + setModalMensaje("Alumno actualizado correctamente"); + setMostrarModal(true); + setAlumnoEditando(null); + await cargarAlumnos(); // <--- Refresca la tabla } else { - setModalMensaje("Alumno actualizado exitosamente"); - await cargarAlumnos(); - cancelarEdicion(); + setModalMensaje("Error al actualizar el alumno"); + setMostrarModal(true); } - setMostrarModal(true); }; // Confirmar eliminación @@ -149,12 +196,13 @@ export default function AlumnosVista() {

- + - + + @@ -166,70 +214,101 @@ export default function AlumnosVista() { {alumno.id} + - ) : ( @@ -239,22 +318,35 @@ export default function AlumnosVista() { - + ) diff --git a/diplomas/src/pages/diplomasVista.jsx b/diplomas/src/pages/diplomasVista.jsx index 4539b60..c5d6b89 100644 --- a/diplomas/src/pages/diplomasVista.jsx +++ b/diplomas/src/pages/diplomasVista.jsx @@ -18,9 +18,23 @@ export default function DiplomasVista() { const cargarAlumnos = async () => { const { data, error } = await supabaseClient .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 }); - if (!error) setAlumnos(data); + setAlumnos(data || []); }; cargarAlumnos(); }, []); @@ -37,16 +51,38 @@ export default function DiplomasVista() { }; useEffect(() => { - if (alumnoSeleccionado && alumnoSeleccionado.curso?.id) { - supabaseClient - .from("curso_competencia") - .select("competencia(id, descripcion)") - .eq("curso_id", alumnoSeleccionado.curso.id) - .then(({ data }) => { - const comps = data?.map((c) => c.competencia).filter(Boolean) || []; - setCompetencias(comps); - setCompetenciasAcreditadas(comps.map((c) => c.id)); // Opcional: selecciona todas por default - }); + if (alumnoSeleccionado) { + if ( + alumnoSeleccionado.tipo_formacion === "curso" && + alumnoSeleccionado.curso?.id + ) { + supabaseClient + .from("curso_competencia") + .select("competencia(id, descripcion)") + .eq("curso_id", alumnoSeleccionado.curso.id) + .then(({ data }) => { + const comps = data?.map((c) => c.competencia).filter(Boolean) || []; + setCompetencias(comps); + setCompetenciasAcreditadas(comps.map((c) => c.id)); + }); + } else if ( + alumnoSeleccionado.tipo_formacion === "inyeccion" && + alumnoSeleccionado.inyeccion?.id + ) { + supabaseClient + .from("inyeccion_competencia_inyeccion") + .select("competencia_inyeccion(id, descripcion)") + .eq("inyeccion_id", alumnoSeleccionado.inyeccion.id) + .then(({ data }) => { + const comps = + data?.map((c) => c.competencia_inyeccion).filter(Boolean) || []; + setCompetencias(comps); + setCompetenciasAcreditadas(comps.map((c) => c.id)); + }); + } else { + setCompetencias([]); + setCompetenciasAcreditadas([]); + } } }, [alumnoSeleccionado]); @@ -62,7 +98,8 @@ export default function DiplomasVista() { - + + @@ -74,7 +111,20 @@ export default function DiplomasVista() { +
ID Nombre Correo TeléfonoCursoTipoFormación Acciones
- - {errors.nombre && ( - - {errors.nombre.message} - - )} + setNuevoNombre(e.target.value)} + className="border rounded px-2 py-1 text-black" + /> - - {errors.correo && ( - - {errors.correo.message} - - )} + setNuevoCorreo(e.target.value)} + className="border rounded px-2 py-1 text-black" + /> - - {errors.telefono && ( - - {errors.telefono.message} - - )} + setNuevoNumero(e.target.value)} + className="border rounded px-2 py-1 text-black" + /> - { + setNuevoTipo(e.target.value); + setNuevaFormacion(""); + }} + className="border rounded px-2 py-1 text-black" > - - - - + + + + + + + {nuevoTipo === "curso" && ( + - {errors.cursoSeleccionado && ( - - {errors.cursoSeleccionado.message} - + + )} + {nuevoTipo === "inyeccion" && ( + + )} + {nuevoTipo === "pildora" && ( + )} - - +
{alumno.correo} {alumno.telefono} - {alumno.curso?.nombre || "Sin curso"} + {alumno.tipo_formacion === "curso" + ? "Curso" + : alumno.tipo_formacion === "inyeccion" + ? "Inyección" + : alumno.tipo_formacion === "pildora" + ? "Píldora" + : ""} - + - - +
Nombre Correo TeléfonoCursoTipoFormación Acciones
{alumno.correo} {alumno.telefono} - {alumno.curso?.nombre || "Sin curso"} + {alumno.tipo_formacion === "curso" + ? "Curso" + : alumno.tipo_formacion === "inyeccion" + ? "Inyección" + : alumno.tipo_formacion === "pildora" + ? "Píldora" + : ""} + + {alumno.tipo_formacion === "curso" && alumno.curso?.nombre} + {alumno.tipo_formacion === "inyeccion" && + alumno.inyeccion?.nombre} + {alumno.tipo_formacion === "pildora" && + alumno.pildoras?.nombre} + + ))} + + )} + + {addCompetencia && ( +
+
+ + {errors.nuevaCompetencia && ( +

+ {errors.nuevaCompetencia.message} +

+ )} +
+
+ + +
+
+ )} + + +