|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | + |
| 3 | +import { connect } from 'react-redux'; |
| 4 | +import { Alert } from '@mui/material'; |
| 5 | +import CircularProgress from '@mui/material/CircularProgress'; |
| 6 | + |
| 7 | +import { restVerifyEmail, verifyEmail } from '../../redux/actions'; |
| 8 | +import { ReducerType } from '../../redux/reducers/rootReducer'; |
| 9 | +import LogInComponent from '../../pages/login'; |
| 10 | + |
| 11 | +const VerfiyEmailPageComponent = (props: any) => { |
| 12 | + const autoScrollToBottomRef = useRef<HTMLDivElement>(null); |
| 13 | + const [showAlert, setShowAlert] = useState<boolean>(false); |
| 14 | + const [logIn, setLogIn] = useState<boolean>(false); |
| 15 | + |
| 16 | + const { confirmEmailIsLoading, confirmEmailIsSuccess, confirmEmailIsError, confirmEmailIsMessage } = props.authState; |
| 17 | + |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + setLogIn(() => false); |
| 21 | + props.restVerifyEmail(); |
| 22 | + }, []) |
| 23 | + |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (props.userId, props.token) { |
| 27 | + const finalData = { |
| 28 | + userId: props.userId, |
| 29 | + token: props.token |
| 30 | + }; |
| 31 | + props.verifyEmail(finalData); |
| 32 | + } |
| 33 | + }, [props.userId, props.token]); |
| 34 | + |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + window.scrollTo({ |
| 38 | + top: 0, |
| 39 | + behavior: 'smooth' |
| 40 | + }); |
| 41 | + // Auto Scroll functionality |
| 42 | + autoScrollToBottomRef?.current?.scrollIntoView({ |
| 43 | + behavior: 'smooth', |
| 44 | + }); |
| 45 | + }, []); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (confirmEmailIsSuccess) { |
| 49 | + const timer = setTimeout(() => { |
| 50 | + setShowAlert(() => false); |
| 51 | + setLogIn(() => true); |
| 52 | + props.restVerifyEmail(); |
| 53 | + }, 5000); |
| 54 | + return () => clearTimeout(timer); |
| 55 | + } |
| 56 | + }, [confirmEmailIsSuccess]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (confirmEmailIsSuccess || confirmEmailIsError) { |
| 60 | + setShowAlert(() => true); |
| 61 | + if (confirmEmailIsError) { |
| 62 | + const timer = setTimeout(() => { |
| 63 | + setShowAlert(() => false); |
| 64 | + setLogIn(() => false); |
| 65 | + props.restVerifyEmail(); |
| 66 | + }, 30000); |
| 67 | + return () => clearTimeout(timer); |
| 68 | + } |
| 69 | + } |
| 70 | + }, [confirmEmailIsSuccess, confirmEmailIsError]); |
| 71 | + |
| 72 | + if (logIn) { |
| 73 | + return <LogInComponent />; |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="flex items-center justify-center py-[3rem] "> |
| 78 | + <div className=" mx-auto w-[90%] md:max-w-[30rem] md:min-w-[20rem]"> |
| 79 | + <div |
| 80 | + className=" w-full rounded-[6px] mt-[2rem] " |
| 81 | + style={{ |
| 82 | + boxShadow: '0 2px 8px rgba(0, 0, 0, 0.2)' |
| 83 | + }} |
| 84 | + > |
| 85 | + {confirmEmailIsLoading && ( |
| 86 | + <div className=" flex items-center justify-center pt-4 "> |
| 87 | + <CircularProgress color="secondary" /> |
| 88 | + </div> |
| 89 | + )} |
| 90 | + {showAlert && (confirmEmailIsError || confirmEmailIsSuccess) && ( |
| 91 | + <Alert |
| 92 | + variant="filled" |
| 93 | + severity={confirmEmailIsError ? 'error' : 'success'} |
| 94 | + onClose={() => setShowAlert(false)} |
| 95 | + > |
| 96 | + {confirmEmailIsMessage} |
| 97 | + </Alert> |
| 98 | + )} |
| 99 | + <section> |
| 100 | + <div className="title p-[1rem] text-center "> |
| 101 | + <div className="flex justify-center mt-[2rem]"> |
| 102 | + <button |
| 103 | + onClick={() => setLogIn(true)} |
| 104 | + className="py-[8px] px-[1.3rem] block bg-[#42b72a] transition duration-150 text-white text-[1rem] border border-[#42b72a] hover:border-[#256818] hover:bg-[#256818] h-[2.7rem] duration-150 rounded-[4px] text-white font-bold h-[2.7rem] " |
| 105 | + > |
| 106 | + Back to Login? |
| 107 | + </button> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </section> |
| 111 | + </div> |
| 112 | + |
| 113 | + <div className="flex justify-center mb-[8px] mt-[2rem] space-x-3"> |
| 114 | + <span className="custom-span">Conditions of Use</span> |
| 115 | + <span className="custom-span"> Privacy Notice </span> |
| 116 | + <span className="custom-span"> Help </span> |
| 117 | + </div> |
| 118 | + <div className="flex justify-center mb-[8px] space-x-3"> |
| 119 | + <span className="text-[14.4px] text-[#555]">© 2004-2021, saddamarbaa.com, Inc. or its affiliates</span> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +const mapStateToProps = (state: ReducerType) => { |
| 127 | + return { |
| 128 | + authState: state.auth |
| 129 | + }; |
| 130 | +}; |
| 131 | + |
| 132 | +const mapDispatchToProps = { |
| 133 | + restVerifyEmail, |
| 134 | + verifyEmail |
| 135 | +}; |
| 136 | + |
| 137 | +export default connect( |
| 138 | + mapStateToProps, |
| 139 | + mapDispatchToProps |
| 140 | +)(VerfiyEmailPageComponent); |
0 commit comments