import Loader from "@components/Loader"; import prisma from "@lib/prisma"; import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; import { GetServerSideProps } from "next"; import { getSession } from "next-auth/client"; import { useEffect, useState } from "react"; import Shell from "@components/Shell"; dayjs.extend(utc); export default function Troubleshoot({ user }) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const [loading, setLoading] = useState(true); const [availability, setAvailability] = useState([]); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [selectedDate, setSelectedDate] = useState(dayjs()); function convertMinsToHrsMins(mins) { let h = Math.floor(mins / 60); let m = mins % 60; h = h < 10 ? "0" + h : h; m = m < 10 ? "0" + m : m; return `${h}:${m}`; } const fetchAvailability = (date) => { const dateFrom = date.startOf("day").utc().format(); const dateTo = date.endOf("day").utc().format(); fetch(`/api/availability/${user.username}?dateFrom=${dateFrom}&dateTo=${dateTo}`) .then((res) => { return res.json(); }) .then((availableIntervals) => { setAvailability(availableIntervals); setLoading(false); }) .catch((e) => { console.error(e); }); }; useEffect(() => { fetchAvailability(selectedDate); }, [selectedDate]); if (loading) { return ; } return (
Here is an overview of your day on {selectedDate.format("D MMMM YYYY")}: Tip: Hover over the bold times for a full timestamp
Your day starts at {convertMinsToHrsMins(user.startTime)}
{availability.map((slot) => (
Your calendar shows you as busy between{" "} {dayjs(slot.start).format("HH:mm")} {" "} and{" "} {dayjs(slot.end).format("HH:mm")} {" "} on {dayjs(slot.start).format("D MMMM YYYY")}
))} {availability.length === 0 && }
Your day ends at {convertMinsToHrsMins(user.endTime)}
); } export const getServerSideProps: GetServerSideProps = async (context) => { const session = await getSession(context); if (!session) { return { redirect: { permanent: false, destination: "/auth/login" } }; } const user = await prisma.user.findFirst({ where: { id: session.user.id, }, select: { startTime: true, endTime: true, username: true, }, }); return { props: { user }, }; };