2022-08-26 00:11:41 +00:00
|
|
|
import crypto from "crypto";
|
|
|
|
import { signOut } from "next-auth/react";
|
2022-09-06 18:23:17 +00:00
|
|
|
import { useRef, useState, BaseSyntheticEvent } from "react";
|
2022-08-30 19:46:52 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
import { ErrorCode } from "@calcom/lib/auth";
|
2022-09-17 02:40:27 +00:00
|
|
|
import { WEBSITE_URL } from "@calcom/lib/constants";
|
2022-08-26 00:11:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-09-06 18:23:17 +00:00
|
|
|
import { TRPCClientErrorLike } from "@calcom/trpc/client";
|
2022-08-26 00:11:41 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-09-06 18:23:17 +00:00
|
|
|
import { AppRouter } from "@calcom/trpc/server/routers/_app";
|
2022-08-26 00:11:41 +00:00
|
|
|
import { Icon } from "@calcom/ui";
|
2022-09-06 18:23:17 +00:00
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
2022-08-26 00:11:41 +00:00
|
|
|
import Avatar from "@calcom/ui/v2/core/Avatar";
|
|
|
|
import { Button } from "@calcom/ui/v2/core/Button";
|
2022-08-30 19:46:52 +00:00
|
|
|
import { Dialog, DialogContent, DialogTrigger } from "@calcom/ui/v2/core/Dialog";
|
2022-09-12 22:04:33 +00:00
|
|
|
import ImageUploader from "@calcom/ui/v2/core/ImageUploader";
|
2022-08-30 19:46:52 +00:00
|
|
|
import Meta from "@calcom/ui/v2/core/Meta";
|
2022-09-06 18:23:17 +00:00
|
|
|
import { Form, Label, TextField, PasswordField } from "@calcom/ui/v2/core/form/fields";
|
2022-09-09 12:57:31 +00:00
|
|
|
import { getLayout } from "@calcom/ui/v2/core/layouts/SettingsLayout";
|
2022-08-26 18:13:49 +00:00
|
|
|
import showToast from "@calcom/ui/v2/core/notifications";
|
2022-09-15 09:05:26 +00:00
|
|
|
import { SkeletonContainer, SkeletonText, SkeletonButton, SkeletonAvatar } from "@calcom/ui/v2/core/skeleton";
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
import TwoFactor from "@components/auth/TwoFactor";
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
const SkeletonLoader = () => {
|
|
|
|
return (
|
|
|
|
<SkeletonContainer>
|
|
|
|
<div className="mt-6 mb-8 space-y-6 divide-y">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<SkeletonAvatar className=" h-12 w-12 px-4" />
|
|
|
|
<SkeletonButton className=" h-6 w-32 rounded-md p-5" />
|
|
|
|
</div>
|
|
|
|
<SkeletonText className="h-8 w-full" />
|
|
|
|
<SkeletonText className="h-8 w-full" />
|
|
|
|
<SkeletonText className="h-8 w-full" />
|
|
|
|
|
|
|
|
<SkeletonButton className="mr-6 h-8 w-20 rounded-md p-5" />
|
|
|
|
</div>
|
|
|
|
</SkeletonContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
interface DeleteAccountValues {
|
|
|
|
totpCode: string;
|
|
|
|
}
|
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
const ProfileView = () => {
|
2022-08-26 00:11:41 +00:00
|
|
|
const { t } = useLocale();
|
2022-09-06 18:23:17 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
const { data: user, isLoading } = trpc.useQuery(["viewer.me"]);
|
2022-08-26 00:11:41 +00:00
|
|
|
const mutation = trpc.useMutation("viewer.updateProfile", {
|
|
|
|
onSuccess: () => {
|
|
|
|
showToast(t("settings_updated_successfully"), "success");
|
|
|
|
},
|
|
|
|
onError: () => {
|
|
|
|
showToast(t("error_updating_settings"), "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [deleteAccountOpen, setDeleteAccountOpen] = useState(false);
|
2022-09-06 18:23:17 +00:00
|
|
|
const [hasDeleteErrors, setHasDeleteErrors] = useState(false);
|
|
|
|
const [deleteErrorMessage, setDeleteErrorMessage] = useState("");
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
const form = useForm<DeleteAccountValues>();
|
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
const emailMd5 = crypto
|
|
|
|
.createHash("md5")
|
|
|
|
.update(user?.email || "example@example.com")
|
|
|
|
.digest("hex");
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
const onDeleteMeSuccessMutation = async () => {
|
|
|
|
await utils.invalidateQueries(["viewer.me"]);
|
|
|
|
showToast(t("Your account was deleted"), "success");
|
|
|
|
|
|
|
|
setHasDeleteErrors(false); // dismiss any open errors
|
2022-08-26 00:11:41 +00:00
|
|
|
if (process.env.NEXT_PUBLIC_WEBAPP_URL === "https://app.cal.com") {
|
|
|
|
signOut({ callbackUrl: "/auth/logout?survey=true" });
|
|
|
|
} else {
|
|
|
|
signOut({ callbackUrl: "/auth/logout" });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
const onDeleteMeErrorMutation = (error: TRPCClientErrorLike<AppRouter>) => {
|
|
|
|
setHasDeleteErrors(true);
|
|
|
|
setDeleteErrorMessage(errorMessages[error.message]);
|
|
|
|
};
|
|
|
|
const deleteMeMutation = trpc.useMutation("viewer.deleteMe", {
|
|
|
|
onSuccess: onDeleteMeSuccessMutation,
|
|
|
|
onError: onDeleteMeErrorMutation,
|
|
|
|
async onSettled() {
|
|
|
|
await utils.invalidateQueries(["viewer.me"]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const onConfirmButton = (e: Event | React.MouseEvent<HTMLElement, MouseEvent>) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const totpCode = form.getValues("totpCode");
|
|
|
|
const password = passwordRef.current.value;
|
|
|
|
deleteMeMutation.mutate({ password, totpCode });
|
|
|
|
};
|
|
|
|
const onConfirm = ({ totpCode }: DeleteAccountValues, e: BaseSyntheticEvent | undefined) => {
|
|
|
|
e?.preventDefault();
|
|
|
|
const password = passwordRef.current.value;
|
|
|
|
deleteMeMutation.mutate({ password, totpCode });
|
|
|
|
};
|
|
|
|
|
2022-08-26 00:11:41 +00:00
|
|
|
const formMethods = useForm({
|
|
|
|
defaultValues: {
|
2022-09-15 09:05:26 +00:00
|
|
|
avatar: user?.avatar || "",
|
2022-08-26 00:11:41 +00:00
|
|
|
username: user?.username || "",
|
|
|
|
name: user?.name || "",
|
2022-09-15 17:45:03 +00:00
|
|
|
email: user?.email || "",
|
2022-08-26 00:11:41 +00:00
|
|
|
bio: user?.bio || "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-09 12:57:31 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2022-09-06 18:23:17 +00:00
|
|
|
const passwordRef = useRef<HTMLInputElement>(null!);
|
|
|
|
|
|
|
|
const errorMessages: { [key: string]: string } = {
|
|
|
|
[ErrorCode.SecondFactorRequired]: t("2fa_enabled_instructions"),
|
|
|
|
[ErrorCode.IncorrectPassword]: `${t("incorrect_password")} ${t("please_try_again")}`,
|
|
|
|
[ErrorCode.UserNotFound]: t("no_account_exists"),
|
|
|
|
[ErrorCode.IncorrectTwoFactorCode]: `${t("incorrect_2fa_code")} ${t("please_try_again")}`,
|
|
|
|
[ErrorCode.InternalServerError]: `${t("something_went_wrong")} ${t("please_try_again_and_contact_us")}`,
|
|
|
|
[ErrorCode.ThirdPartyIdentityProviderEnabled]: t("account_created_with_identity_provider"),
|
|
|
|
};
|
2022-08-26 00:11:41 +00:00
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
if (isLoading) return <SkeletonLoader />;
|
|
|
|
|
2022-08-26 00:11:41 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Form
|
|
|
|
form={formMethods}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
mutation.mutate(values);
|
|
|
|
}}>
|
2022-09-14 09:42:48 +00:00
|
|
|
<Meta title="Profile" description="Manage settings for your cal profile" />
|
2022-08-26 00:11:41 +00:00
|
|
|
<div className="flex items-center">
|
|
|
|
<Controller
|
|
|
|
control={formMethods.control}
|
|
|
|
name="avatar"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<>
|
2022-09-15 09:05:26 +00:00
|
|
|
<Avatar alt="" imageSrc={value} gravatarFallbackMd5={emailMd5} size="lg" />
|
2022-08-26 00:11:41 +00:00
|
|
|
<div className="ml-4">
|
|
|
|
<ImageUploader
|
|
|
|
target="avatar"
|
|
|
|
id="avatar-upload"
|
|
|
|
buttonMsg={t("change_avatar")}
|
|
|
|
handleAvatarChange={(newAvatar) => {
|
|
|
|
formMethods.setValue("avatar", newAvatar);
|
|
|
|
}}
|
|
|
|
imageSrc={value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-09-17 02:40:27 +00:00
|
|
|
<div className="mt-8">
|
|
|
|
<TextField
|
|
|
|
data-testid="username-input"
|
|
|
|
label={t("personal_cal_url")}
|
|
|
|
addOnLeading={WEBSITE_URL + "/"}
|
|
|
|
{...formMethods.register("username")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-8">
|
|
|
|
<TextField label={t("full_name")} {...formMethods.register("name")} />
|
|
|
|
</div>
|
|
|
|
<div className="mt-8">
|
|
|
|
<TextField label={t("email")} hint={t("change_email_hint")} {...formMethods.register("email")} />
|
|
|
|
</div>
|
|
|
|
<div className="mt-8">
|
|
|
|
<TextField label={t("about")} hint={t("bio_hint")} {...formMethods.register("bio")} />
|
|
|
|
</div>
|
|
|
|
|
2022-08-26 00:11:41 +00:00
|
|
|
<Button color="primary" className="mt-8" type="submit" loading={mutation.isLoading}>
|
|
|
|
{t("update")}
|
|
|
|
</Button>
|
|
|
|
|
2022-09-08 17:37:26 +00:00
|
|
|
<hr className="my-6 border-neutral-200" />
|
2022-08-26 00:11:41 +00:00
|
|
|
|
|
|
|
<Label>{t("danger_zone")}</Label>
|
|
|
|
{/* Delete account Dialog */}
|
|
|
|
<Dialog open={deleteAccountOpen} onOpenChange={setDeleteAccountOpen}>
|
|
|
|
<DialogTrigger asChild>
|
2022-09-15 12:10:41 +00:00
|
|
|
<Button
|
2022-09-15 16:59:48 +00:00
|
|
|
data-testid="delete-account"
|
2022-09-15 12:10:41 +00:00
|
|
|
color="destructive"
|
|
|
|
className="mt-1 border-2"
|
2022-09-15 16:59:48 +00:00
|
|
|
StartIcon={Icon.FiTrash2}>
|
2022-08-26 00:11:41 +00:00
|
|
|
{t("delete_account")}
|
|
|
|
</Button>
|
|
|
|
</DialogTrigger>
|
|
|
|
<DialogContent
|
|
|
|
title={t("delete_account_modal_title")}
|
|
|
|
description={t("confirm_delete_account_modal")}
|
2022-09-06 18:23:17 +00:00
|
|
|
type="creation"
|
2022-08-26 00:11:41 +00:00
|
|
|
actionText={t("delete_my_account")}
|
2022-09-15 12:10:41 +00:00
|
|
|
actionProps={{
|
|
|
|
// @ts-expect-error data attributes aren't typed
|
|
|
|
"data-testid": "delete-account-confirm",
|
|
|
|
}}
|
2022-08-26 00:11:41 +00:00
|
|
|
Icon={Icon.FiAlertTriangle}
|
2022-09-06 18:23:17 +00:00
|
|
|
actionOnClick={(e) => e && onConfirmButton(e)}>
|
|
|
|
<>
|
|
|
|
<p className="mb-7">{t("delete_account_confirmation_message")}</p>
|
|
|
|
<PasswordField
|
|
|
|
data-testid="password"
|
|
|
|
name="password"
|
|
|
|
id="password"
|
|
|
|
type="password"
|
|
|
|
autoComplete="current-password"
|
|
|
|
required
|
|
|
|
label="Password"
|
|
|
|
ref={passwordRef}
|
|
|
|
/>
|
|
|
|
|
2022-09-15 09:05:26 +00:00
|
|
|
{user?.twoFactorEnabled && (
|
2022-09-06 18:23:17 +00:00
|
|
|
<Form handleSubmit={onConfirm} className="pb-4" form={form}>
|
|
|
|
<TwoFactor center={false} />
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{hasDeleteErrors && <Alert severity="error" title={deleteErrorMessage} />}
|
|
|
|
</>
|
2022-08-26 00:11:41 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ProfileView.getLayout = getLayout;
|
|
|
|
|
|
|
|
export default ProfileView;
|