import type { InferGetServerSidePropsType, NextPage } from "next"; import { useRouter } from "next/router"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import axios from "axios"; import { withPageAuthRequired } from "../../../lib/session-helpers"; import OnboardingLayout from "../../components/welcome/onboarding-layout"; import clsx from "clsx"; import { findCustomer } from "../../database/customer"; type Props = InferGetServerSidePropsType; type Form = { twilioAccountSid: string; twilioAuthToken: string; } const StepTwo: NextPage = ({ accountSid, authToken }) => { const { register, handleSubmit, setValue, formState: { isSubmitting }, } = useForm
(); const router = useRouter(); useEffect(() => { setValue("twilioAuthToken", authToken); setValue("twilioAccountSid", accountSid); }); const onSubmit = handleSubmit(async ({ twilioAccountSid, twilioAuthToken }) => { if (isSubmitting) { return; } await axios.post("/api/user/update-user", { twilioAccountSid, twilioAuthToken, }, { withCredentials: true }); await router.push("/welcome/step-three"); }); const hasTwilioCredentials = accountSid.length > 0 && authToken.length > 0; return (
); }; export const getServerSideProps = withPageAuthRequired(async (context, user) => { const customer = await findCustomer(user.id); return { props: { accountSid: customer.accountSid ?? "", authToken: customer.authToken ?? "", }, }; }); export default StepTwo;