2023-03-30 11:34:41 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2023-09-15 09:33:32 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import Link from "next/link";
|
2023-03-30 11:34:41 +00:00
|
|
|
|
|
|
|
// react-hook-form
|
2023-03-30 12:17:49 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2023-03-30 11:34:41 +00:00
|
|
|
// services
|
|
|
|
import fileService from "services/file.service";
|
|
|
|
import userService from "services/user.service";
|
|
|
|
// hooks
|
2023-05-30 13:44:35 +00:00
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-03-30 11:34:41 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// layouts
|
2023-04-08 08:16:46 +00:00
|
|
|
import { WorkspaceAuthorizationLayout } from "layouts/auth-layout";
|
2023-03-30 11:34:41 +00:00
|
|
|
// components
|
2023-08-11 10:26:26 +00:00
|
|
|
import { ImagePickerPopover, ImageUploadModal } from "components/core";
|
2023-09-15 09:33:32 +00:00
|
|
|
import { SettingsSidebar } from "components/project";
|
2023-03-30 11:34:41 +00:00
|
|
|
// ui
|
2023-09-15 09:33:32 +00:00
|
|
|
import { CustomSearchSelect, CustomSelect, Input, PrimaryButton, Spinner } from "components/ui";
|
2023-03-30 11:34:41 +00:00
|
|
|
import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs";
|
|
|
|
// icons
|
2023-03-30 12:17:49 +00:00
|
|
|
import { UserIcon } from "@heroicons/react/24/outline";
|
2023-09-15 09:33:32 +00:00
|
|
|
import { UserCircle } from "lucide-react";
|
2023-03-30 11:34:41 +00:00
|
|
|
// types
|
2023-04-08 08:16:46 +00:00
|
|
|
import type { NextPage } from "next";
|
2023-03-30 11:34:41 +00:00
|
|
|
import type { IUser } from "types";
|
2023-03-30 12:17:49 +00:00
|
|
|
// constants
|
|
|
|
import { USER_ROLES } from "constants/workspace";
|
2023-08-29 14:41:06 +00:00
|
|
|
import { TIME_ZONES } from "constants/timezones";
|
2023-03-30 11:34:41 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<IUser> = {
|
|
|
|
avatar: "",
|
2023-08-11 10:26:26 +00:00
|
|
|
cover_image: "",
|
2023-03-30 11:34:41 +00:00
|
|
|
first_name: "",
|
|
|
|
last_name: "",
|
|
|
|
email: "",
|
2023-07-23 16:40:40 +00:00
|
|
|
role: "Product / Project Manager",
|
2023-08-29 14:41:06 +00:00
|
|
|
user_timezone: "Asia/Kolkata",
|
2023-03-30 11:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Profile: NextPage = () => {
|
|
|
|
const [isRemoving, setIsRemoving] = useState(false);
|
|
|
|
const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false);
|
|
|
|
|
2023-09-15 09:33:32 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
2023-03-30 11:34:41 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
|
|
|
watch,
|
|
|
|
setValue,
|
2023-03-30 12:17:49 +00:00
|
|
|
control,
|
2023-03-30 11:34:41 +00:00
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
} = useForm<IUser>({ defaultValues });
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
2023-05-30 13:44:35 +00:00
|
|
|
const { user: myProfile, mutateUser } = useUserAuth();
|
2023-03-30 11:34:41 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({ ...defaultValues, ...myProfile });
|
|
|
|
}, [myProfile, reset]);
|
|
|
|
|
|
|
|
const onSubmit = async (formData: IUser) => {
|
2023-07-23 16:40:40 +00:00
|
|
|
if (formData.first_name === "" || formData.last_name === "") {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "First and last names are required.",
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:34:41 +00:00
|
|
|
const payload: Partial<IUser> = {
|
|
|
|
first_name: formData.first_name,
|
|
|
|
last_name: formData.last_name,
|
|
|
|
avatar: formData.avatar,
|
2023-08-11 10:26:26 +00:00
|
|
|
cover_image: formData.cover_image,
|
2023-03-30 12:17:49 +00:00
|
|
|
role: formData.role,
|
2023-08-09 11:14:35 +00:00
|
|
|
display_name: formData.display_name,
|
2023-08-29 14:41:06 +00:00
|
|
|
user_timezone: formData.user_timezone,
|
2023-03-30 11:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await userService
|
|
|
|
.updateUser(payload)
|
|
|
|
.then((res) => {
|
2023-08-15 09:34:46 +00:00
|
|
|
mutateUser((prevData: any) => {
|
2023-03-30 11:34:41 +00:00
|
|
|
if (!prevData) return prevData;
|
2023-07-23 16:40:40 +00:00
|
|
|
|
2023-06-07 13:33:49 +00:00
|
|
|
return { ...prevData, ...res };
|
2023-03-30 11:34:41 +00:00
|
|
|
}, false);
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Profile updated successfully.",
|
|
|
|
});
|
|
|
|
})
|
2023-03-30 22:54:57 +00:00
|
|
|
.catch(() =>
|
2023-03-30 11:34:41 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "There was some error in updating your profile. Please try again.",
|
2023-03-30 22:54:57 +00:00
|
|
|
})
|
|
|
|
);
|
2023-03-30 11:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleDelete = (url: string | null | undefined, updateUser: boolean = false) => {
|
|
|
|
if (!url) return;
|
|
|
|
|
|
|
|
setIsRemoving(true);
|
|
|
|
|
2023-06-09 04:54:57 +00:00
|
|
|
fileService.deleteUserFile(url).then(() => {
|
2023-03-30 11:34:41 +00:00
|
|
|
if (updateUser)
|
|
|
|
userService
|
|
|
|
.updateUser({ avatar: "" })
|
2023-06-07 13:33:49 +00:00
|
|
|
.then(() => {
|
2023-03-30 11:34:41 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Profile picture removed successfully.",
|
|
|
|
});
|
2023-08-15 09:34:46 +00:00
|
|
|
mutateUser((prevData: any) => {
|
2023-03-30 11:34:41 +00:00
|
|
|
if (!prevData) return prevData;
|
2023-06-07 13:33:49 +00:00
|
|
|
return { ...prevData, avatar: "" };
|
2023-03-30 11:34:41 +00:00
|
|
|
}, false);
|
2023-09-15 09:33:32 +00:00
|
|
|
setIsRemoving(false);
|
2023-03-30 11:34:41 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "There was some error in deleting your profile picture. Please try again.",
|
|
|
|
});
|
2023-03-30 22:54:57 +00:00
|
|
|
})
|
|
|
|
.finally(() => setIsRemoving(false));
|
2023-03-30 11:34:41 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-08-29 14:41:06 +00:00
|
|
|
const timeZoneOptions = TIME_ZONES.map((timeZone) => ({
|
|
|
|
value: timeZone.value,
|
|
|
|
query: timeZone.label + " " + timeZone.value,
|
|
|
|
content: timeZone.label,
|
|
|
|
}));
|
|
|
|
|
2023-03-30 11:34:41 +00:00
|
|
|
return (
|
2023-04-08 08:16:46 +00:00
|
|
|
<WorkspaceAuthorizationLayout
|
2023-03-30 11:34:41 +00:00
|
|
|
breadcrumbs={
|
|
|
|
<Breadcrumbs>
|
|
|
|
<BreadcrumbItem title="My Profile" />
|
|
|
|
</Breadcrumbs>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<ImageUploadModal
|
|
|
|
isOpen={isImageUploadModalOpen}
|
|
|
|
onClose={() => setIsImageUploadModalOpen(false)}
|
2023-09-15 09:33:32 +00:00
|
|
|
isRemoving={isRemoving}
|
|
|
|
handleDelete={() => handleDelete(myProfile?.avatar, true)}
|
2023-03-30 11:34:41 +00:00
|
|
|
onSuccess={(url) => {
|
|
|
|
setValue("avatar", url);
|
|
|
|
handleSubmit(onSubmit)();
|
|
|
|
setIsImageUploadModalOpen(false);
|
|
|
|
}}
|
|
|
|
value={watch("avatar") !== "" ? watch("avatar") : undefined}
|
|
|
|
userImage
|
|
|
|
/>
|
|
|
|
{myProfile ? (
|
2023-09-15 09:33:32 +00:00
|
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
2023-09-15 14:00:53 +00:00
|
|
|
<div className="flex flex-row gap-2 h-full">
|
|
|
|
<div className="w-80 pt-8 overflow-y-hidden flex-shrink-0">
|
2023-09-15 09:33:32 +00:00
|
|
|
<SettingsSidebar />
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
2023-09-15 14:00:53 +00:00
|
|
|
<div className={`flex flex-col gap-8 pr-9 py-9 w-full overflow-y-auto`}>
|
2023-09-15 09:33:32 +00:00
|
|
|
<div className="relative h-44 w-full mt-6">
|
|
|
|
<img
|
|
|
|
src={
|
|
|
|
watch("cover_image") ??
|
|
|
|
"https://images.unsplash.com/photo-1506383796573-caf02b4a79ab"
|
|
|
|
}
|
|
|
|
className="h-44 w-full rounded-lg object-cover"
|
|
|
|
alt={myProfile?.name ?? "Cover image"}
|
|
|
|
/>
|
|
|
|
<div className="flex items-end justify-between absolute left-8 -bottom-6">
|
|
|
|
<div className="flex gap-3">
|
|
|
|
<div className="flex items-center justify-center bg-custom-background-90 h-16 w-16 rounded-lg">
|
|
|
|
<button type="button" onClick={() => setIsImageUploadModalOpen(true)}>
|
|
|
|
{!watch("avatar") || watch("avatar") === "" ? (
|
|
|
|
<div className="h-16 w-16 rounded-md bg-custom-background-80 p-2">
|
|
|
|
<UserIcon className="h-full w-full text-custom-text-200" />
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="relative h-16 w-16 overflow-hidden">
|
|
|
|
<img
|
|
|
|
src={watch("avatar")}
|
|
|
|
className="absolute top-0 left-0 h-full w-full object-cover rounded-lg"
|
|
|
|
onClick={() => setIsImageUploadModalOpen(true)}
|
|
|
|
alt={myProfile.display_name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
2023-09-15 09:33:32 +00:00
|
|
|
|
|
|
|
<div className="flex absolute right-3 bottom-3">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="cover_image"
|
|
|
|
render={() => (
|
2023-08-11 10:26:26 +00:00
|
|
|
<ImagePickerPopover
|
|
|
|
label={"Change cover"}
|
|
|
|
onChange={(imageUrl) => {
|
|
|
|
setValue("cover_image", imageUrl);
|
|
|
|
}}
|
|
|
|
value={
|
|
|
|
watch("cover_image") ??
|
|
|
|
"https://images.unsplash.com/photo-1506383796573-caf02b4a79ab"
|
|
|
|
}
|
|
|
|
/>
|
2023-09-15 09:33:32 +00:00
|
|
|
)}
|
|
|
|
/>
|
2023-08-11 10:26:26 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-09-15 09:33:32 +00:00
|
|
|
|
|
|
|
<div className="flex item-center justify-between px-8 mt-4">
|
|
|
|
<div className="flex flex-col">
|
|
|
|
<div className="flex item-center text-lg font-semibold text-custom-text-100">
|
|
|
|
<span>{`${watch("first_name")} ${watch("last_name")}`}</span>
|
|
|
|
</div>
|
|
|
|
<span className="text-sm tracking-tight">{watch("email")}</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Link href={`/${workspaceSlug}/profile/${myProfile.id}`}>
|
|
|
|
<a className="flex item-center cursor-pointer gap-2 h-4 leading-4 text-sm text-custom-primary-100">
|
|
|
|
<span className="h-4 w-4">
|
|
|
|
<UserCircle className="h-4 w-4" />
|
|
|
|
</span>
|
|
|
|
View Profile
|
|
|
|
</a>
|
|
|
|
</Link>
|
2023-05-05 11:37:29 +00:00
|
|
|
</div>
|
2023-09-15 09:33:32 +00:00
|
|
|
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-2 2xl:grid-cols-3 gap-6 px-8">
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">First Name</h4>
|
|
|
|
<Input
|
|
|
|
name="first_name"
|
|
|
|
id="first_name"
|
|
|
|
register={register}
|
|
|
|
error={errors.first_name}
|
|
|
|
placeholder="Enter your first name"
|
|
|
|
className="!px-3 !py-2 rounded-md font-medium"
|
|
|
|
autoComplete="off"
|
2023-09-25 10:45:49 +00:00
|
|
|
maxLength={24}
|
2023-09-15 09:33:32 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">Last Name</h4>
|
|
|
|
<Input
|
|
|
|
name="last_name"
|
|
|
|
register={register}
|
|
|
|
error={errors.last_name}
|
|
|
|
id="last_name"
|
|
|
|
placeholder="Enter your last name"
|
|
|
|
autoComplete="off"
|
|
|
|
className="!px-3 !py-2 rounded-md font-medium"
|
2023-09-25 10:45:49 +00:00
|
|
|
maxLength={24}
|
2023-09-15 09:33:32 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">Email</h4>
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
className="!px-3 !py-2 rounded-md font-medium"
|
|
|
|
error={errors.name}
|
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">Role</h4>
|
|
|
|
<Controller
|
|
|
|
name="role"
|
|
|
|
control={control}
|
|
|
|
rules={{ required: "This field is required" }}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomSelect
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
label={value ? value.toString() : "Select your role"}
|
|
|
|
buttonClassName={errors.role ? "border-red-500 bg-red-500/10" : ""}
|
|
|
|
width="w-full"
|
|
|
|
input
|
|
|
|
verticalPosition="top"
|
|
|
|
position="right"
|
|
|
|
>
|
|
|
|
{USER_ROLES.map((item) => (
|
|
|
|
<CustomSelect.Option key={item.value} value={item.value}>
|
|
|
|
{item.label}
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{errors.role && (
|
|
|
|
<span className="text-xs text-red-500">Please select a role</span>
|
2023-05-05 11:37:29 +00:00
|
|
|
)}
|
2023-09-15 09:33:32 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">Display name </h4>
|
|
|
|
|
|
|
|
<Input
|
|
|
|
id="display_name"
|
|
|
|
name="display_name"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
error={errors.display_name}
|
|
|
|
className="w-full"
|
|
|
|
placeholder="Enter your display name"
|
|
|
|
validations={{
|
|
|
|
required: "Display name is required.",
|
|
|
|
validate: (value) => {
|
|
|
|
if (value.trim().length < 1) return "Display name can't be empty.";
|
|
|
|
|
|
|
|
if (value.split(" ").length > 1)
|
|
|
|
return "Display name can't have two consecutive spaces.";
|
|
|
|
|
|
|
|
if (value.replace(/\s/g, "").length < 1)
|
|
|
|
return "Display name must be at least 1 characters long.";
|
|
|
|
|
|
|
|
if (value.replace(/\s/g, "").length > 20)
|
|
|
|
return "Display name must be less than 20 characters long.";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
<h4 className="text-sm">Timezone </h4>
|
|
|
|
|
|
|
|
<Controller
|
|
|
|
name="user_timezone"
|
|
|
|
control={control}
|
|
|
|
rules={{ required: "This field is required" }}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomSearchSelect
|
|
|
|
value={value}
|
|
|
|
label={
|
|
|
|
value
|
|
|
|
? TIME_ZONES.find((t) => t.value === value)?.label ?? value
|
|
|
|
: "Select a timezone"
|
|
|
|
}
|
|
|
|
options={timeZoneOptions}
|
|
|
|
onChange={onChange}
|
|
|
|
verticalPosition="top"
|
|
|
|
optionsClassName="w-full"
|
|
|
|
input
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{errors.role && (
|
|
|
|
<span className="text-xs text-red-500">Please select a role</span>
|
2023-08-29 14:41:06 +00:00
|
|
|
)}
|
2023-09-15 09:33:32 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex items-center justify-between py-2">
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
|
|
|
{isSubmitting ? "Updating Project..." : "Update Project"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
2023-08-29 14:41:06 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-09-15 09:33:32 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
2023-03-30 11:34:41 +00:00
|
|
|
) : (
|
|
|
|
<div className="grid h-full w-full place-items-center px-4 sm:px-0">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-04-08 08:16:46 +00:00
|
|
|
</WorkspaceAuthorizationLayout>
|
2023-03-30 11:34:41 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Profile;
|