2022-09-06 22:58:16 +00:00
|
|
|
import { ArrowRightIcon } from "@heroicons/react/solid";
|
|
|
|
import { useRouter } from "next/router";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { FormEvent } from "react";
|
|
|
|
import { useRef, useState } from "react";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-03-03 23:20:13 +00:00
|
|
|
import { md } from "@calcom/lib/markdownIt";
|
2023-02-10 09:53:03 +00:00
|
|
|
import { telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
|
2023-01-27 23:14:58 +00:00
|
|
|
import turndown from "@calcom/lib/turndownService";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-25 01:08:10 +00:00
|
|
|
import { Button, Editor, ImageUploader, Label, showToast } from "@calcom/ui";
|
2022-12-03 19:20:18 +00:00
|
|
|
import { Avatar } from "@calcom/ui";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-12-03 19:20:18 +00:00
|
|
|
import type { IOnboardingPageProps } from "../../../pages/getting-started/[[...step]]";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
type FormData = {
|
|
|
|
bio: string;
|
|
|
|
};
|
2022-12-03 19:20:18 +00:00
|
|
|
interface IUserProfileProps {
|
|
|
|
user: IOnboardingPageProps["user"];
|
|
|
|
}
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-12-03 19:20:18 +00:00
|
|
|
const UserProfile = (props: IUserProfileProps) => {
|
2022-09-06 22:58:16 +00:00
|
|
|
const { user } = props;
|
|
|
|
const { t } = useLocale();
|
|
|
|
const avatarRef = useRef<HTMLInputElement>(null!);
|
2023-01-25 01:08:10 +00:00
|
|
|
const { setValue, handleSubmit, getValues } = useForm<FormData>({
|
|
|
|
defaultValues: { bio: user?.bio || "" },
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: eventTypes } = trpc.viewer.eventTypes.list.useQuery();
|
2022-09-06 22:58:16 +00:00
|
|
|
const [imageSrc, setImageSrc] = useState<string>(user?.avatar || "");
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const router = useRouter();
|
2022-11-10 23:40:01 +00:00
|
|
|
const createEventType = trpc.viewer.eventTypes.create.useMutation();
|
2023-02-10 09:53:03 +00:00
|
|
|
const telemetry = useTelemetry();
|
2022-09-07 01:34:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const mutation = trpc.viewer.updateProfile.useMutation({
|
2022-09-07 03:24:12 +00:00
|
|
|
onSuccess: async (_data, context) => {
|
|
|
|
if (context.avatar) {
|
|
|
|
showToast(t("your_user_profile_updated_successfully"), "success");
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.me.refetch();
|
2022-09-07 03:24:12 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
if (eventTypes?.length === 0) {
|
|
|
|
await Promise.all(
|
|
|
|
DEFAULT_EVENT_TYPES.map(async (event) => {
|
|
|
|
return createEventType.mutate(event);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2022-09-07 01:34:33 +00:00
|
|
|
}
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.me.refetch();
|
2022-09-07 03:24:12 +00:00
|
|
|
router.push("/");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: () => {
|
|
|
|
showToast(t("problem_saving_user_profile"), "error");
|
|
|
|
},
|
2022-09-06 22:58:16 +00:00
|
|
|
});
|
2022-09-07 01:34:33 +00:00
|
|
|
const onSubmit = handleSubmit((data: { bio: string }) => {
|
2022-09-06 22:58:16 +00:00
|
|
|
const { bio } = data;
|
|
|
|
|
2023-02-10 09:53:03 +00:00
|
|
|
telemetry.event(telemetryEventTypes.onboardingFinished);
|
|
|
|
|
2022-09-06 22:58:16 +00:00
|
|
|
mutation.mutate({
|
|
|
|
bio,
|
|
|
|
completedOnboarding: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
async function updateProfileHandler(event: FormEvent<HTMLFormElement>) {
|
|
|
|
event.preventDefault();
|
|
|
|
const enteredAvatar = avatarRef.current.value;
|
|
|
|
mutation.mutate({
|
|
|
|
avatar: enteredAvatar,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_EVENT_TYPES = [
|
|
|
|
{
|
|
|
|
title: t("15min_meeting"),
|
|
|
|
slug: "15min",
|
|
|
|
length: 15,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("30min_meeting"),
|
|
|
|
slug: "30min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("secret_meeting"),
|
|
|
|
slug: "secret",
|
|
|
|
length: 15,
|
|
|
|
hidden: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
2022-09-07 01:34:33 +00:00
|
|
|
<form onSubmit={onSubmit}>
|
2022-11-08 07:49:44 +00:00
|
|
|
<div className="flex flex-row items-center justify-start rtl:justify-end">
|
2022-12-03 19:20:18 +00:00
|
|
|
{user && (
|
|
|
|
<Avatar
|
|
|
|
alt={user.username || "user avatar"}
|
|
|
|
gravatarFallbackMd5={user.emailMd5}
|
|
|
|
size="lg"
|
|
|
|
imageSrc={imageSrc}
|
|
|
|
/>
|
|
|
|
)}
|
2022-09-06 22:58:16 +00:00
|
|
|
<input
|
|
|
|
ref={avatarRef}
|
|
|
|
type="hidden"
|
|
|
|
name="avatar"
|
|
|
|
id="avatar"
|
|
|
|
placeholder="URL"
|
2023-01-20 22:04:58 +00:00
|
|
|
className="mt-1 block w-full rounded-sm border border-gray-300 px-3 py-2 text-sm focus:border-gray-800 focus:outline-none focus:ring-gray-800"
|
2022-09-06 22:58:16 +00:00
|
|
|
defaultValue={imageSrc}
|
|
|
|
/>
|
|
|
|
<div className="flex items-center px-4">
|
|
|
|
<ImageUploader
|
|
|
|
target="avatar"
|
|
|
|
id="avatar-upload"
|
2022-11-08 07:49:44 +00:00
|
|
|
buttonMsg={t("add_profile_photo")}
|
2022-09-06 22:58:16 +00:00
|
|
|
handleAvatarChange={(newAvatar) => {
|
|
|
|
avatarRef.current.value = newAvatar;
|
|
|
|
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
|
|
|
|
window.HTMLInputElement.prototype,
|
|
|
|
"value"
|
|
|
|
)?.set;
|
|
|
|
nativeInputValueSetter?.call(avatarRef.current, newAvatar);
|
|
|
|
const ev2 = new Event("input", { bubbles: true });
|
|
|
|
avatarRef.current.dispatchEvent(ev2);
|
|
|
|
updateProfileHandler(ev2 as unknown as FormEvent<HTMLFormElement>);
|
|
|
|
setImageSrc(newAvatar);
|
|
|
|
}}
|
|
|
|
imageSrc={imageSrc}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<fieldset className="mt-8">
|
2023-01-25 01:08:10 +00:00
|
|
|
<Label className="mb-2 block text-sm font-medium text-gray-700">{t("about")}</Label>
|
|
|
|
<Editor
|
|
|
|
getText={() => md.render(getValues("bio") || user?.bio || "")}
|
2023-01-27 23:14:58 +00:00
|
|
|
setText={(value: string) => setValue("bio", turndown(value))}
|
2023-01-25 01:08:10 +00:00
|
|
|
excludedToolbarItems={["blockType"]}
|
2022-09-06 22:58:16 +00:00
|
|
|
/>
|
2022-09-07 01:34:33 +00:00
|
|
|
<p className="mt-2 font-sans text-sm font-normal text-gray-600 dark:text-white">
|
2022-09-06 22:58:16 +00:00
|
|
|
{t("few_sentences_about_yourself")}
|
|
|
|
</p>
|
|
|
|
</fieldset>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
className="mt-8 flex w-full flex-row justify-center rounded-md border border-black bg-black p-2 text-center text-sm text-white">
|
|
|
|
{t("finish")}
|
|
|
|
<ArrowRightIcon className="ml-2 h-4 w-4 self-center" aria-hidden="true" />
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UserProfile;
|