import type { BlitzPage } from "blitz"; import { Routes, useMutation, useRouter } from "blitz"; import clsx from "clsx"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import OnboardingLayout from "../../components/onboarding-layout"; import useCurrentCustomer from "../../../core/hooks/use-current-customer"; import setTwilioApiFields from "../../mutations/set-twilio-api-fields"; type Form = { twilioAccountSid: string; twilioAuthToken: string; }; const StepTwo: BlitzPage = () => { const { register, handleSubmit, setValue, formState: { isSubmitting }, } = useForm
(); const router = useRouter(); const { customer } = useCurrentCustomer(); const [setTwilioApiFieldsMutation] = useMutation(setTwilioApiFields); const initialAuthToken = customer?.authToken ?? ""; const initialAccountSid = customer?.accountSid ?? ""; const hasTwilioCredentials = initialAccountSid.length > 0 && initialAuthToken.length > 0; useEffect(() => { setValue("twilioAuthToken", initialAuthToken); setValue("twilioAccountSid", initialAccountSid); }, [initialAuthToken, initialAccountSid]); const onSubmit = handleSubmit(async ({ twilioAccountSid, twilioAuthToken }) => { if (isSubmitting) { return; } await setTwilioApiFieldsMutation({ twilioAccountSid, twilioAuthToken, }); await router.push(Routes.StepThree()); }); return (
); }; StepTwo.authenticate = true; export default StepTwo;