|
|
@@ -0,0 +1,107 @@
|
|
|
+"use client"
|
|
|
+
|
|
|
+import { useState, useEffect } from "react"
|
|
|
+import { useSession, signOut } from "next-auth/react"
|
|
|
+import {
|
|
|
+ AlertDialog,
|
|
|
+ AlertDialogAction,
|
|
|
+ AlertDialogCancel,
|
|
|
+ AlertDialogContent,
|
|
|
+ AlertDialogDescription,
|
|
|
+ AlertDialogFooter,
|
|
|
+ AlertDialogHeader,
|
|
|
+ AlertDialogTitle,
|
|
|
+} from "@/components/ui/alert-dialog"
|
|
|
+import { toast } from "sonner"
|
|
|
+
|
|
|
+export function DataConsentModal() {
|
|
|
+ const { data: session, update } = useSession()
|
|
|
+ const [isOpen, setIsOpen] = useState(false)
|
|
|
+ const [isLoading, setIsLoading] = useState(false)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ // Si hay sesión, no está cargando, y el usuario NO ha dado consentimiento
|
|
|
+ if (session?.user && session.user.dataProcessingConsent === false) {
|
|
|
+ setIsOpen(true)
|
|
|
+ } else {
|
|
|
+ setIsOpen(false)
|
|
|
+ }
|
|
|
+ }, [session])
|
|
|
+
|
|
|
+ const handleAccept = async (e: React.MouseEvent) => {
|
|
|
+ e.preventDefault() // Prevenir cierre automático
|
|
|
+ setIsLoading(true)
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await fetch("/api/users/consent", {
|
|
|
+ method: "POST",
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!response.ok) {
|
|
|
+ throw new Error("Error al procesar la solicitud")
|
|
|
+ }
|
|
|
+
|
|
|
+ // Actualizar la sesión localmente para reflejar el cambio sin recargar
|
|
|
+ await update({
|
|
|
+ ...session,
|
|
|
+ user: {
|
|
|
+ ...session?.user,
|
|
|
+ dataProcessingConsent: true
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ toast.success("Consentimiento registrado correctamente")
|
|
|
+ setIsOpen(false)
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error)
|
|
|
+ toast.error("Hubo un error al guardar tu respuesta. Por favor intenta de nuevo.")
|
|
|
+ } finally {
|
|
|
+ setIsLoading(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const handleReject = async (e: React.MouseEvent) => {
|
|
|
+ e.preventDefault()
|
|
|
+ setIsLoading(true)
|
|
|
+
|
|
|
+ try {
|
|
|
+ await signOut({ callbackUrl: "/auth/login" })
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error)
|
|
|
+ setIsLoading(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!session) return null
|
|
|
+
|
|
|
+ return (
|
|
|
+ <AlertDialog open={isOpen}>
|
|
|
+ <AlertDialogContent onEscapeKeyDown={(e) => e.preventDefault()}>
|
|
|
+ <AlertDialogHeader>
|
|
|
+ <AlertDialogTitle>Consentimiento de Tratamiento de Datos</AlertDialogTitle>
|
|
|
+ <AlertDialogDescription asChild className="space-y-4 text-left">
|
|
|
+ <div>
|
|
|
+ <p>
|
|
|
+ Para continuar utilizando nuestros servicios, necesitamos tu consentimiento para el tratamiento de tus datos personales.
|
|
|
+ </p>
|
|
|
+ <p>
|
|
|
+ Tus datos serán utilizados exclusivamente para fines médicos y de seguimiento de tu tratamiento, garantizando su confidencialidad y seguridad de acuerdo con las normativas vigentes.
|
|
|
+ </p>
|
|
|
+ <p className="text-sm text-muted-foreground">
|
|
|
+ Si no aceptas, se cerrará tu sesión automáticamente.
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+ </AlertDialogDescription>
|
|
|
+ </AlertDialogHeader>
|
|
|
+ <AlertDialogFooter>
|
|
|
+ <AlertDialogCancel onClick={handleReject} disabled={isLoading}>
|
|
|
+ No acepto (Salir)
|
|
|
+ </AlertDialogCancel>
|
|
|
+ <AlertDialogAction onClick={handleAccept} disabled={isLoading}>
|
|
|
+ {isLoading ? "Procesando..." : "Acepto el tratamiento de datos"}
|
|
|
+ </AlertDialogAction>
|
|
|
+ </AlertDialogFooter>
|
|
|
+ </AlertDialogContent>
|
|
|
+ </AlertDialog>
|
|
|
+ )
|
|
|
+}
|