From 7ce5096f135c8a5d7d24cb79219ccbb26286628f Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Thu, 8 Dec 2022 01:45:15 +0530 Subject: [PATCH] fix: resetting input on cancel and refactoring input fiedl (#5522) * fix: resetting input on cancel and refactoring input fiedl * feat: add username field in /usersettings * chore: remove unused Signed-off-by: Udit Takkar Signed-off-by: Udit Takkar --- .../steps-views/UserSettings.tsx | 24 +------- .../UsernameTextfield.tsx | 2 +- .../ui/UsernameAvailability/index.tsx | 61 +++++++++++++++++++ .../web/pages/settings/my-account/profile.tsx | 22 ++----- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/apps/web/components/getting-started/steps-views/UserSettings.tsx b/apps/web/components/getting-started/steps-views/UserSettings.tsx index 04f0285844..9e1ae55562 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 (
{/* Username textfield */} - ( - - )} - /> + {/* Full name textfield */}
diff --git a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx index b1edd1b919..55878552c8 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) => { { ? "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.tsx index ef0b8e1a33..f0c5f49819 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) => 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 ( + { + return ( + + ); + }} + /> + ); +}; diff --git a/apps/web/pages/settings/my-account/profile.tsx b/apps/web/pages/settings/my-account/profile.tsx index d5d3d51ce6..b61ccf5360 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 = () => { />
- { - return ( - - ); - }} +