diff --git a/components/settings/settings-form.tsx b/components/settings/settings-form.tsx index f82b08e6..27eecb51 100644 --- a/components/settings/settings-form.tsx +++ b/components/settings/settings-form.tsx @@ -47,6 +47,13 @@ const defaultValues = { const ERROR_TOAST_DURATION = 5000; +type LoadingStates = { + isLoadingDetails: boolean; + isPendingGetUser: boolean; + isPendingUpdateUser: boolean; + isCancellingSignature: boolean; +}; + export const SettingsForm = () => { const { selectedAccount } = useAccountStore(); const { @@ -109,11 +116,6 @@ export const SettingsForm = () => { }; const cancelSignatureRequest = useCancelSignatureRequest(); - const isPending = - isPendingGetUser || - isLoadingDetails || - isPendingUpdateUser || - cancelSignatureRequest.isPending; const form = useForm({ resolver: zodResolver(formSchema), @@ -127,10 +129,30 @@ export const SettingsForm = () => { name: ["displayName", "avatar"], }); + const loadingStates: LoadingStates = { + isLoadingDetails, + isPendingGetUser, + isPendingUpdateUser, + isCancellingSignature: cancelSignatureRequest.isPending, + }; + + const isLoading = Object.values(loadingStates).some(Boolean); + + const isFormDisabled = isLoading || !!pendingUpdate; + + const isSubmitDisabled = + form.formState.isSubmitting || !form.formState.isValid || isFormDisabled; + + const shouldShowAvatar = + avatar && + isURL(avatar) && + !form.formState.isValidating && + !form.formState.errors.avatar; + const [updatedUserName, setUpdatedUserName] = useState(false); const [updatedUserNameEns, setUpdatedUserNameEns] = useState(false); useEffect(() => { - if (updatedUserName || isPending) return; + if (updatedUserName || isLoading) return; if (userData?.user?.display_name) { console.log( @@ -154,13 +176,13 @@ export const SettingsForm = () => { userData, updatedUserName, updatedUserNameEns, - isPending, + isLoading, ]); const [updatedAvatar, setUpdatedAvatar] = useState(false); const [updatedEnsAvatar, setUpdatedEnsAvatar] = useState(false); useEffect(() => { - if (updatedAvatar || isPending) return; + if (updatedAvatar || isLoading) return; if (userData?.user?.avatar) { console.log("Setting avatar from graphql", userData?.user?.avatar); @@ -181,7 +203,7 @@ export const SettingsForm = () => { userData, updatedAvatar, updatedEnsAvatar, - isPending, + isLoading, ]); // Reset form state when account changes @@ -241,18 +263,6 @@ export const SettingsForm = () => { } }; - const submitDisabled = - form.formState.isSubmitting || - !form.formState.isValid || - isPending || - !!pendingUpdate; - - const shouldShowAvatar = - avatar && - isURL(avatar) && - !form.formState.isValidating && - !form.formState.errors.avatar; - return (
@@ -267,7 +277,7 @@ export const SettingsForm = () => { Display name - + Max. 30 characters @@ -281,7 +291,7 @@ export const SettingsForm = () => { Image - + @@ -301,7 +311,7 @@ export const SettingsForm = () => { )} -