Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
2.2 kB
2
Indexable
Never
import React, { useState, useEffect } from 'react'
import { Login,Signup } from '../../Services/APIs';
import {ToastContainer, toast } from 'react-toastify';

const Auth = () => {

  const [formData1,setFormData1] = useState ({
    userName: '',
    pass: ''
  });

  const [formData2, setFormData2] = useState ({
    fullname: '',
    newUserName: '',
    Email: '',
    passWord: '',
    confirmPass: ''
  });

  const [errorMessage, setErrorMessage] = useState('');

  useEffect(() => {
    let timeout;
    if (errorMessage) {
      timeout = setTimeout(() => {
        setErrorMessage('');
      }, 2000); 
    }
    return () => {
      clearTimeout(timeout);
    };
  }, [errorMessage]);

  function handleChange (event, formName) {

    const {name,value} = event.target;
    if (formName === 'form1') {
      setFormData1(prevState => ({ ...prevState, [name]: value}));
    } else if (formName === 'form2') {
      setFormData2 (prevState => ({...prevState, [name]: value}));
    }
  }

  
  const handleSubmit1 = async (event) => {
    event.preventDefault();
    console.log(formData1);
    //apicall

    const loginRes = await Login (formData1);


  };

  const handleSubmit2 = async (event) => {
    event.preventDefault();
    console.log(formData2);

    if ( formData2.passWord !== formData2.confirmPass) {
        setErrorMessage('Passwords do not match');
        
    } else {

      setErrorMessage('');

      try {

        const signupRes = await Signup(formData2);
        console.log(signupRes.data)
        if (signupRes.status === 200 ) {

            toast.success('Signup Successful!', {
            autoClose: 2000,
            hideProgressBar: true,
            pauseOnHover: false,
          });

        } else if (signupRes.status === 400) {
          toast.error('Account Creation Failed!', {
            
            autoClose: 2000,
            hideProgressBar: true,
            pauseOnHover: false
          });
        }


      } catch (error) {
         console.log(error);
      }
      
      
    }
    
      
  };