/* eslint-disable @next/next/no-img-element */ import Head from "next/head"; import Navbar from "@/components/Navbar"; import {BsFileEarmarkText, BsPencil, BsStar, BsBook, BsHeadphones, BsPen, BsMegaphone, BsArrowRepeat} from "react-icons/bs"; import {withIronSessionSsr} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import {ChangeEvent, useEffect, useRef, useState} from "react"; import useStats from "@/hooks/useStats"; import {averageScore, totalExams} from "@/utils/stats"; import useUser from "@/hooks/useUser"; import Sidebar from "@/components/Sidebar"; import Diagnostic from "@/components/Diagnostic"; import {toast, ToastContainer} from "react-toastify"; import {capitalize} from "lodash"; import {Module} from "@/interfaces"; import ProgressBar from "@/components/Low/ProgressBar"; import Layout from "@/components/High/Layout"; import {calculateAverageLevel} from "@/utils/score"; import Input from "@/components/Low/Input"; import Button from "@/components/Low/Button"; import {useRouter} from "next/router"; 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} from "@/interfaces/user"; import countryCodes from "country-codes-list"; 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, }, }; } return { props: {user: req.session.user}, }; }, sessionOptions); export default function Home() { const [bio, setBio] = useState(""); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [profilePicture, setProfilePicture] = useState(""); const [country, setCountry] = useState(); const [phone, setPhone] = useState(); const [gender, setGender] = useState(); const [employment, setEmployment] = useState(); const profilePictureInput = useRef(null); const router = useRouter(); const {user, mutateUser} = useUser({redirectTo: "/login"}); useEffect(() => { if (user) { setName(user.name); setEmail(user.email); setBio(user.bio); setProfilePicture(user.profilePicture); setCountry(user.demographicInformation?.country); setPhone(user.demographicInformation?.phone); setGender(user.demographicInformation?.gender); setEmployment(user.demographicInformation?.employment); } }, [user]); 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; } const request = await axios.post("/api/users/update", { bio, name, email, password, newPassword, profilePicture, demographicInformation: { phone, country, employment, gender, }, }); if (request.status === 200) { toast.success("Your profile has been updated!"); mutateUser(); setIsLoading(false); return; } toast.error((request.data as ErrorMessage).message); setIsLoading(false); }; return ( <> EnCoach | Muscat Training Institute {user && (

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)" />
setPhone(e)} placeholder="Enter phone number" defaultValue={phone} required />
{EMPLOYMENT_STATUS.map(({status, label}) => ( {({checked}) => ( {label} )} ))}
{({checked}) => ( Male )} {({checked}) => ( Female )} {({checked}) => ( Other )}
{user.name} (profilePictureInput.current as any)?.click()} className="cursor-pointer text-mti-purple-light text-sm"> Change picture
Bio