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 ; } if (queryMe.status !== "success") { return ; } const user = queryMe.data; return (

{t("change_start_end")}

{t("current_start_date")} {convertMinsToHrsMins(user.startTime)} {t("and_end_at")}{" "} {convertMinsToHrsMins(user.endTime)}.

{t("something_doesnt_look_right")}

{t("troubleshoot_availability")}

{ router.push(isOpen ? formModal.hrefOn : formModal.hrefOff); }}>

{t("change_start_end_buffer")}

{ const startMins = parseInt(values.startHours) * 60 + parseInt(values.startMins); const endMins = parseInt(values.endHours) * 60 + parseInt(values.endMins); const bufferMins = parseInt(values.bufferHours) * 60 + parseInt(values.bufferMins); // TODO: Add validation // eslint-disable-next-line @typescript-eslint/no-unused-vars const response = await fetch("/api/availability/day", { method: "PATCH", body: JSON.stringify({ start: startMins, end: endMins, buffer: bufferMins }), headers: { "Content-Type": "application/json", }, }); if (!response.ok) { showToast(t("something_went_wrong"), "error"); return; } await queryMe.refetch(); router.push(formModal.hrefOff); showToast(t("start_end_changed_successfully"), "success"); })}>
:
:
:
); }