From c0016a55f745c33c7b7f3f4908e3259ee3c72480 Mon Sep 17 00:00:00 2001 From: m5r Date: Sat, 4 Dec 2021 00:46:03 +0100 Subject: [PATCH] use error boundary in phone number form --- app/core/components/alert.tsx | 6 +- .../components/phone/phone-number-form.tsx | 176 ++++++++++-------- 2 files changed, 106 insertions(+), 76 deletions(-) diff --git a/app/core/components/alert.tsx b/app/core/components/alert.tsx index c517aa4..c3806f2 100644 --- a/app/core/components/alert.tsx +++ b/app/core/components/alert.tsx @@ -1,4 +1,4 @@ -import type { ReactElement } from "react"; +import type { ReactElement, ReactChild } from "react"; type AlertVariant = "error" | "success" | "info" | "warning"; @@ -10,8 +10,8 @@ type AlertVariantProps = { }; type Props = { - title: string; - message: string; + title: ReactChild; + message: ReactChild; variant: AlertVariant; }; diff --git a/app/settings/components/phone/phone-number-form.tsx b/app/settings/components/phone/phone-number-form.tsx index 0208d6c..d2ddf14 100644 --- a/app/settings/components/phone/phone-number-form.tsx +++ b/app/settings/components/phone/phone-number-form.tsx @@ -1,5 +1,5 @@ import { useEffect } from "react"; -import { useMutation, useQuery } from "blitz"; +import { useMutation, useQuery, withErrorBoundary } from "blitz"; import { useForm } from "react-hook-form"; import setPhoneNumber from "../../mutations/set-phone-number"; @@ -14,83 +14,113 @@ type Form = { phoneNumberSid: string; }; -export default function PhoneNumberForm() { - const { hasFilledTwilioCredentials } = useCurrentUser(); - const currentPhoneNumber = useUserPhoneNumber(); - const { - register, - handleSubmit, - setValue, - formState: { isSubmitting }, - } = useForm
(); - const [setPhoneNumberMutation, { error, isError, isSuccess }] = useMutation(setPhoneNumber); - const [availablePhoneNumbers] = useQuery(getAvailablePhoneNumbers, {}, { enabled: hasFilledTwilioCredentials }); +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]); + useEffect(() => { + if (currentPhoneNumber) { + setValue("phoneNumberSid", currentPhoneNumber.id); + } + }, [currentPhoneNumber]); - const onSubmit = handleSubmit(async ({ phoneNumberSid }) => { - if (isSubmitting) { - return; + const onSubmit = handleSubmit(async ({ phoneNumberSid }) => { + if (isSubmitting) { + return; + } + + await setPhoneNumberMutation({ phoneNumberSid }); + }); + + if (!hasFilledTwilioCredentials) { + return null; } - await setPhoneNumberMutation({ phoneNumberSid }); - }); - - if (!hasFilledTwilioCredentials) { - return null; - } - - return ( - - - - - } - > - {isError ? ( -
- -
- ) : null} - - {isSuccess ? ( -
- -
- ) : null} - - - -
-
- ); -} + {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) {