import { useState, useEffect } from 'react'; import { useDispatch } from 'react-redux'; import QRCode from './QRCode' import Authorization from './Authorization' import SMSCode from './SMSCode' import Registration from './Registration' import { authorizeUser } from '../../api-data' import { asyncLogin } from '../../redux/authorization/operations' const AuthPage = () => { const [isQR, setIsQR] = useState(false) const [isCode, setIsCode] = useState('') const [isChecked, setIsChecked] = useState(true); const [isReg, setIsReg] = useState(false); const [country, setCountry] = useState('Country') const [number, setNumber] = useState('') const dispatch = useDispatch() useEffect(() => { const isCheckedLC = localStorage.isChecked isCheckedLC && setIsChecked(isCheckedLC === 'true'?true:false) }, []) const format = (a: string) => a.split(' ').join('').trim() const handleIsQR = (): void => setIsQR(!isQR) const handleSendCode = async (): Promise => { const res = await authorizeUser(format(number), country) res&&setIsCode(res) } const handleIsValidCode = async (e: React.ChangeEvent):Promise => { if (isCode && format(e.target.value) === isCode) dispatch(await asyncLogin(number, isCode, () => setIsReg(!isReg))) } const handleIsCheck = (e: React.ChangeEvent): void => { const flag = e.target.checked setIsChecked(flag); localStorage.isChecked = flag } const handleResetState = ():void => { setIsCode('') setCountry('Country') setNumber('') } return ( isReg?: isCode ? : isQR ? : ) }; export default AuthPage;