import { ClockIcon } from "@heroicons/react/outline";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { useLocale } from "@lib/hooks/useLocale";
import { useToggleQuery } from "@lib/hooks/useToggleQuery";
import showToast from "@lib/notification";
import { trpc } from "@lib/trpc";
import { Dialog, DialogContent } from "@components/Dialog";
import Loader from "@components/Loader";
import Shell from "@components/Shell";
import { Alert } from "@components/ui/Alert";
import Button from "@components/ui/Button";
function convertMinsToHrsMins(mins: number) {
const h = Math.floor(mins / 60);
const m = mins % 60;
const hours = h < 10 ? "0" + h : h;
const minutes = m < 10 ? "0" + m : m;
return `${hours}:${minutes}`;
}
export default function Availability() {
const { t } = useLocale();
const queryMe = trpc.useQuery(["viewer.me"]);
const formModal = useToggleQuery("edit");
const formMethods = useForm<{
startHours: string;
startMins: string;
endHours: string;
endMins: string;
bufferHours: string;
bufferMins: string;
}>({});
const router = useRouter();
useEffect(() => {
/**
* This hook populates the form with new values as soon as the user is loaded or changes
*/
const user = queryMe.data;
if (formMethods.formState.isDirty || !user) {
return;
}
formMethods.reset({
startHours: convertMinsToHrsMins(user.startTime).split(":")[0],
startMins: convertMinsToHrsMins(user.startTime).split(":")[1],
endHours: convertMinsToHrsMins(user.endTime).split(":")[0],
endMins: convertMinsToHrsMins(user.endTime).split(":")[1],
bufferHours: convertMinsToHrsMins(user.bufferTime).split(":")[0],
bufferMins: convertMinsToHrsMins(user.bufferTime).split(":")[1],
});
}, [formMethods, queryMe.data]);
if (queryMe.status === "loading") {
return
{t("current_start_date")} {convertMinsToHrsMins(user.startTime)} {t("and_end_at")}{" "} {convertMinsToHrsMins(user.endTime)}.