import { CheckIcon, PencilAltIcon, XIcon } from "@heroicons/react/solid"; import classNames from "classnames"; import { debounce } from "lodash"; import { MutableRefObject, useCallback, useEffect, useState } from "react"; import { fetchUsername } from "@calcom/lib/fetchUsername"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import Button from "@calcom/ui/Button"; import { Dialog, DialogClose, DialogContent, DialogHeader } from "@calcom/ui/Dialog"; import { Input, Label } from "@calcom/ui/form/fields"; import { trpc } from "@lib/trpc"; import { AppRouter } from "@server/routers/_app"; import { TRPCClientErrorLike } from "@trpc/client"; interface ICustomUsernameProps { currentUsername: string | undefined; setCurrentUsername: (value: string | undefined) => void; inputUsernameValue: string | undefined; usernameRef: MutableRefObject; setInputUsernameValue: (value: string) => void; onSuccessMutation?: () => void; onErrorMutation?: (error: TRPCClientErrorLike) => void; } const UsernameTextfield = (props: ICustomUsernameProps) => { const { t } = useLocale(); const { currentUsername, setCurrentUsername, inputUsernameValue, setInputUsernameValue, usernameRef, onSuccessMutation, onErrorMutation, } = props; const [usernameIsAvailable, setUsernameIsAvailable] = useState(false); 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), [] ); useEffect(() => { if (currentUsername !== inputUsernameValue) { debouncedApiCall(inputUsernameValue); } else if (inputUsernameValue === "") { setMarkAsError(false); setUsernameIsAvailable(false); } else { setUsernameIsAvailable(false); } }, [inputUsernameValue]); const utils = trpc.useContext(); const updateUsername = trpc.useMutation("viewer.updateProfile", { onSuccess: async () => { onSuccessMutation && (await onSuccessMutation()); setCurrentUsername(inputUsernameValue); setOpenDialogSaveUsername(false); }, onError: (error) => { onErrorMutation && onErrorMutation(error); }, async onSettled() { await utils.invalidateQueries(["viewer.public.i18n"]); }, }); const ActionButtons = (props: { index: string }) => { const { index } = props; return usernameIsAvailable && currentUsername !== inputUsernameValue ? (
) : ( <> ); }; return ( <>
{process.env.NEXT_PUBLIC_WEBSITE_URL}/
{ event.preventDefault(); setInputUsernameValue(event.target.value); }} data-testid="username-input" /> {currentUsername !== inputUsernameValue && (
{usernameIsAvailable ? : <>}
)}
{markAsError &&

Username is already taken

} {usernameIsAvailable && currentUsername !== inputUsernameValue && (
)}

{t("current")} {t("username").toLocaleLowerCase()}

{currentUsername}

{t("new")} {t("username").toLocaleLowerCase()}

{inputUsernameValue}

); }; export { UsernameTextfield };