import { useEffect } from "react"; import { useMutation, useQuery, withErrorBoundary } from "blitz"; import { useForm } from "react-hook-form"; import setPhoneNumber from "../../mutations/set-phone-number"; import getAvailablePhoneNumbers from "../../queries/get-available-phone-numbers"; import useCurrentUser from "app/core/hooks/use-current-user"; import useUserPhoneNumber from "app/core/hooks/use-current-phone-number"; import Button from "../button"; import SettingsSection from "../settings-section"; import Alert from "app/core/components/alert"; type Form = { phoneNumberSid: string; }; export default withErrorBoundary( function PhoneNumberForm() { const { organization } = useCurrentUser(); const currentPhoneNumber = useUserPhoneNumber(); const { register, handleSubmit, setValue, formState: { isSubmitting }, } = useForm
(); const [setPhoneNumberMutation, { error, isError, isSuccess }] = useMutation(setPhoneNumber); const hasFilledTwilioCredentials = Boolean(organization?.twilioAccountSid && organization?.twilioAuthToken); const [availablePhoneNumbers] = useQuery(getAvailablePhoneNumbers, {}, { enabled: hasFilledTwilioCredentials }); useEffect(() => { if (currentPhoneNumber) { setValue("phoneNumberSid", currentPhoneNumber.id); } }, [currentPhoneNumber]); const onSubmit = handleSubmit(async ({ phoneNumberSid }) => { if (isSubmitting) { return; } await setPhoneNumberMutation({ phoneNumberSid }); }); if (!hasFilledTwilioCredentials) { return null; } return ( } > {isError ? (
) : null} {isSuccess ? (
) : null}
); }, { fallbackRender: ({ error, resetErrorBoundary }) => (

We failed to fetch your Twilio phone numbers. Make sure both your SID and your auth token are valid and that your Twilio account isn't suspended. {error.moreInfo ? Related Twilio docs : null}

} /> ), onError: (error) => console.log("error", error), }, ); function parseErrorMessage(error: Error | null): string { if (!error) { return ""; } if (error.name === "ZodError") { return JSON.parse(error.message)[0].message; } return error.message; }