/* eslint-disable @next/next/no-img-element */ import {toast, ToastContainer} from "react-toastify"; import {useState} from "react"; import Head from "next/head"; import useUser from "@/hooks/useUser"; import Button from "@/components/Low/Button"; import {BsArrowRepeat} from "react-icons/bs"; import Link from "next/link"; import Input from "@/components/Low/Input"; import axios from "axios"; import {Divider} from "primereact/divider"; import {useRouter} from "next/router"; import clsx from "clsx"; import {EmploymentStatus, EMPLOYMENT_STATUS, Gender} from "@/interfaces/user"; import countryCodes from "country-codes-list"; import {RadioGroup} from "@headlessui/react"; export default function Register() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [country, setCountry] = useState(); const [phone, setPhone] = useState(); const [gender, setGender] = useState(); const [employment, setEmployment] = useState(); const [step, setStep] = useState<0 | 1>(0); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const {user, mutateUser} = useUser({ redirectTo: "/", redirectIfFound: true, }); const register = (e: any) => { e.preventDefault(); if (confirmPassword !== password) { toast.error("Your passwords do not match!", {toastId: "password-not-match"}); setStep(0); return; } setIsLoading(true); axios .post("/api/register", { name, email, password, profilePicture: "/defaultAvatar.png", demographicInformation: { country, phone, gender, employment, }, }) .then((response) => { mutateUser(response.data.user).then(sendEmailVerification); }) .catch((error) => { console.log(error.response.data); if (error.response.status === 401) { toast.error("There is already a user with that e-mail!"); setStep(0); return; } toast.error("There was something wrong, please try again!"); }) .finally(() => setIsLoading(false)); }; const sendEmailVerification = () => { setIsLoading(true); axios .post<{ok: boolean}>("/api/reset/sendVerification", {}) .catch((e) => { console.log(e); toast.error("Something went wrong, please logout and re-login.", {toastId: "send-verify-error"}); }) .finally(() => setIsLoading(false)); }; const logout = async () => { axios.post("/api/logout").finally(() => { setTimeout(() => router.reload(), 500); }); }; const initialStep = () => ( <>
{ e.preventDefault(); setStep(1); }}> setName(e)} placeholder="Enter your name" defaultValue={name} required /> setEmail(e)} placeholder="Enter email address" defaultValue={email} required /> setPassword(e)} placeholder="Enter your password" defaultValue={password} required /> setConfirmPassword(e)} placeholder="Confirm your password" defaultValue={confirmPassword} required />
); const demographicStep = () => ( <>
setPhone(e)} placeholder="Enter phone number" defaultValue={phone} required />
{({checked}) => ( Male )} {({checked}) => ( Female )} {({checked}) => ( Other )}
{EMPLOYMENT_STATUS.map(({status, label}) => ( {({checked}) => ( {label} )} ))}
); return ( <> Register | EnCoach
People smiling looking at a tablet
EnCoach's Logo

{step === 1 ? "Demographic Information" : "Create new account"}

{!user && ( <> {step === 0 && initialStep()} {step === 1 && demographicStep()} Sign in instead )} {user && !user.isVerified && ( <>

Please confirm your account!

An e-mail has been sent to {user.email}, please click the link in it to confirm your account to be able to use the application.

Please refresh this page once it has been verified.
)}
); }