2023-03-30 11:34:41 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
// 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-06-23 05:39:03 +00:00
|
|
|
import SettingsNavbar from "layouts/settings-navbar";
|
2023-03-30 11:34:41 +00:00
|
|
|
// components
|
2023-05-11 13:10:17 +00:00
|
|
|
import { ImageUploadModal } from "components/core";
|
2023-03-30 11:34:41 +00:00
|
|
|
// ui
|
2023-03-30 12:17:49 +00:00
|
|
|
import { CustomSelect, DangerButton, Input, SecondaryButton, 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-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-03-30 11:34:41 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<IUser> = {
|
|
|
|
avatar: "",
|
|
|
|
first_name: "",
|
|
|
|
last_name: "",
|
|
|
|
email: "",
|
2023-03-30 12:17:49 +00:00
|
|
|
role: "",
|
2023-03-30 11:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Profile: NextPage = () => {
|
|
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
|
|
const [isRemoving, setIsRemoving] = useState(false);
|
|
|
|
const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false);
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
const payload: Partial<IUser> = {
|
|
|
|
first_name: formData.first_name,
|
|
|
|
last_name: formData.last_name,
|
|
|
|
avatar: formData.avatar,
|
2023-03-30 12:17:49 +00:00
|
|
|
role: formData.role,
|
2023-03-30 11:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await userService
|
|
|
|
.updateUser(payload)
|
|
|
|
.then((res) => {
|
|
|
|
mutateUser((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
2023-06-07 13:33:49 +00:00
|
|
|
return { ...prevData, ...res };
|
2023-03-30 11:34:41 +00:00
|
|
|
}, false);
|
|
|
|
setIsEditing(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.",
|
|
|
|
});
|
|
|
|
mutateUser((prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
2023-06-07 13:33:49 +00:00
|
|
|
return { ...prevData, avatar: "" };
|
2023-03-30 11:34:41 +00:00
|
|
|
}, false);
|
|
|
|
})
|
|
|
|
.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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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)}
|
|
|
|
onSuccess={(url) => {
|
|
|
|
setValue("avatar", url);
|
|
|
|
handleSubmit(onSubmit)();
|
|
|
|
setIsImageUploadModalOpen(false);
|
|
|
|
}}
|
|
|
|
value={watch("avatar") !== "" ? watch("avatar") : undefined}
|
|
|
|
userImage
|
|
|
|
/>
|
|
|
|
{myProfile ? (
|
2023-06-23 05:39:03 +00:00
|
|
|
<div className="p-8">
|
|
|
|
<div className="mb-8 space-y-6">
|
2023-05-05 11:37:29 +00:00
|
|
|
<div>
|
|
|
|
<h3 className="text-3xl font-semibold">Profile Settings</h3>
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="mt-1 text-custom-text-200">
|
2023-05-05 11:37:29 +00:00
|
|
|
This information will be visible to only you.
|
2023-03-30 11:34:41 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
<SettingsNavbar profilePage />
|
|
|
|
</div>
|
|
|
|
<div className="space-y-8 sm:space-y-12">
|
|
|
|
<div className="grid grid-cols-12 gap-4 sm:gap-16">
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h4 className="text-lg font-semibold text-custom-text-100">Profile Picture</h4>
|
|
|
|
<p className="text-sm text-custom-text-200">
|
2023-05-05 11:37:29 +00:00
|
|
|
Max file size is 5MB. Supported file types are .jpg and .png.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
<button type="button" onClick={() => setIsImageUploadModalOpen(true)}>
|
|
|
|
{!watch("avatar") || watch("avatar") === "" ? (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="h-12 w-12 rounded-md bg-custom-background-80 p-2">
|
|
|
|
<UserIcon className="h-full w-full text-custom-text-200" />
|
2023-05-05 11:37:29 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="relative h-12 w-12 overflow-hidden">
|
2023-06-06 20:26:21 +00:00
|
|
|
<img
|
2023-05-05 11:37:29 +00:00
|
|
|
src={watch("avatar")}
|
2023-06-06 20:26:21 +00:00
|
|
|
className="absolute top-0 left-0 h-full w-full object-cover rounded-md"
|
2023-05-05 11:37:29 +00:00
|
|
|
onClick={() => setIsImageUploadModalOpen(true)}
|
2023-06-06 20:26:21 +00:00
|
|
|
alt={myProfile.first_name}
|
2023-05-05 11:37:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<SecondaryButton
|
|
|
|
onClick={() => {
|
|
|
|
setIsImageUploadModalOpen(true);
|
|
|
|
}}
|
2023-03-30 11:34:41 +00:00
|
|
|
>
|
2023-05-05 11:37:29 +00:00
|
|
|
Upload
|
|
|
|
</SecondaryButton>
|
|
|
|
{myProfile.avatar && myProfile.avatar !== "" && (
|
|
|
|
<DangerButton
|
|
|
|
onClick={() => handleDelete(myProfile.avatar, true)}
|
|
|
|
loading={isRemoving}
|
|
|
|
>
|
|
|
|
{isRemoving ? "Removing..." : "Remove"}
|
|
|
|
</DangerButton>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
<div className="grid grid-cols-12 gap-4 sm:gap-16">
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h4 className="text-lg font-semibold text-custom-text-100">Full Name</h4>
|
|
|
|
<p className="text-sm text-custom-text-200">
|
2023-05-05 11:37:29 +00:00
|
|
|
This name will be reflected on all the projects you are working on.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-12 flex items-center gap-2 sm:col-span-6">
|
|
|
|
<Input
|
|
|
|
name="first_name"
|
|
|
|
id="first_name"
|
|
|
|
register={register}
|
|
|
|
error={errors.first_name}
|
|
|
|
placeholder="Enter your first name"
|
|
|
|
autoComplete="off"
|
|
|
|
validations={{
|
|
|
|
required: "This field is required.",
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
name="last_name"
|
|
|
|
register={register}
|
|
|
|
error={errors.last_name}
|
|
|
|
id="last_name"
|
|
|
|
placeholder="Enter your last name"
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
<div className="grid grid-cols-12 gap-4 sm:gap-16">
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h4 className="text-lg font-semibold text-custom-text-100">Email</h4>
|
|
|
|
<p className="text-sm text-custom-text-200">
|
2023-05-05 11:37:29 +00:00
|
|
|
The email address that you are using.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
autoComplete="off"
|
|
|
|
register={register}
|
|
|
|
error={errors.name}
|
|
|
|
className="w-full"
|
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
<div className="grid grid-cols-12 gap-4 sm:gap-16">
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h4 className="text-lg font-semibold text-custom-text-100">Role</h4>
|
|
|
|
<p className="text-sm text-custom-text-200">Add your role.</p>
|
2023-05-05 11:37:29 +00:00
|
|
|
</div>
|
|
|
|
<div className="col-span-12 sm:col-span-6">
|
|
|
|
<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"}
|
|
|
|
width="w-full"
|
|
|
|
input
|
|
|
|
position="right"
|
|
|
|
>
|
|
|
|
{USER_ROLES.map((item) => (
|
|
|
|
<CustomSelect.Option key={item.value} value={item.value}>
|
|
|
|
{item.label}
|
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
<div className="sm:text-right">
|
|
|
|
<SecondaryButton onClick={handleSubmit(onSubmit)} loading={isSubmitting}>
|
|
|
|
{isSubmitting ? "Updating..." : "Update profile"}
|
|
|
|
</SecondaryButton>
|
2023-04-20 08:11:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-03-30 11:34:41 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<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;
|