diff --git a/apps/web/components/getting-started/steps-views/UserSettings.tsx b/apps/web/components/getting-started/steps-views/UserSettings.tsx index e45ed6aff1..3e04fdc2b9 100644 --- a/apps/web/components/getting-started/steps-views/UserSettings.tsx +++ b/apps/web/components/getting-started/steps-views/UserSettings.tsx @@ -1,6 +1,6 @@ import { ArrowRightIcon } from "@heroicons/react/outline"; import { useRef, useState } from "react"; -import { useForm } from "react-hook-form"; +import { Controller, useForm } from "react-hook-form"; import dayjs from "@calcom/dayjs"; import { useLocale } from "@calcom/lib/hooks/useLocale"; @@ -16,21 +16,22 @@ interface IUserSettingsProps { nextStep: () => void; } -type FormData = { - name: string; -}; - const UserSettings = (props: IUserSettingsProps) => { const { user, nextStep } = props; const { t } = useLocale(); const [selectedTimeZone, setSelectedTimeZone] = useState(user.timeZone ?? dayjs.tz.guess()); - const { register, handleSubmit, formState } = useForm({ + const { + register, + handleSubmit, + formState: { errors }, + control, + } = useForm({ defaultValues: { - name: user?.name || undefined, + name: user?.name || "", + username: user?.username || "", }, reValidateMode: "onChange", }); - const { errors } = formState; const defaultOptions = { required: true, maxLength: 255 }; const utils = trpc.useContext(); @@ -52,22 +53,27 @@ const UserSettings = (props: IUserSettingsProps) => { timeZone: selectedTimeZone, }); }); - const [currentUsername, setCurrentUsername] = useState(user.username || undefined); - const [inputUsernameValue, setInputUsernameValue] = useState(currentUsername); - const usernameRef = useRef(null!); + + const [currentUsername, setCurrentUsername] = useState(user.username || ""); return (
{/* Username textfield */} - ( + + )} /> {/* Full name textfield */} diff --git a/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx b/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx index d14f613737..612c33890f 100644 --- a/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx +++ b/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx @@ -1,7 +1,7 @@ import classNames from "classnames"; -import { debounce } from "lodash"; +import { debounce, noop } from "lodash"; import { useRouter } from "next/router"; -import { MutableRefObject, useCallback, useEffect, useState } from "react"; +import { RefCallback, useMemo, useEffect, useState } from "react"; import { getPremiumPlanMode, getPremiumPlanPriceValue } from "@calcom/app-store/stripepayment/lib/utils"; import { fetchUsername } from "@calcom/lib/fetchUsername"; @@ -24,9 +24,9 @@ export enum UsernameChangeStatusEnum { interface ICustomUsernameProps { currentUsername: string | undefined; - setCurrentUsername: (value: string | undefined) => void; + setCurrentUsername?: (newUsername: string) => void; inputUsernameValue: string | undefined; - usernameRef: MutableRefObject; + usernameRef: RefCallback; setInputUsernameValue: (value: string) => void; onSuccessMutation?: () => void; onErrorMutation?: (error: TRPCClientErrorLike) => void; @@ -73,7 +73,7 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { const { t } = useLocale(); const { currentUsername, - setCurrentUsername, + setCurrentUsername = noop, inputUsernameValue, setInputUsernameValue, usernameRef, @@ -91,13 +91,14 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { const isCurrentUsernamePremium = user && user.metadata && hasKeyInMetadata(user, "isPremium") ? !!user.metadata.isPremium : false; const [isInputUsernamePremium, setIsInputUsernamePremium] = useState(false); - const debouncedApiCall = useCallback( - debounce(async (username: string) => { - const { data } = await fetchUsername(username); - setMarkAsError(!data.available && !!currentUsername && username !== currentUsername); - setIsInputUsernamePremium(data.premium); - setUsernameIsAvailable(data.available); - }, 150), + const debouncedApiCall = useMemo( + () => + debounce(async (username: string) => { + const { data } = await fetchUsername(username); + setMarkAsError(!data.available && !!currentUsername && username !== currentUsername); + setIsInputUsernamePremium(data.premium); + setUsernameIsAvailable(data.available); + }, 150), [] ); @@ -107,7 +108,10 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { }, [setInputUsernameValue, currentUsername, stripeCustomer?.username]); useEffect(() => { - if (!inputUsernameValue) return; + if (!inputUsernameValue) { + debouncedApiCall.cancel(); + return; + } debouncedApiCall(inputUsernameValue); }, [debouncedApiCall, inputUsernameValue]); @@ -115,7 +119,6 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { const updateUsername = trpc.useMutation("viewer.updateProfile", { onSuccess: async () => { onSuccessMutation && (await onSuccessMutation()); - setCurrentUsername(inputUsernameValue); setOpenDialogSaveUsername(false); }, onError: (error) => { @@ -173,9 +176,6 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { onClick={() => { if (currentUsername) { setInputUsernameValue(currentUsername); - if (usernameRef.current) { - usernameRef.current.value = currentUsername; - } } }}> {t("cancel")} @@ -191,6 +191,7 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { updateUsername.mutate({ username: inputUsernameValue, }); + setCurrentUsername(inputUsernameValue); } }; diff --git a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx index 6be9c1bcd5..b40ce477bf 100644 --- a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx +++ b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx @@ -1,6 +1,6 @@ import classNames from "classnames"; -import { debounce } from "lodash"; -import { MutableRefObject, useCallback, useEffect, useState } from "react"; +import { debounce, noop } from "lodash"; +import { RefCallback, useEffect, useMemo, useState } from "react"; import { fetchUsername } from "@calcom/lib/fetchUsername"; import { useLocale } from "@calcom/lib/hooks/useLocale"; @@ -13,9 +13,9 @@ import { Button, Input, Label } from "@calcom/ui/components"; interface ICustomUsernameProps { currentUsername: string | undefined; - setCurrentUsername: (value: string | undefined) => void; + setCurrentUsername?: (newUsername: string) => void; inputUsernameValue: string | undefined; - usernameRef: MutableRefObject; + usernameRef: RefCallback; setInputUsernameValue: (value: string) => void; onSuccessMutation?: () => void; onErrorMutation?: (error: TRPCClientErrorLike) => void; @@ -25,7 +25,7 @@ const UsernameTextfield = (props: ICustomUsernameProps) => { const { t } = useLocale(); const { currentUsername, - setCurrentUsername, + setCurrentUsername = noop, inputUsernameValue, setInputUsernameValue, usernameRef, @@ -36,32 +36,36 @@ const UsernameTextfield = (props: ICustomUsernameProps) => { const [markAsError, setMarkAsError] = useState(false); const [openDialogSaveUsername, setOpenDialogSaveUsername] = useState(false); - const debouncedApiCall = useCallback( - debounce(async (username) => { - const { data } = await fetchUsername(username); - setMarkAsError(!data.available); - setUsernameIsAvailable(data.available); - }, 150), + const debouncedApiCall = useMemo( + () => + debounce(async (username) => { + const { data } = await fetchUsername(username); + setMarkAsError(!data.available); + setUsernameIsAvailable(data.available); + }, 150), [] ); useEffect(() => { + if (!inputUsernameValue) { + debouncedApiCall.cancel(); + setUsernameIsAvailable(false); + setMarkAsError(false); + return; + } + if (currentUsername !== inputUsernameValue) { debouncedApiCall(inputUsernameValue); - } else if (inputUsernameValue === "") { - setMarkAsError(false); - setUsernameIsAvailable(false); } else { setUsernameIsAvailable(false); } - }, [inputUsernameValue]); + }, [inputUsernameValue, debouncedApiCall, currentUsername]); const utils = trpc.useContext(); - const updateUsername = trpc.useMutation("viewer.updateProfile", { + const updateUsernameMutation = trpc.useMutation("viewer.updateProfile", { onSuccess: async () => { onSuccessMutation && (await onSuccessMutation()); - setCurrentUsername(inputUsernameValue); setOpenDialogSaveUsername(false); }, onError: (error) => { @@ -89,9 +93,6 @@ const UsernameTextfield = (props: ICustomUsernameProps) => { onClick={() => { if (currentUsername) { setInputUsernameValue(currentUsername); - if (usernameRef.current) { - usernameRef.current.value = currentUsername; - } } }}> {t("cancel")} @@ -102,6 +103,13 @@ const UsernameTextfield = (props: ICustomUsernameProps) => { ); }; + const updateUsername = async () => { + await updateUsernameMutation.mutate({ + username: inputUsernameValue, + }); + setCurrentUsername(inputUsernameValue); + }; + return (
@@ -182,13 +190,9 @@ const UsernameTextfield = (props: ICustomUsernameProps) => {
diff --git a/apps/web/pages/settings/my-account/profile.tsx b/apps/web/pages/settings/my-account/profile.tsx index c187c33933..78af9a28d2 100644 --- a/apps/web/pages/settings/my-account/profile.tsx +++ b/apps/web/pages/settings/my-account/profile.tsx @@ -49,7 +49,6 @@ interface DeleteAccountValues { const ProfileView = () => { const { t } = useLocale(); const utils = trpc.useContext(); - const usernameRef = useRef(null); const { data: user, isLoading } = trpc.useQuery(["viewer.me"]); const mutation = trpc.useMutation("viewer.updateProfile", { @@ -66,11 +65,7 @@ const ProfileView = () => { const [deleteAccountOpen, setDeleteAccountOpen] = useState(false); const [hasDeleteErrors, setHasDeleteErrors] = useState(false); const [deleteErrorMessage, setDeleteErrorMessage] = useState(""); - const [currentUsername, setCurrentUsername] = useState(user?.username || undefined); - const [inputUsernameValue, setInputUsernameValue] = useState(currentUsername); - useEffect(() => { - if (user?.username) setCurrentUsername(user?.username); - }, [user?.username]); + const form = useForm(); const emailMd5 = crypto @@ -148,28 +143,36 @@ const ProfileView = () => { } }; - const formMethods = useForm<{ - avatar?: string; - username?: string; - name?: string; - email?: string; - bio?: string; - }>(); + const formMethods = useForm({ + defaultValues: { + username: "", + avatar: "", + name: "", + email: "", + bio: "", + }, + }); + + const { + reset, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + formState: { dirtyFields }, + } = formMethods; - const { reset } = formMethods; - const formInitializedRef = useRef(false); useEffect(() => { - // The purpose of reset is to set the initial value obtained from tRPC. - // `user` would change for many reasons (e.g. when viewer.me automatically fetches on window re-focus(a react query feature)) - if (user && !formInitializedRef.current) { - formInitializedRef.current = true; - reset({ - avatar: user?.avatar || "", - username: user?.username || "", - name: user?.name || "", - email: user?.email || "", - bio: user?.bio || "", - }); + if (user) { + reset( + { + avatar: user?.avatar || "", + username: user?.username || "", + name: user?.name || "", + email: user?.email || "", + bio: user?.bio || "", + }, + { + keepDirtyValues: true, + } + ); } }, [reset, user]); @@ -230,15 +233,22 @@ const ProfileView = () => { />
- { + return ( + + ); + }} />