"use client"; import { useEffect } from "react"; import { CheckCircledIcon, EnterIcon, DrawingPinFilledIcon } from "@radix-ui/react-icons"; import { Event, EventAppointment, Participant, User } from "@repo/db"; import { cn } from "@repo/shared-components"; import { inscribeToMoodleCourse, upsertParticipant } from "../actions"; import { BookCheck, Calendar, Check, CirclePlay, Clock10Icon, ExternalLink, EyeIcon, Info, TriangleAlert, } from "lucide-react"; import { useForm } from "react-hook-form"; import { InputJsonValueType, ParticipantOptionalDefaults, ParticipantOptionalDefaultsSchema, } from "@repo/db/zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Select } from "../../../_components/ui/Select"; import { useRouter } from "next/navigation"; import { handleParticipantEnrolled } from "../../../../helper/events"; import { eventCompleted } from "@repo/shared-components"; import MDEditor from "@uiw/react-md-editor"; import toast from "react-hot-toast"; import { formatDate } from "date-fns"; interface ModalBtnProps { title: string; event: Event; dates: (EventAppointment & { Participants: { userId: string }[]; })[]; selectedAppointments: EventAppointment[]; participant?: Participant; user: User; modalId: string; } const ModalBtn = ({ title, dates, modalId, participant, selectedAppointments, event, user, }: ModalBtnProps) => { useEffect(() => { const modal = document.getElementById(modalId) as HTMLDialogElement; const handleOpen = () => { document.body.classList.add("modal-open"); }; const handleClose = () => { document.body.classList.remove("modal-open"); }; modal?.addEventListener("show", handleOpen); modal?.addEventListener("close", handleClose); return () => { modal?.removeEventListener("show", handleOpen); modal?.removeEventListener("close", handleClose); }; }, [modalId]); const router = useRouter(); const canSelectDate = event.hasPresenceEvents && !participant?.attended && (selectedAppointments.length === 0 || participant?.appointmentCancelled); const openModal = () => { const modal = document.getElementById(modalId) as HTMLDialogElement; modal?.showModal(); }; const closeModal = () => { const modal = document.getElementById(modalId) as HTMLDialogElement; modal?.close(); }; const selectAppointmentForm = useForm({ resolver: zodResolver(ParticipantOptionalDefaultsSchema), defaultValues: { eventId: event.id, userId: user.id, ...participant, }, }); const selectedAppointment = selectedAppointments[0]; const selectedDate = dates.find( (date) => date.id === selectAppointmentForm.watch("eventAppointmentId") || selectedAppointment?.id, ); const ownIndexInParticipantList = selectedDate?.Participants?.findIndex( (p) => p.userId === user.id, ); const ownPlaceInParticipantList = typeof ownIndexInParticipantList === "number" ? ownIndexInParticipantList === -1 ? (selectedDate?.Participants?.length ?? 0) + 1 : ownIndexInParticipantList + 1 : undefined; const missingRequirements = event.requiredBadges?.length > 0 && !event.requiredBadges.some((badge) => user.badges.includes(badge)); return ( <>

{title}

Details

{event.hasPresenceEvents && (

Termine

{!!dates.length && !selectedDate && ( <>

Melde dich zu einem Termin an