2021-07-18 13:12:18 +00:00
|
|
|
import Link from "next/link";
|
2021-09-30 21:37:52 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
import { QueryCell } from "@lib/QueryCell";
|
2021-10-13 10:49:15 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-09-30 21:37:52 +00:00
|
|
|
import showToast from "@lib/notification";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { inferQueryOutput, trpc } from "@lib/trpc";
|
|
|
|
import { Schedule as ScheduleType } from "@lib/types/schedule";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
|
|
|
import Shell from "@components/Shell";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { Form } from "@components/form/fields";
|
2021-09-30 21:37:52 +00:00
|
|
|
import Button from "@components/ui/Button";
|
2021-11-10 11:16:32 +00:00
|
|
|
import Schedule, { DEFAULT_SCHEDULE } from "@components/ui/form/Schedule";
|
2021-09-30 21:37:52 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
type FormValues = {
|
|
|
|
schedule: ScheduleType;
|
|
|
|
};
|
2021-09-30 21:37:52 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
export function AvailabilityForm(props: inferQueryOutput<"viewer.availability">) {
|
|
|
|
const { t } = useLocale();
|
2021-04-29 13:04:08 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
const createSchedule = async ({ schedule }: FormValues) => {
|
|
|
|
const res = await fetch(`/api/schedule`, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ schedule }),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2021-09-30 21:37:52 +00:00
|
|
|
});
|
2021-04-13 16:16:32 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error((await res.json()).message);
|
|
|
|
}
|
|
|
|
const responseData = await res.json();
|
|
|
|
showToast(t("availability_updated_successfully"), "success");
|
|
|
|
return responseData.data;
|
|
|
|
};
|
2021-04-08 14:20:38 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
const form = useForm({
|
|
|
|
defaultValues: {
|
|
|
|
schedule: props.schedule || DEFAULT_SCHEDULE,
|
|
|
|
},
|
|
|
|
});
|
2021-07-18 13:12:18 +00:00
|
|
|
return (
|
2021-11-10 11:16:32 +00:00
|
|
|
<div className="grid grid-cols-3 gap-2">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
await createSchedule(values);
|
|
|
|
}}
|
|
|
|
className="col-span-3 space-y-2 lg:col-span-2">
|
|
|
|
<div className="px-4 py-5 bg-white border border-gray-200 divide-y rounded-sm sm:p-6">
|
|
|
|
<h3 className="mb-4 text-lg font-semibold leading-6 text-gray-900">{t("change_start_end")}</h3>
|
|
|
|
<Schedule name="schedule" />
|
|
|
|
</div>
|
|
|
|
<div className="text-right">
|
|
|
|
<Button>{t("save")}</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
<div className="col-span-3 ml-2 lg:col-span-1 min-w-40">
|
|
|
|
<div className="px-4 py-5 border border-gray-200 rounded-sm sm:p-6 ">
|
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900">{t("something_doesnt_look_right")}</h3>
|
|
|
|
<div className="max-w-xl mt-2 text-sm text-gray-500">
|
|
|
|
<p>{t("troubleshoot_availability")}</p>
|
2021-07-18 13:12:18 +00:00
|
|
|
</div>
|
2021-11-10 11:16:32 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
<Link href="/availability/troubleshoot">
|
|
|
|
<a className="btn btn-white">{t("launch_troubleshooter")}</a>
|
|
|
|
</Link>
|
2021-07-18 13:12:18 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-11-10 11:16:32 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-04-08 14:20:38 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
export default function Availability() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const query = trpc.useQuery(["viewer.availability"]);
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Shell heading={t("availability")} subtitle={t("configure_availability")}>
|
|
|
|
<QueryCell query={query} success={({ data }) => <AvailabilityForm {...data} />} />
|
2021-07-18 13:12:18 +00:00
|
|
|
</Shell>
|
|
|
|
</div>
|
|
|
|
);
|
2021-04-08 14:20:38 +00:00
|
|
|
}
|