* Refactored Schedule component * Merge branch 'main' into feature/availability-page-revamp * wip * Turned value into number, many other TS tweaks * NodeJS 16x works 100% on my local, but out of scope for this already massive PR * Fixed TS errors in viewer.tsx and schedule/index.ts * Reverted next.config.js * Fixed minor remnant from moving types to @lib/types * schema comment * some changes to form handling * add comments * Turned ConfigType into number; which seems to be the value preferred by tRPC * Fixed localized time display during onboarding * Update components/ui/form/Schedule.tsx Co-authored-by: Alex Johansson <alexander@n1s.se> * Added showToast to indicate save success * Converted number to Date, and also always establish time based on current date * prevent height flickering of availability by removing mb-2 of input field * availabilty: re-added mb-2 but added min-height * Quite a few bugs discovered, but this seems functional Co-authored-by: KATT <alexander@n1s.se> Co-authored-by: Bailey Pumfleet <pumfleet@hey.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
		
			
				
	
	
		
			87 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import Link from "next/link";
 | 
						|
import { useForm } from "react-hook-form";
 | 
						|
 | 
						|
import { QueryCell } from "@lib/QueryCell";
 | 
						|
import { useLocale } from "@lib/hooks/useLocale";
 | 
						|
import showToast from "@lib/notification";
 | 
						|
import { inferQueryOutput, trpc } from "@lib/trpc";
 | 
						|
import { Schedule as ScheduleType } from "@lib/types/schedule";
 | 
						|
 | 
						|
import Shell from "@components/Shell";
 | 
						|
import { Form } from "@components/form/fields";
 | 
						|
import Button from "@components/ui/Button";
 | 
						|
import Schedule, { DEFAULT_SCHEDULE } from "@components/ui/form/Schedule";
 | 
						|
 | 
						|
type FormValues = {
 | 
						|
  schedule: ScheduleType;
 | 
						|
};
 | 
						|
 | 
						|
export function AvailabilityForm(props: inferQueryOutput<"viewer.availability">) {
 | 
						|
  const { t } = useLocale();
 | 
						|
 | 
						|
  const createSchedule = async ({ schedule }: FormValues) => {
 | 
						|
    const res = await fetch(`/api/schedule`, {
 | 
						|
      method: "POST",
 | 
						|
      body: JSON.stringify({ schedule }),
 | 
						|
      headers: {
 | 
						|
        "Content-Type": "application/json",
 | 
						|
      },
 | 
						|
    });
 | 
						|
 | 
						|
    if (!res.ok) {
 | 
						|
      throw new Error((await res.json()).message);
 | 
						|
    }
 | 
						|
    const responseData = await res.json();
 | 
						|
    showToast(t("availability_updated_successfully"), "success");
 | 
						|
    return responseData.data;
 | 
						|
  };
 | 
						|
 | 
						|
  const form = useForm({
 | 
						|
    defaultValues: {
 | 
						|
      schedule: props.schedule || DEFAULT_SCHEDULE,
 | 
						|
    },
 | 
						|
  });
 | 
						|
  return (
 | 
						|
    <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>
 | 
						|
          </div>
 | 
						|
          <div className="mt-5">
 | 
						|
            <Link href="/availability/troubleshoot">
 | 
						|
              <a className="btn btn-white">{t("launch_troubleshooter")}</a>
 | 
						|
            </Link>
 | 
						|
          </div>
 | 
						|
        </div>
 | 
						|
      </div>
 | 
						|
    </div>
 | 
						|
  );
 | 
						|
}
 | 
						|
 | 
						|
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} />} />
 | 
						|
      </Shell>
 | 
						|
    </div>
 | 
						|
  );
 | 
						|
}
 |