import React, { useEffect, useState } from "react"; import Link from "next/link"; import Image from "next/image"; import { useRouter } from "next/router"; import useSWR from "swr"; import { useForm } from "react-hook-form"; import { ChevronRightIcon, ClipboardDocumentListIcon, PencilIcon, RectangleStackIcon, UserIcon, UserPlusIcon, XMarkIcon, } from "@heroicons/react/24/outline"; import type { NextPage, NextPageContext } from "next"; // hooks import type { IIssue, IUser } from "types"; import useUser from "hooks/use-user"; import useToast from "hooks/use-toast"; // lib import { requiredAuth } from "lib/auth"; // services import projectService from "services/project.service"; // layouts import AppLayout from "layouts/app-layout"; // constants import { USER_ISSUE, USER_WORKSPACE_INVITATIONS, PROJECTS_LIST } from "constants/fetch-keys"; // services import userService from "services/user.service"; import workspaceService from "services/workspace.service"; // components import { ImageUploadModal } from "components/common/image-upload-modal"; // ui import { Button, Input, Spinner } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons // types const defaultValues: Partial = { avatar: "", first_name: "", last_name: "", email: "", }; const Profile: NextPage = () => { const [isEditing, setIsEditing] = useState(false); const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false); const { query: { workspaceSlug }, } = useRouter(); const { register, handleSubmit, reset, watch, setValue, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const { setToastAlert } = useToast(); const { user: myProfile, mutateUser } = useUser(); const { data: myIssues } = useSWR( myProfile && workspaceSlug ? USER_ISSUE(workspaceSlug as string) : null, myProfile && workspaceSlug ? () => userService.userIssues(workspaceSlug as string) : null ); const { data: invitations } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); const { data: projects } = useSWR( workspaceSlug ? PROJECTS_LIST(workspaceSlug as string) : null, () => (workspaceSlug ? () => projectService.getProjects(workspaceSlug as string) : null) ); useEffect(() => { reset({ ...defaultValues, ...myProfile }); }, [myProfile, reset]); const onSubmit = (formData: IUser) => { const payload: Partial = { id: formData.id, first_name: formData.first_name, last_name: formData.last_name, avatar: formData.avatar, }; userService .updateUser(payload) .then((response) => { mutateUser((prevData) => { if (!prevData) return prevData; return { ...prevData, user: { ...payload, ...response } }; }); setIsEditing(false); setToastAlert({ title: "Success", type: "success", message: "Profile updated successfully", }); }) .catch((error) => { console.log(error); }); }; const quickLinks = [ { icon: RectangleStackIcon, title: "My Issues", number: myIssues?.length ?? 0, description: "View the list of issues assigned to you for this workspace.", href: `/${workspaceSlug}/me/my-issues`, }, { icon: ClipboardDocumentListIcon, title: "My Projects", number: projects?.length ?? 0, description: "View the list of projects of the workspace.", href: `/${workspaceSlug}/projects`, }, { icon: UserPlusIcon, title: "Workspace Invitations", number: invitations?.length ?? 0, description: "View your workspace invitations.", href: "/invitations", }, ]; return ( setIsImageUploadModalOpen(false)} onSuccess={(url) => { setValue("avatar", url); handleSubmit(onSubmit)(); setIsImageUploadModalOpen(false); }} value={watch("avatar") !== "" ? watch("avatar") : undefined} />
{myProfile ? ( <>
{!watch("avatar") || watch("avatar") === "" ? ( ) : (
{myProfile.first_name} setIsImageUploadModalOpen(true)} />
)}

Max file size is 5MB.
Supported file types are .jpg and .png.

First Name

{isEditing ? ( ) : (

{myProfile.first_name}

)}

Last Name

{isEditing ? ( ) : (

{myProfile.last_name}

)}

Email ID

{myProfile.email}

{isEditing && (
)}

Quick Links

{quickLinks.map((item, index) => (

{item.title}

{item.number}

{item.description}

))}
) : (
)}
); }; export const getServerSideProps = async (ctx: NextPageContext) => { const user = await requiredAuth(ctx.req?.headers.cookie); const redirectAfterSignIn = ctx.req?.url; if (!user) { return { redirect: { destination: `/signin?next=${redirectAfterSignIn}`, permanent: false, }, }; } return { props: { user, }, }; }; export default Profile;