2022-12-07 20:15:15 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
|
2022-07-06 19:31:07 +00:00
|
|
|
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
|
2022-12-07 20:15:15 +00:00
|
|
|
import { User } from "@calcom/prisma/client";
|
|
|
|
import { TRPCClientErrorLike } from "@calcom/trpc/client";
|
|
|
|
import { AppRouter } from "@calcom/trpc/server/routers/_app";
|
2022-07-06 19:31:07 +00:00
|
|
|
|
|
|
|
import { PremiumTextfield } from "./PremiumTextfield";
|
|
|
|
import { UsernameTextfield } from "./UsernameTextfield";
|
|
|
|
|
|
|
|
export const UsernameAvailability = IS_SELF_HOSTED ? UsernameTextfield : PremiumTextfield;
|
2022-12-07 20:15:15 +00:00
|
|
|
|
|
|
|
interface UsernameAvailabilityFieldProps {
|
|
|
|
onSuccessMutation?: () => void;
|
|
|
|
onErrorMutation?: (error: TRPCClientErrorLike<AppRouter>) => void;
|
2022-12-08 09:50:03 +00:00
|
|
|
user: Pick<User, "username" | "metadata">;
|
2022-12-07 20:15:15 +00:00
|
|
|
}
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|