diff --git a/src/components/AuthPages/SignUp/index.jsx b/src/components/AuthPages/SignUp/index.jsx index 55769f6..af9c4ca 100755 --- a/src/components/AuthPages/SignUp/index.jsx +++ b/src/components/AuthPages/SignUp/index.jsx @@ -6,12 +6,128 @@ import titleShape from "../../../assets/images/shape/title-shape-two.svg"; import InputCom from "../../Helpers/Inputs/InputCom"; import { Link, useNavigate } from 'react-router-dom'; +import usersService from "../../../services/UsersService"; // site api services + export default function SignUp() { const navigate = useNavigate(); + + const userSignup = new usersService(); + + const [loading, setLoading] = useState(false) // For loading spinner + + const [errorMessage, setErrorMessage] = useState({ // For Displaying success or error message + success: false, + message: '' + }) + const [checked, setValue] = useState(false); const rememberMe = () => { setValue(!checked); }; + + // state for user inputed values + const [userDetails, setUserDetails] = useState({ + // username: '', + email: '', + password: '', + confirm_password: '', + firstname: '', + lastname: '' + }) + + const handleInputChange = ({target:{name, value}}) => { // function that listens to input change + setUserDetails(prev => { + return {...prev, [name]:value} + }) + } + + const handleUserSignup = async () => { // handles input validation and submits to api call + + setErrorMessage({ // resets the error message to empty string + success: false, + message: '' + }) + + setLoading(true) // Sets loading spinner + + let userInfo = {...userDetails} // assigns userDetails to be user information + + let {email, password, confirm_password, firstname, lastname} = userInfo + + if(!email || !password || !confirm_password || !firstname || !lastname){ // checks if any field is empty + setLoading(false) + setErrorMessage({ + success: false, + message: 'Please Fill all inputs' + }) + return + } + + + //checks if email is a valid email address + let regEx = /^[^0-9][a-zA-Z0-9._%+-]+@[a-zA-Z]+(\.[a-zA-Z]+)+$/; + if (regEx.test(email) == false) { + setLoading(false) + setErrorMessage({ + success: false, + message: 'Please Input a valid email; e.g: text@gmail.com' + }) + return + } + + if(password != confirm_password){ //checks if password matches confirm password + setLoading(false) + setErrorMessage({ + success: false, + message: 'Password do not match' + }) + return + } + + //checks if password is matches alphanumeric with at least one uppercase letter + // let PwdRegEx = /[A-Z]/; + if (/[A-Z]/.test(password) == false) { + setLoading(false) + setErrorMessage({ + success: false, + message: 'Password must contain at least one uppercase character; e.g: Text123' + }) + return + } + + userInfo.username = email // assigns email as username also + userInfo.mode = 'START' // assigns mode as START + + delete userInfo.confirm_password // deletes confrim password before making API call + + try { + const res = await userSignup.signupUser(userInfo); + if(res.status != 200 || res.data.status < 1){ + setLoading(false) + setErrorMessage({ + success: false, + message: 'Could not create account try again later' + }) + return + } + // if status code is 200 proceed + setErrorMessage({ + success: true, + message: 'Account created successfully' + }) + setTimeout(()=>{ + setLoading(false) + navigate("/verify-signup", { replace: true }) + }, 1000) + } catch (error) { + setLoading(false) + setErrorMessage({ + success: false, + message: 'Opps! something went wrong, Try agian later' + }) + } + } + return ( <>