/* eslint-disable @next/next/no-img-element */ import Head from "next/head"; import {withIronSessionSsr} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import {ChangeEvent, useEffect, useRef, useState} from "react"; import useUser from "@/hooks/useUser"; import {toast, ToastContainer} from "react-toastify"; import Layout from "@/components/High/Layout"; import Input from "@/components/Low/Input"; import Button from "@/components/Low/Button"; import Link from "next/link"; import axios from "axios"; import {ErrorMessage} from "@/constants/errors"; import {RadioGroup} from "@headlessui/react"; import clsx from "clsx"; import {EmploymentStatus, EMPLOYMENT_STATUS, Gender, User} from "@/interfaces/user"; import CountrySelect from "@/components/Low/CountrySelect"; import {shouldRedirectHome} from "@/utils/navigation.disabled"; import moment from "moment"; import {BsCamera, BsCameraFill} from "react-icons/bs"; import {USER_TYPE_LABELS} from "@/resources/user"; import useGroups from "@/hooks/useGroups"; import useUsers from "@/hooks/useUsers"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { res.setHeader("location", "/login"); res.statusCode = 302; res.end(); return { props: { user: null, }, }; } if (shouldRedirectHome(user)) { res.setHeader("location", "/"); res.statusCode = 302; res.end(); return { props: { user: null, }, }; } return { props: {user: req.session.user}, }; }, sessionOptions); interface Props { user: User; mutateUser: Function; } function UserProfile({user, mutateUser}: Props) { const [bio, setBio] = useState(user.bio || ""); const [name, setName] = useState(user.name || ""); const [email, setEmail] = useState(user.email || ""); const [password, setPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [profilePicture, setProfilePicture] = useState(user.profilePicture); const [country, setCountry] = useState(user.demographicInformation?.country || ""); const [phone, setPhone] = useState(user.demographicInformation?.phone || ""); const [gender, setGender] = useState(user.demographicInformation?.gender || undefined); const [employment, setEmployment] = useState( user.type === "corporate" ? undefined : user.demographicInformation?.employment, ); const [position, setPosition] = useState(user.type === "corporate" ? user.demographicInformation?.position : undefined); const [companyName, setCompanyName] = useState(user.type === "agent" ? user.agentInformation?.companyName : undefined); const [commercialRegistration, setCommercialRegistration] = useState( user.type === "agent" ? user.agentInformation?.commercialRegistration : undefined, ); const {groups} = useGroups(); const {users} = useUsers(); const profilePictureInput = useRef(null); const expirationDateColor = (date: Date) => { const momentDate = moment(date); const today = moment(new Date()); if (today.add(1, "days").isAfter(momentDate)) return "!bg-mti-red-ultralight border-mti-red-light"; if (today.add(3, "days").isAfter(momentDate)) return "!bg-mti-rose-ultralight border-mti-rose-light"; if (today.add(7, "days").isAfter(momentDate)) return "!bg-mti-orange-ultralight border-mti-orange-light"; }; const convertBase64 = (file: File) => { return new Promise((resolve, reject) => { const fileReader = new FileReader(); fileReader.readAsDataURL(file); fileReader.onload = () => { resolve(fileReader.result); }; fileReader.onerror = (error) => { reject(error); }; }); }; const uploadProfilePicture = async (event: ChangeEvent) => { if (event.target.files && event.target.files[0]) { const picture = event.target.files[0]; const base64 = await convertBase64(picture); setProfilePicture(base64 as string); } }; const updateUser = async () => { setIsLoading(true); if (email !== user?.email && !password) { toast.error("To update your e-mail you need to input your password!"); setIsLoading(false); return; } if (newPassword && !password) { toast.error("To update your password you need to input your current one!"); setIsLoading(false); return; } if (email !== user?.email) { const userAdmins = groups.filter((x) => x.participants.includes(user.id)).map((x) => x.admin); const message = users.filter((x) => userAdmins.includes(x.id) && x.type === "corporate").length > 0 ? "If you change your e-mail address, you will lose all benefits from your university/institute. Are you sure you want to continue?" : "Are you sure you want to update your e-mail address?"; if (!confirm(message)) { setIsLoading(false); return; } } const request = await axios.post("/api/users/update", { bio, name, email, password, newPassword, profilePicture, demographicInformation: { phone, country, employment: user?.type === "corporate" ? undefined : employment, position: user?.type === "corporate" ? position : undefined, gender, }, }); if (request.status === 200) { toast.success("Your profile has been updated!"); mutateUser((request.data as {user: User}).user); setIsLoading(false); return; } toast.error((request.data as ErrorMessage).message); setIsLoading(false); }; return (

Edit Profile

Edit Profile

setName(e)} placeholder="Enter your name" defaultValue={name} required /> setEmail(e)} placeholder="Enter email address" defaultValue={email} required />
setPassword(e)} placeholder="Enter your password" required /> setNewPassword(e)} placeholder="Enter your new password (optional)" />
{user.type === "agent" && (
null} placeholder="Enter corporate name" defaultValue={companyName} disabled /> null} placeholder="Enter commercial registration" defaultValue={commercialRegistration} disabled />
)}
setPhone(e)} placeholder="Enter phone number" defaultValue={phone} required />
{user.type === "corporate" && ( )} {user.type !== "corporate" && (
{EMPLOYMENT_STATUS.map(({status, label}) => ( {({checked}) => ( {label} )} ))}
)}
{({checked}) => ( Male )} {({checked}) => ( Female )} {({checked}) => ( Other )}
{!user.subscriptionExpirationDate && "Unlimited"} {user.subscriptionExpirationDate && moment(user.subscriptionExpirationDate).format("DD/MM/YYYY")}
(profilePictureInput.current as any)?.click()}>
{user.name}
(profilePictureInput.current as any)?.click()} className="cursor-pointer text-mti-purple-light text-sm"> Change picture
{USER_TYPE_LABELS[user.type]}
{user.type === "agent" && (
{user.demographicInformation?.country.toLowerCase()
)}
Bio