fix: update form methods when username is updated (#5403)
* fix: update form methods when username is updated * Using RHF to decrease state logic in profile.tsx for UsernameAvailability (#5405) * Type fixes & fixed onboarding * Removed undefined allowed value + removed invalidation * No longer need usernameRef.current.value Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
1 parent
69100a0fe8
commit
2ba5e6cdd4
4 files changed
+118
-97
No files matched your search
@@ -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<FormData>({
|
||||
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<HTMLInputElement>(null!);
|
||||
|
||||
const [currentUsername, setCurrentUsername] = useState(user.username || "");
|
||||
|
||||
return (
|
||||
<form onSubmit={onSubmit}>
|
||||
<div className="space-y-6">
|
||||
{/* Username textfield */}
|
||||
<UsernameAvailability
|
||||
readonly={true}
|
||||
currentUsername={currentUsername}
|
||||
setCurrentUsername={setCurrentUsername}
|
||||
inputUsernameValue={inputUsernameValue}
|
||||
usernameRef={usernameRef}
|
||||
setInputUsernameValue={setInputUsernameValue}
|
||||
user={user}
|
||||
<Controller
|
||||
control={control}
|
||||
name="username"
|
||||
render={({ field: { value, ref, onChange } }) => (
|
||||
<UsernameAvailability
|
||||
readonly={true}
|
||||
currentUsername={currentUsername}
|
||||
setCurrentUsername={setCurrentUsername}
|
||||
inputUsernameValue={value}
|
||||
usernameRef={ref}
|
||||
setInputUsernameValue={onChange}
|
||||
user={user}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Full name textfield */}
|
||||
|
||||
@@ -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<HTMLInputElement | null>;
|
||||
usernameRef: RefCallback<HTMLInputElement>;
|
||||
setInputUsernameValue: (value: string) => void;
|
||||
onSuccessMutation?: () => void;
|
||||
onErrorMutation?: (error: TRPCClientErrorLike<AppRouter>) => 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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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<HTMLInputElement | null>;
|
||||
usernameRef: RefCallback<HTMLInputElement>;
|
||||
setInputUsernameValue: (value: string) => void;
|
||||
onSuccessMutation?: () => void;
|
||||
onErrorMutation?: (error: TRPCClientErrorLike<AppRouter>) => 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 (
|
||||
<div>
|
||||
<div>
|
||||
@@ -182,13 +190,9 @@ const UsernameTextfield = (props: ICustomUsernameProps) => {
|
||||
<div className="mt-4 flex flex-row-reverse gap-x-2">
|
||||
<Button
|
||||
type="button"
|
||||
loading={updateUsername.isLoading}
|
||||
loading={updateUsernameMutation.isLoading}
|
||||
data-testid="save-username"
|
||||
onClick={() => {
|
||||
updateUsername.mutate({
|
||||
username: inputUsernameValue,
|
||||
});
|
||||
}}>
|
||||
onClick={updateUsername}>
|
||||
{t("save")}
|
||||
</Button>
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ interface DeleteAccountValues {
|
||||
const ProfileView = () => {
|
||||
const { t } = useLocale();
|
||||
const utils = trpc.useContext();
|
||||
const usernameRef = useRef<HTMLInputElement>(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<string | undefined>(user?.username || undefined);
|
||||
const [inputUsernameValue, setInputUsernameValue] = useState(currentUsername);
|
||||
useEffect(() => {
|
||||
if (user?.username) setCurrentUsername(user?.username);
|
||||
}, [user?.username]);
|
||||
|
||||
const form = useForm<DeleteAccountValues>();
|
||||
|
||||
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 = () => {
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-8">
|
||||
<UsernameAvailability
|
||||
currentUsername={currentUsername}
|
||||
setCurrentUsername={setCurrentUsername}
|
||||
inputUsernameValue={inputUsernameValue}
|
||||
usernameRef={usernameRef}
|
||||
setInputUsernameValue={setInputUsernameValue}
|
||||
onSuccessMutation={onSuccessfulUsernameUpdate}
|
||||
onErrorMutation={onErrorInUsernameUpdate}
|
||||
user={user}
|
||||
<Controller
|
||||
control={formMethods.control}
|
||||
name="username"
|
||||
render={({ field: { ref, onChange, value } }) => {
|
||||
return (
|
||||
<UsernameAvailability
|
||||
currentUsername={user?.username || ""}
|
||||
inputUsernameValue={value}
|
||||
usernameRef={ref}
|
||||
setInputUsernameValue={onChange}
|
||||
onSuccessMutation={onSuccessfulUsernameUpdate}
|
||||
onErrorMutation={onErrorInUsernameUpdate}
|
||||
user={user}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-8">
|
||||
|
||||
Reference in new issue
Block a user