From 9d53103fe9068e428faa63d107d7cd58cd5f8e1d Mon Sep 17 00:00:00 2001 From: Sean Brydon Date: Wed, 20 Sep 2023 17:02:24 +0100 Subject: [PATCH] Premium username fixes --- apps/web/pages/signup.tsx | 59 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/web/pages/signup.tsx b/apps/web/pages/signup.tsx index f9324ad59c..4f20a459e8 100644 --- a/apps/web/pages/signup.tsx +++ b/apps/web/pages/signup.tsx @@ -5,7 +5,7 @@ import { signIn } from "next-auth/react"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import type { CSSProperties } from "react"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import type { SubmitHandler } from "react-hook-form"; import { useForm, useFormContext } from "react-hook-form"; import { Trans } from "react-i18next"; @@ -20,6 +20,7 @@ import { useFlagMap } from "@calcom/features/flags/context/provider"; import { getFeatureFlagMap } from "@calcom/features/flags/server/utils"; import { classNames } from "@calcom/lib"; import { IS_CALCOM, IS_SELF_HOSTED, WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants"; +import { fetchUsername } from "@calcom/lib/fetchUsername"; import { useDebounce } from "@calcom/lib/hooks/useDebounce"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import slugify from "@calcom/lib/slugify"; @@ -66,34 +67,43 @@ const FEATURES = [ ]; function UsernameField({ + username, setPremium, premium, ...props -}: React.ComponentProps & { setPremium: (value: boolean) => void; premium: boolean }) { +}: React.ComponentProps & { + username: string; + setPremium: (value: boolean) => void; + premium: boolean; +}) { const { t } = useLocale(); const { watch, register, formState } = useFormContext(); const [taken, setTaken] = useState(false); - const watchedUsername = watch("username"); - const debouncedUsername = useDebounce(watchedUsername, 500); - // const { isFetching } = useQuery({ - // queryKey: ["username"], - // queryFn: async () => await fetchUsername(debouncedUsername), - // refetchOnWindowFocus: false, - // onSuccess: ({ data }) => { - // setPremium(data.premium); - // if (!data.available) { - // setTaken(true); - // } - // }, - // enabled: debouncedUsername?.length > 0 || false, - // }); + const [loading, setLoading] = useState(false); + const debouncedUsername = useDebounce(username, 500); - // useEffect(() => { - // if (!debouncedUsername) { - // setTaken(false); - // setPremium(false); - // } - // }, [debouncedUsername, setPremium, setTaken]); + useEffect(() => { + async function checkUsername() { + if (!debouncedUsername) { + setLoading(false); + setPremium(false); + setTaken(false); + return; + } + setLoading(true); + fetchUsername(debouncedUsername) + .then(({ data }) => { + setPremium(data.premium); + if (!data.available) { + setTaken(true); + } + }) + .finally(() => { + setLoading(false); + }); + } + checkUsername(); + }, [debouncedUsername, setPremium, setTaken]); return (
@@ -101,7 +111,7 @@ function UsernameField({ {!formState.isSubmitting && (

- {false ? ( + {loading ? ( <> {t("loading")} @@ -140,7 +150,7 @@ export default function Signup({ prepopulateFormValues, token, orgSlug }: Signup }); const { register, - + watch, formState: { isSubmitting, errors }, } = formMethods; @@ -233,6 +243,7 @@ export default function Signup({ prepopulateFormValues, token, orgSlug }: Signup {/* Username */} setPremiumUsername(value)}