import DailyIframe from "@daily-co/daily-js";
import { getSession } from "next-auth/client";
import Head from "next/head";
import Link from "next/link";
import { useRouter } from "next/router";
import React, { useEffect } from "react";
import { HeadSeo } from "@components/seo/head-seo";
import prisma from "../../lib/prisma";
export default function JoinCall(props, session) {
const router = useRouter();
//if no booking redirectis to the 404 page
const emptyBooking = props.booking === null;
//daily.co calls have a 60 minute exit and entry buffer when a user enters a call when it's not available it will trigger the modals
const now = new Date();
const enterDate = new Date(now.getTime() + 60 * 60 * 1000);
const exitDate = new Date(now.getTime() - 60 * 60 * 1000);
console.log(enterDate);
//find out if the meeting is upcoming or in the past
const isPast = new Date(props.booking.endTime) <= exitDate;
const isUpcoming = new Date(props.booking.startTime) >= enterDate;
const meetingUnavailable = isUpcoming == true || isPast == true;
useEffect(() => {
if (emptyBooking) {
router.push("/call/no-meeting-found");
}
if (isUpcoming) {
router.push(`/call/meeting-not-started/${props.booking.uid}`);
}
if (isPast) {
router.push(`/call/meeting-ended/${props.booking.uid}`);
}
});
useEffect(() => {
if (!meetingUnavailable && !emptyBooking && session.userid !== props.booking.user.id) {
const callFrame = DailyIframe.createFrame({
theme: {
colors: {
accent: "#FFF",
accentText: "#111111",
background: "#111111",
backgroundAccent: "#111111",
baseText: "#FFF",
border: "#292929",
mainAreaBg: "#111111",
mainAreaBgAccent: "#111111",
mainAreaText: "#FFF",
supportiveText: "#FFF",
},
},
showLeaveButton: true,
iframeStyle: {
position: "fixed",
width: "100%",
height: "100%",
},
});
callFrame.join({
url: props.booking.dailyRef.dailyurl,
showLeaveButton: true,
});
}
if (!meetingUnavailable && !emptyBooking && session.userid === props.booking.user.id) {
const callFrame = DailyIframe.createFrame({
theme: {
colors: {
accent: "#FFF",
accentText: "#111111",
background: "#111111",
backgroundAccent: "#111111",
baseText: "#FFF",
border: "#292929",
mainAreaBg: "#111111",
mainAreaBgAccent: "#111111",
mainAreaText: "#FFF",
supportiveText: "#FFF",
},
},
showLeaveButton: true,
iframeStyle: {
position: "fixed",
width: "100%",
height: "100%",
},
});
callFrame.join({
url: props.booking.dailyRef.dailyurl,
showLeaveButton: true,
token: props.booking.dailyRef.dailytoken,
});
}
}, []);
return (
<>