Scheduling infrastructure for absolutely everyone.
fix: resetting input on cancel and refactoring input fiedl (#5522)
Udit Takkar7ce5096f135c
diff --git a/apps/web/components/getting-started/steps-views/UserSettings.tsx b/apps/web/components/getting-started/steps-views/UserSettings.tsxindex 04f0285844f..9e1ae555624 100644--- a/apps/web/components/getting-started/steps-views/UserSettings.tsx+++ b/apps/web/components/getting-started/steps-views/UserSettings.tsx@@ -1,13 +1,13 @@import { ArrowRightIcon } from "@heroicons/react/outline";import { useState } from "react";-import { Controller, useForm } from "react-hook-form";+import { useForm } from "react-hook-form";import dayjs from "@calcom/dayjs";import { useLocale } from "@calcom/lib/hooks/useLocale";import { trpc } from "@calcom/trpc/react";import { Button, TimezoneSelect } from "@calcom/ui";-import { UsernameAvailability } from "@components/ui/UsernameAvailability";+import { UsernameAvailabilityField } from "@components/ui/UsernameAvailability";import type { IOnboardingPageProps } from "../../../pages/getting-started/[[...step]]";@@ -24,11 +24,9 @@ const UserSettings = (props: IUserSettingsProps) => {register,handleSubmit,formState: { errors },- control,} = useForm({defaultValues: {name: user?.name || "",- username: user?.username || "",},reValidateMode: "onChange",});@@ -50,27 +48,11 @@ const UserSettings = (props: IUserSettingsProps) => {});});- const [currentUsername, setCurrentUsername] = useState(user.username || "");-return (<form onSubmit={onSubmit}><div className="space-y-6">{/* Username textfield */}- <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}- />- )}- />+ <UsernameAvailabilityField user={user} />{/* Full name textfield */}<div className="w-full">diff --git a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsxindex b1edd1b9197..55878552c82 100644--- a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx+++ b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx@@ -124,6 +124,7 @@ const UsernameTextfield = (props: ICustomUsernameProps) => {<Inputref={usernameRef}name="username"+ value={inputUsernameValue}autoComplete="none"autoCapitalize="none"autoCorrect="none"@@ -133,7 +134,6 @@ const UsernameTextfield = (props: ICustomUsernameProps) => {? "focus:shadow-0 focus:ring-shadow-0 border-red-500 focus:border-red-500 focus:outline-none focus:ring-0": "")}- defaultValue={currentUsername}onChange={(event) => {event.preventDefault();setInputUsernameValue(event.target.value);diff --git a/apps/web/components/ui/UsernameAvailability/index.tsx b/apps/web/components/ui/UsernameAvailability/index.tsxindex ef0b8e1a33e..f0c5f498195 100644--- a/apps/web/components/ui/UsernameAvailability/index.tsx+++ b/apps/web/components/ui/UsernameAvailability/index.tsx@@ -1,6 +1,67 @@+import { useState } from "react";+import { Controller, useForm } from "react-hook-form";+import { IS_SELF_HOSTED } from "@calcom/lib/constants";+import { User } from "@calcom/prisma/client";+import { TRPCClientErrorLike } from "@calcom/trpc/client";+import { AppRouter } from "@calcom/trpc/server/routers/_app";import { PremiumTextfield } from "./PremiumTextfield";import { UsernameTextfield } from "./UsernameTextfield";export const UsernameAvailability = IS_SELF_HOSTED ? UsernameTextfield : PremiumTextfield;++interface UsernameAvailabilityFieldProps {+ onSuccessMutation?: () => void;+ onErrorMutation?: (error: TRPCClientErrorLike<AppRouter>) => void;+ user: Pick<+ User,+ | "username"+ | "name"+ | "email"+ | "bio"+ | "avatar"+ | "timeZone"+ | "weekStart"+ | "hideBranding"+ | "theme"+ | "plan"+ | "brandColor"+ | "darkBrandColor"+ | "timeFormat"+ | "metadata"+ >;+}+export const UsernameAvailabilityField = ({+ onSuccessMutation,+ onErrorMutation,+ user,+}: UsernameAvailabilityFieldProps) => {+ const [currentUsername, setCurrentUsername] = useState(user.username ?? "");+ const formMethods = useForm({+ defaultValues: {+ username: currentUsername,+ },+ });++ return (+ <Controller+ control={formMethods.control}+ name="username"+ render={({ field: { ref, onChange, value } }) => {+ return (+ <UsernameAvailability+ currentUsername={currentUsername}+ setCurrentUsername={setCurrentUsername}+ inputUsernameValue={value}+ usernameRef={ref}+ setInputUsernameValue={onChange}+ onSuccessMutation={onSuccessMutation}+ onErrorMutation={onErrorMutation}+ user={user}+ />+ );+ }}+ />+ );+};diff --git a/apps/web/pages/settings/my-account/profile.tsx b/apps/web/pages/settings/my-account/profile.tsxindex d5d3d51ce6b..b61ccf53603 100644--- a/apps/web/pages/settings/my-account/profile.tsx+++ b/apps/web/pages/settings/my-account/profile.tsx@@ -35,7 +35,7 @@ import {} from "@calcom/ui";import TwoFactor from "@components/auth/TwoFactor";-import { UsernameAvailability } from "@components/ui/UsernameAvailability";+import { UsernameAvailabilityField } from "@components/ui/UsernameAvailability";const SkeletonLoader = () => {return (@@ -246,22 +246,10 @@ const ProfileView = () => {/></div><div className="mt-8">- <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}- />- );- }}+ <UsernameAvailabilityField+ user={user}+ onSuccessMutation={onSuccessfulUsernameUpdate}+ onErrorMutation={onErrorInUsernameUpdate}/></div><div className="mt-8">