calcom/pages/availability/troubleshoot.tsx

137 lines
4.5 KiB
TypeScript
Raw Normal View History

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import { GetServerSidePropsContext } from "next";
2021-10-13 10:49:15 +00:00
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
2021-08-09 09:24:39 +00:00
import { useEffect, useState } from "react";
import { getSession } from "@lib/auth";
2021-10-13 10:49:15 +00:00
import { getOrSetUserLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
import { useLocale } from "@lib/hooks/useLocale";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
import Loader from "@components/Loader";
import Shell from "@components/Shell";
dayjs.extend(utc);
export default function Troubleshoot({ user }: inferSSRProps<typeof getServerSideProps>) {
2021-10-13 10:49:15 +00:00
const { t } = useLocale();
2021-08-09 09:24:39 +00:00
const [loading, setLoading] = useState(true);
const [availability, setAvailability] = useState([]);
const [selectedDate, setSelectedDate] = useState(dayjs());
function convertMinsToHrsMins(mins: number) {
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) => {
2021-08-09 09:24:39 +00:00
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();
})
2021-08-09 09:24:39 +00:00
.then((availableIntervals) => {
setAvailability(availableIntervals.busy);
2021-08-09 09:24:39 +00:00
setLoading(false);
})
.catch((e) => {
console.error(e);
});
};
2021-08-09 09:24:39 +00:00
useEffect(() => {
fetchAvailability(selectedDate);
}, [selectedDate]);
2021-08-09 09:24:39 +00:00
if (loading) {
return <Loader />;
}
return (
<div>
2021-10-13 10:49:15 +00:00
<Shell heading={t("troubleshoot")} subtitle={t("troubleshoot_description")}>
<div className="bg-white max-w-xl overflow-hidden shadow rounded-sm">
<div className="px-4 py-5 sm:p-6">
2021-10-13 10:49:15 +00:00
{t("overview_of_day")}{" "}
<input
type="date"
className="inline border-none h-8 p-0"
defaultValue={selectedDate.format("YYYY-MM-DD")}
onBlur={(e) => {
setSelectedDate(dayjs(e.target.value));
}}
/>
2021-10-13 10:49:15 +00:00
<small className="block text-neutral-400">{t("hover_over_bold_times_tip")}</small>
<div className="mt-4 space-y-4">
<div className="bg-black overflow-hidden rounded-sm">
<div className="px-4 sm:px-6 py-2 text-white">
2021-10-13 10:49:15 +00:00
{t("your_day_starts_at")} {convertMinsToHrsMins(user.startTime)}
</div>
</div>
{availability.map((slot) => (
2021-07-30 23:05:38 +00:00
<div key={slot.start} className="bg-neutral-100 overflow-hidden rounded-sm">
<div className="px-4 py-5 sm:p-6 text-black">
2021-10-13 10:49:15 +00:00
{t("calendar_shows_busy_between")}{" "}
<span className="font-medium text-neutral-800" title={slot.start}>
{dayjs(slot.start).format("HH:mm")}
</span>{" "}
2021-10-13 10:49:15 +00:00
{t("and")}{" "}
<span className="font-medium text-neutral-800" title={slot.end}>
{dayjs(slot.end).format("HH:mm")}
</span>{" "}
2021-10-13 10:49:15 +00:00
{t("on")} {dayjs(slot.start).format("D")}{" "}
{t(dayjs(slot.start).format("MMMM").toLowerCase())} {dayjs(slot.start).format("YYYY")}
</div>
</div>
))}
2021-08-02 20:57:58 +00:00
{availability.length === 0 && <Loader />}
<div className="bg-black overflow-hidden rounded-sm">
<div className="px-4 sm:px-6 py-2 text-white">
2021-10-13 10:49:15 +00:00
{t("your_day_ends_at")} {convertMinsToHrsMins(user.endTime)}
</div>
</div>
</div>
</div>
</div>
</Shell>
</div>
);
}
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const session = await getSession(context);
2021-10-13 10:49:15 +00:00
const locale = await getOrSetUserLocaleFromHeaders(context.req);
if (!session?.user?.id) {
return { redirect: { permanent: false, destination: "/auth/login" } };
}
const user = await prisma.user.findFirst({
where: {
2021-08-09 09:24:39 +00:00
id: session.user.id,
},
select: {
startTime: true,
endTime: true,
2021-08-09 09:24:39 +00:00
username: true,
},
});
if (!user) return { redirect: { permanent: false, destination: "/auth/login" } };
return {
2021-10-13 10:49:15 +00:00
props: {
session,
user,
...(await serverSideTranslations(locale, ["common"])),
},
};
};