import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { useDispatch, useSelector } from "react-redux"; import { toast } from "react-toastify"; import LoadingIcons from "react-loading-icons"; import { register } from "../redux/features/authSlice"; import "../register.css"; import Footer from "./Footer"; import Navbar from "./Navbar"; import "primeicons/primeicons.css"; const initialState = { title: "None", firstName: "", middleName: "", lastName: "", email: "", password: "", confirmPassword: "", termsconditions: "", userType: "", // New field for radio button selection }; const Register = () => { const [formValue, setFormValue] = useState(initialState); const [isFormValid, setIsFormValid] = useState(false); // New state variable const { loading, error } = useSelector((state) => ({ ...state.auth })); const { title, email, password, firstName, middleName, lastName, confirmPassword, termsconditions, userType, } = formValue; // include userType const dispatch = useDispatch(); const navigate = useNavigate(); useEffect(() => { error && toast.error(error); }, [error]); useEffect(() => { // Check if all fields are filled and radio button is selected const isValid = title !== "None" && email && password && firstName && middleName && lastName && confirmPassword && termsconditions && userType !== ""; // Validate userType selection setIsFormValid(isValid); }, [ title, email, password, firstName, middleName, lastName, confirmPassword, termsconditions, userType, ]); const handleSubmit = (e) => { e.preventDefault(); if (password !== confirmPassword) { return toast.error("Password should match"); } if (isFormValid) { dispatch(register({ formValue, navigate, toast })); } else { toast.error("Please fill in all fields and select all checkboxes"); } }; const capitalizeFirstLetter = (str) => { return str.charAt(0).toUpperCase() + str.slice(1); }; const onInputChange = (e) => { const { name, value, type, checked } = e.target; if (type === "checkbox") { // Set to true if checked, false if unchecked setFormValue((prevData) => ({ ...prevData, [name]: checked, })); } else { setFormValue((prevData) => ({ ...prevData, [name]: name === "email" || name === "password" || name === "confirmPassword" ? value : capitalizeFirstLetter(value), })); } }; // New handler for radio buttons const handleUserTypeChange = (e) => { setFormValue((prevData) => ({ ...prevData, userType: e.target.value, })); }; return ( <>




{/*