2022-07-06 19:31:07 +00:00
|
|
|
import classNames from "classnames";
|
2022-11-08 16:36:14 +00:00
|
|
|
import { debounce, noop } from "lodash";
|
2022-09-08 00:38:37 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-11-08 16:36:14 +00:00
|
|
|
import { RefCallback, useMemo, useEffect, useState } from "react";
|
2022-07-06 19:31:07 +00:00
|
|
|
|
2022-09-08 00:38:37 +00:00
|
|
|
import { getPremiumPlanMode, getPremiumPlanPriceValue } from "@calcom/app-store/stripepayment/lib/utils";
|
2022-07-06 19:31:07 +00:00
|
|
|
import { fetchUsername } from "@calcom/lib/fetchUsername";
|
2022-09-22 17:34:17 +00:00
|
|
|
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
2022-07-06 19:31:07 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { User } from "@calcom/prisma/client";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { TRPCClientErrorLike } from "@calcom/trpc/client";
|
2022-11-10 23:40:01 +00:00
|
|
|
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
2022-07-22 17:27:06 +00:00
|
|
|
import type { AppRouter } from "@calcom/trpc/server/routers/_app";
|
2022-07-06 19:31:07 +00:00
|
|
|
import { Dialog, DialogClose, DialogContent, DialogHeader } from "@calcom/ui/Dialog";
|
2022-09-08 00:38:37 +00:00
|
|
|
import { Icon, StarIconSolid } from "@calcom/ui/Icon";
|
2022-11-04 15:40:46 +00:00
|
|
|
import { Button } from "@calcom/ui/components/button";
|
|
|
|
import { Input, Label } from "@calcom/ui/components/form";
|
2022-07-06 19:31:07 +00:00
|
|
|
|
|
|
|
export enum UsernameChangeStatusEnum {
|
|
|
|
NORMAL = "NORMAL",
|
|
|
|
UPGRADE = "UPGRADE",
|
|
|
|
DOWNGRADE = "DOWNGRADE",
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ICustomUsernameProps {
|
|
|
|
currentUsername: string | undefined;
|
2022-11-08 16:36:14 +00:00
|
|
|
setCurrentUsername?: (newUsername: string) => void;
|
2022-07-06 19:31:07 +00:00
|
|
|
inputUsernameValue: string | undefined;
|
2022-11-08 16:36:14 +00:00
|
|
|
usernameRef: RefCallback<HTMLInputElement>;
|
2022-07-06 19:31:07 +00:00
|
|
|
setInputUsernameValue: (value: string) => void;
|
|
|
|
onSuccessMutation?: () => void;
|
|
|
|
onErrorMutation?: (error: TRPCClientErrorLike<AppRouter>) => void;
|
|
|
|
user: Pick<
|
|
|
|
User,
|
|
|
|
| "username"
|
|
|
|
| "name"
|
|
|
|
| "email"
|
|
|
|
| "bio"
|
|
|
|
| "avatar"
|
|
|
|
| "timeZone"
|
|
|
|
| "weekStart"
|
|
|
|
| "hideBranding"
|
|
|
|
| "theme"
|
|
|
|
| "plan"
|
|
|
|
| "brandColor"
|
|
|
|
| "darkBrandColor"
|
|
|
|
| "timeFormat"
|
2022-09-22 17:34:17 +00:00
|
|
|
| "metadata"
|
2022-07-06 19:31:07 +00:00
|
|
|
>;
|
2022-09-08 00:38:37 +00:00
|
|
|
readonly?: boolean;
|
2022-07-06 19:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 00:38:37 +00:00
|
|
|
const obtainNewUsernameChangeCondition = ({
|
|
|
|
userIsPremium,
|
|
|
|
isNewUsernamePremium,
|
|
|
|
stripeCustomer,
|
|
|
|
}: {
|
|
|
|
userIsPremium: boolean;
|
|
|
|
isNewUsernamePremium: boolean;
|
2022-11-10 23:40:01 +00:00
|
|
|
stripeCustomer: RouterOutputs["viewer"]["stripeCustomer"] | undefined;
|
2022-09-08 00:38:37 +00:00
|
|
|
}) => {
|
|
|
|
if (!userIsPremium && isNewUsernamePremium && !stripeCustomer?.paidForPremium) {
|
|
|
|
return UsernameChangeStatusEnum.UPGRADE;
|
|
|
|
}
|
2022-09-22 17:34:17 +00:00
|
|
|
|
2022-09-08 00:38:37 +00:00
|
|
|
if (userIsPremium && !isNewUsernamePremium && getPremiumPlanMode() === "subscription") {
|
|
|
|
return UsernameChangeStatusEnum.DOWNGRADE;
|
|
|
|
}
|
|
|
|
return UsernameChangeStatusEnum.NORMAL;
|
|
|
|
};
|
|
|
|
|
2022-07-06 19:31:07 +00:00
|
|
|
const PremiumTextfield = (props: ICustomUsernameProps) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const {
|
|
|
|
currentUsername,
|
2022-11-08 16:36:14 +00:00
|
|
|
setCurrentUsername = noop,
|
2022-07-06 19:31:07 +00:00
|
|
|
inputUsernameValue,
|
|
|
|
setInputUsernameValue,
|
|
|
|
usernameRef,
|
|
|
|
onSuccessMutation,
|
|
|
|
onErrorMutation,
|
2022-09-08 00:38:37 +00:00
|
|
|
readonly: disabled,
|
2022-09-22 17:34:17 +00:00
|
|
|
user,
|
2022-07-06 19:31:07 +00:00
|
|
|
} = props;
|
|
|
|
const [usernameIsAvailable, setUsernameIsAvailable] = useState(false);
|
|
|
|
const [markAsError, setMarkAsError] = useState(false);
|
2022-09-08 00:38:37 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { paymentStatus: recentAttemptPaymentStatus } = router.query;
|
2022-07-06 19:31:07 +00:00
|
|
|
const [openDialogSaveUsername, setOpenDialogSaveUsername] = useState(false);
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: stripeCustomer } = trpc.viewer.stripeCustomer.useQuery();
|
2022-09-22 17:34:17 +00:00
|
|
|
const isCurrentUsernamePremium =
|
|
|
|
user && user.metadata && hasKeyInMetadata(user, "isPremium") ? !!user.metadata.isPremium : false;
|
2022-09-08 00:38:37 +00:00
|
|
|
const [isInputUsernamePremium, setIsInputUsernamePremium] = useState(false);
|
2022-11-08 16:36:14 +00:00
|
|
|
const debouncedApiCall = useMemo(
|
|
|
|
() =>
|
|
|
|
debounce(async (username: string) => {
|
|
|
|
const { data } = await fetchUsername(username);
|
|
|
|
setMarkAsError(!data.available && !!currentUsername && username !== currentUsername);
|
|
|
|
setIsInputUsernamePremium(data.premium);
|
|
|
|
setUsernameIsAvailable(data.available);
|
|
|
|
}, 150),
|
2022-07-06 19:31:07 +00:00
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-09-08 00:38:37 +00:00
|
|
|
// Use the current username or if it's not set, use the one available from stripe
|
|
|
|
setInputUsernameValue(currentUsername || stripeCustomer?.username || "");
|
|
|
|
}, [setInputUsernameValue, currentUsername, stripeCustomer?.username]);
|
2022-07-06 19:31:07 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-08 16:36:14 +00:00
|
|
|
if (!inputUsernameValue) {
|
|
|
|
debouncedApiCall.cancel();
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 00:38:37 +00:00
|
|
|
debouncedApiCall(inputUsernameValue);
|
|
|
|
}, [debouncedApiCall, inputUsernameValue]);
|
2022-07-06 19:31:07 +00:00
|
|
|
|
|
|
|
const utils = trpc.useContext();
|
2022-11-10 23:40:01 +00:00
|
|
|
const updateUsername = trpc.viewer.updateProfile.useMutation({
|
2022-07-06 19:31:07 +00:00
|
|
|
onSuccess: async () => {
|
|
|
|
onSuccessMutation && (await onSuccessMutation());
|
|
|
|
setOpenDialogSaveUsername(false);
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
onErrorMutation && onErrorMutation(error);
|
|
|
|
},
|
|
|
|
async onSettled() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.public.i18n.invalidate();
|
2022-07-06 19:31:07 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-08 00:38:37 +00:00
|
|
|
// when current username isn't set - Go to stripe to check what username he wanted to buy and was it a premium and was it paid for
|
|
|
|
const paymentRequired = !currentUsername && stripeCustomer?.isPremium && !stripeCustomer?.paidForPremium;
|
|
|
|
|
|
|
|
const usernameChangeCondition = obtainNewUsernameChangeCondition({
|
|
|
|
userIsPremium: isCurrentUsernamePremium,
|
|
|
|
isNewUsernamePremium: isInputUsernamePremium,
|
|
|
|
stripeCustomer,
|
|
|
|
});
|
|
|
|
|
|
|
|
const usernameFromStripe = stripeCustomer?.username;
|
|
|
|
|
|
|
|
const paymentLink = `/api/integrations/stripepayment/subscription?intentUsername=${
|
|
|
|
inputUsernameValue || usernameFromStripe
|
|
|
|
}&action=${usernameChangeCondition}&callbackUrl=${router.asPath}`;
|
|
|
|
|
|
|
|
const ActionButtons = () => {
|
|
|
|
if (paymentRequired) {
|
|
|
|
return (
|
|
|
|
<div className="flex flex-row">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="primary"
|
|
|
|
className="mx-2"
|
|
|
|
href={paymentLink}
|
|
|
|
data-testid="reserve-username-btn">
|
|
|
|
{t("Reserve")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ((usernameIsAvailable || isInputUsernamePremium) && currentUsername !== inputUsernameValue) {
|
|
|
|
return (
|
|
|
|
<div className="flex flex-row">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="primary"
|
|
|
|
className="mx-2"
|
|
|
|
onClick={() => setOpenDialogSaveUsername(true)}
|
|
|
|
data-testid="update-username-btn">
|
|
|
|
{t("update")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
if (currentUsername) {
|
|
|
|
setInputUsernameValue(currentUsername);
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <></>;
|
2022-07-06 19:31:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const saveUsername = () => {
|
|
|
|
if (usernameChangeCondition === UsernameChangeStatusEnum.NORMAL) {
|
|
|
|
updateUsername.mutate({
|
|
|
|
username: inputUsernameValue,
|
|
|
|
});
|
2022-11-08 16:36:14 +00:00
|
|
|
setCurrentUsername(inputUsernameValue);
|
2022-07-06 19:31:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-22 17:34:17 +00:00
|
|
|
let paymentMsg = !currentUsername ? (
|
|
|
|
<span className="text-xs text-orange-400">
|
|
|
|
You need to reserve your premium username for {getPremiumPlanPriceValue()}
|
|
|
|
</span>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
if (recentAttemptPaymentStatus && recentAttemptPaymentStatus !== "paid") {
|
|
|
|
paymentMsg = (
|
|
|
|
<span className="text-sm text-red-500">
|
|
|
|
Your payment could not be completed. Your username is still not reserved
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:31:07 +00:00
|
|
|
return (
|
2022-09-06 22:58:16 +00:00
|
|
|
<div>
|
2022-09-08 00:38:37 +00:00
|
|
|
<div className="flex justify-items-center">
|
2022-07-12 17:50:04 +00:00
|
|
|
<Label htmlFor="username">{t("username")}</Label>
|
2022-07-06 19:31:07 +00:00
|
|
|
</div>
|
2022-09-06 22:58:16 +00:00
|
|
|
<div className="mt-2 flex rounded-md">
|
2022-07-06 19:31:07 +00:00
|
|
|
<span
|
|
|
|
className={classNames(
|
2022-09-14 15:57:20 +00:00
|
|
|
isInputUsernamePremium ? "border-1 border-orange-400 " : "",
|
2022-09-22 17:34:17 +00:00
|
|
|
"hidden h-9 items-center rounded-l-md border border-r-0 border-gray-300 border-r-gray-300 bg-gray-50 px-3 text-sm text-gray-500 md:inline-flex"
|
2022-07-06 19:31:07 +00:00
|
|
|
)}>
|
2022-09-06 22:58:16 +00:00
|
|
|
{process.env.NEXT_PUBLIC_WEBSITE_URL.replace("https://", "").replace("http://", "")}/
|
2022-07-06 19:31:07 +00:00
|
|
|
</span>
|
2022-09-08 00:38:37 +00:00
|
|
|
|
|
|
|
<div className="relative w-full">
|
2022-07-06 19:31:07 +00:00
|
|
|
<Input
|
|
|
|
ref={usernameRef}
|
2022-07-12 17:50:04 +00:00
|
|
|
name="username"
|
|
|
|
autoComplete="none"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoCorrect="none"
|
2022-09-08 00:38:37 +00:00
|
|
|
disabled={disabled}
|
2022-07-06 19:31:07 +00:00
|
|
|
className={classNames(
|
2022-09-14 15:57:20 +00:00
|
|
|
"border-l-1 mb-0 mt-0 rounded-md rounded-l-none font-sans text-sm leading-4 focus:!ring-0",
|
2022-09-08 00:38:37 +00:00
|
|
|
isInputUsernamePremium
|
2022-09-14 15:57:20 +00:00
|
|
|
? "border-1 focus:border-1 border-orange-400 focus:border-orange-400"
|
|
|
|
: "border-1 focus:border-2",
|
2022-07-06 19:31:07 +00:00
|
|
|
markAsError
|
2022-09-08 00:38:37 +00:00
|
|
|
? "focus:shadow-0 focus:ring-shadow-0 border-red-500 focus:border-red-500 focus:outline-none"
|
2022-09-14 15:57:20 +00:00
|
|
|
: "border-l-gray-300",
|
|
|
|
disabled ? "bg-gray-100 text-gray-400 focus:border-0" : ""
|
2022-07-06 19:31:07 +00:00
|
|
|
)}
|
2022-09-08 00:38:37 +00:00
|
|
|
value={inputUsernameValue}
|
2022-07-06 19:31:07 +00:00
|
|
|
onChange={(event) => {
|
|
|
|
event.preventDefault();
|
2022-09-22 17:34:17 +00:00
|
|
|
// Reset payment status
|
|
|
|
delete router.query.paymentStatus;
|
2022-07-06 19:31:07 +00:00
|
|
|
setInputUsernameValue(event.target.value);
|
|
|
|
}}
|
|
|
|
data-testid="username-input"
|
|
|
|
/>
|
2022-09-08 00:38:37 +00:00
|
|
|
<div className="absolute top-0 right-2 flex flex-row">
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
"mx-2 py-1",
|
|
|
|
isInputUsernamePremium ? "text-orange-400" : "",
|
|
|
|
usernameIsAvailable ? "" : ""
|
|
|
|
)}>
|
2022-09-22 17:34:17 +00:00
|
|
|
{isInputUsernamePremium ? <StarIconSolid className="mt-[2px] w-6" /> : <></>}
|
2022-09-14 15:57:20 +00:00
|
|
|
{!isInputUsernamePremium && usernameIsAvailable ? <Icon.FiCheck className="mt-2 w-6" /> : <></>}
|
2022-09-08 00:38:37 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
2022-07-06 19:31:07 +00:00
|
|
|
</div>
|
2022-09-08 00:38:37 +00:00
|
|
|
|
|
|
|
{(usernameIsAvailable || isInputUsernamePremium) && currentUsername !== inputUsernameValue && (
|
|
|
|
<div className="flex justify-end">
|
|
|
|
<ActionButtons />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-07-06 19:31:07 +00:00
|
|
|
</div>
|
2022-09-22 17:34:17 +00:00
|
|
|
{paymentMsg}
|
2022-07-06 19:31:07 +00:00
|
|
|
{markAsError && <p className="mt-1 text-xs text-red-500">Username is already taken</p>}
|
|
|
|
|
|
|
|
{usernameIsAvailable && (
|
|
|
|
<p className={classNames("mt-1 text-xs text-gray-900")}>
|
|
|
|
{usernameChangeCondition === UsernameChangeStatusEnum.DOWNGRADE && (
|
2022-09-08 00:38:37 +00:00
|
|
|
<>{t("premium_to_standard_username_description")}</>
|
2022-07-06 19:31:07 +00:00
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Dialog open={openDialogSaveUsername}>
|
|
|
|
<DialogContent>
|
2022-09-08 00:38:37 +00:00
|
|
|
<div className="flex flex-row">
|
2022-07-06 19:31:07 +00:00
|
|
|
<div className="xs:hidden flex h-10 w-10 flex-shrink-0 justify-center rounded-full bg-[#FAFAFA]">
|
2022-08-03 16:01:29 +00:00
|
|
|
<Icon.FiEdit2 className="m-auto h-6 w-6" />
|
2022-07-06 19:31:07 +00:00
|
|
|
</div>
|
|
|
|
<div className="mb-4 w-full px-4 pt-1">
|
2022-07-27 02:24:00 +00:00
|
|
|
<DialogHeader title={t("confirm_username_change_dialog_title")} />
|
2022-07-06 19:31:07 +00:00
|
|
|
{usernameChangeCondition && usernameChangeCondition !== UsernameChangeStatusEnum.NORMAL && (
|
|
|
|
<p className="-mt-4 mb-4 text-sm text-gray-800">
|
|
|
|
{usernameChangeCondition === UsernameChangeStatusEnum.UPGRADE &&
|
|
|
|
t("change_username_standard_to_premium")}
|
|
|
|
{usernameChangeCondition === UsernameChangeStatusEnum.DOWNGRADE &&
|
|
|
|
t("change_username_premium_to_standard")}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="flex w-full flex-wrap rounded-sm bg-gray-100 py-3 text-sm">
|
|
|
|
<div className="flex-1 px-2">
|
2022-07-20 18:12:18 +00:00
|
|
|
<p className="text-gray-500">{t("current_username")}</p>
|
2022-07-06 19:31:07 +00:00
|
|
|
<p className="mt-1" data-testid="current-username">
|
|
|
|
{currentUsername}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="ml-6 flex-1">
|
|
|
|
<p className="text-gray-500" data-testid="new-username">
|
2022-07-20 18:12:18 +00:00
|
|
|
{t("new_username")}
|
2022-07-06 19:31:07 +00:00
|
|
|
</p>
|
|
|
|
<p>{inputUsernameValue}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="mt-4 flex flex-row-reverse gap-x-2">
|
|
|
|
{/* redirect to checkout */}
|
|
|
|
{(usernameChangeCondition === UsernameChangeStatusEnum.UPGRADE ||
|
|
|
|
usernameChangeCondition === UsernameChangeStatusEnum.DOWNGRADE) && (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
loading={updateUsername.isLoading}
|
|
|
|
data-testid="go-to-billing"
|
2022-09-08 00:38:37 +00:00
|
|
|
href={paymentLink}>
|
2022-07-06 19:31:07 +00:00
|
|
|
<>
|
2022-08-03 16:01:29 +00:00
|
|
|
{t("go_to_stripe_billing")} <Icon.FiExternalLink className="ml-1 h-4 w-4" />
|
2022-07-06 19:31:07 +00:00
|
|
|
</>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
{/* Normal save */}
|
|
|
|
{usernameChangeCondition === UsernameChangeStatusEnum.NORMAL && (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
loading={updateUsername.isLoading}
|
|
|
|
data-testid="save-username"
|
|
|
|
onClick={() => {
|
|
|
|
saveUsername();
|
|
|
|
}}>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
<DialogClose asChild>
|
|
|
|
<Button color="secondary" onClick={() => setOpenDialogSaveUsername(false)}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</DialogClose>
|
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
2022-09-06 22:58:16 +00:00
|
|
|
</div>
|
2022-07-06 19:31:07 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { PremiumTextfield };
|