|
|
@ -5,75 +5,112 @@ import "./index.scss"; |
|
|
|
import { isEmail } from "../../../../_helpers/validateEmail"; |
|
|
|
import { isEmail } from "../../../../_helpers/validateEmail"; |
|
|
|
import { apiCaller } from "../../../../_helpers"; |
|
|
|
import { apiCaller } from "../../../../_helpers"; |
|
|
|
import { stepAuthConstants } from "../../../../_constants/auth"; |
|
|
|
import { stepAuthConstants } from "../../../../_constants/auth"; |
|
|
|
|
|
|
|
import { isPhone } from "../../../../_helpers/validatePhone"; |
|
|
|
|
|
|
|
import { renderPhoneIcon } from "../AddressStudying/renderIcon"; |
|
|
|
|
|
|
|
|
|
|
|
const CreateAccount = (props) => { |
|
|
|
const CreateAccount = (props) => { |
|
|
|
const [email, setEmail] = useState(""); |
|
|
|
const [email, setEmail] = useState(""); |
|
|
|
|
|
|
|
const [phone, setPhone] = useState(""); |
|
|
|
const [password, setPassword] = useState(""); |
|
|
|
const [password, setPassword] = useState(""); |
|
|
|
const [rePassword, setRePassword] = useState(""); |
|
|
|
const [rePassword, setRePassword] = useState(""); |
|
|
|
const [emailError, setEmailError] = useState(""); |
|
|
|
const [emailError, setEmailError] = useState(""); |
|
|
|
|
|
|
|
const [phoneError, setPhoneError] = useState(""); |
|
|
|
const [pwError, setPwError] = useState(""); |
|
|
|
const [pwError, setPwError] = useState(""); |
|
|
|
const [rePwError, setRePwError] = useState(""); |
|
|
|
const [rePwError, setRePwError] = useState(""); |
|
|
|
const [cacheEmailPass, setCacheEmailPass] = useState([]); |
|
|
|
const [cacheEmailPass, setCacheEmailPass] = useState([]); |
|
|
|
const [cacheEmailFail, setCacheEmailFail] = useState([]); |
|
|
|
const [cacheEmailFail, setCacheEmailFail] = useState([]); |
|
|
|
|
|
|
|
const [cachePhoneFail, setCachePhoneFail] = useState([]); |
|
|
|
const [disabledBtn, setDisabledBtn] = useState(false); |
|
|
|
const [disabledBtn, setDisabledBtn] = useState(false); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
setPhone(props.data.phone); |
|
|
|
setEmail(props.data.email); |
|
|
|
setEmail(props.data.email); |
|
|
|
setPassword(props.data.password); |
|
|
|
setPassword(props.data.password); |
|
|
|
setRePassword(props.data.rePassword); |
|
|
|
setRePassword(props.data.rePassword); |
|
|
|
}, [props.data]); |
|
|
|
}, [props.data]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const verifyEmail = async () => { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const res = await apiCaller("/api_register/check_email", "POST", { email }) |
|
|
|
|
|
|
|
if (res?.status) { |
|
|
|
|
|
|
|
setCacheEmailPass([...cacheEmailPass, email]); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
setEmailError(res?.msg); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
setCacheEmailFail([...cacheEmailFail, email]); |
|
|
|
|
|
|
|
if (e === "Email đã tồn tại trên hệ thống.") { |
|
|
|
|
|
|
|
setEmailError( |
|
|
|
|
|
|
|
"Email đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
setEmailError(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const verifyPhone = async () => { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const res = await apiCaller("/api_register/check_phone", "POST", { phone }) |
|
|
|
|
|
|
|
if (res?.status) { |
|
|
|
|
|
|
|
// setCachePhonePass([...cachePhonePass, phone]);
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
setPhoneError(res?.msg); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
setCachePhoneFail([...cachePhoneFail, phone]); |
|
|
|
|
|
|
|
if (e === "Số điện thoại đã tồn tại trên hệ thống.") { |
|
|
|
|
|
|
|
setPhoneError( |
|
|
|
|
|
|
|
"Số điện thoại đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
setPhoneError(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function handleSubmit(e) { |
|
|
|
async function handleSubmit(e) { |
|
|
|
// setSubmitted(true);
|
|
|
|
|
|
|
|
// if (validateParam()) {
|
|
|
|
|
|
|
|
// dispatch(userActions.udpateInformation(inputs));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
e.preventDefault(); |
|
|
|
e.preventDefault(); |
|
|
|
if (!!email && password.length >= 6 && password === rePassword) { |
|
|
|
let isFormValid = true; |
|
|
|
if (cacheEmailPass.includes(email)) { |
|
|
|
if(validateParam() ) { |
|
|
|
nextStep(); |
|
|
|
if (cacheEmailFail.includes(email)) { |
|
|
|
return; |
|
|
|
|
|
|
|
} else if (cacheEmailFail.includes(email)) { |
|
|
|
|
|
|
|
setDisabledBtn(true); |
|
|
|
|
|
|
|
setEmailError( |
|
|
|
setEmailError( |
|
|
|
"Email đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
"Email đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
isFormValid = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (cachePhoneFail.includes(phone)) { |
|
|
|
|
|
|
|
setPhoneError( |
|
|
|
|
|
|
|
"Số điện thoại đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
isFormValid = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!!phone.length && !cachePhoneFail.includes(phone)) { |
|
|
|
|
|
|
|
const isPhoneValid = await verifyPhone(); |
|
|
|
|
|
|
|
if(!isPhoneValid) isFormValid = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!!email.length && !cacheEmailFail.includes(email)) { |
|
|
|
|
|
|
|
const isEmailValid = await verifyEmail(); |
|
|
|
|
|
|
|
if(!isEmailValid) isFormValid = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (isFormValid) { |
|
|
|
|
|
|
|
nextStep(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} else { |
|
|
|
|
|
|
|
await apiCaller("/api_register/check_email", "POST", { |
|
|
|
|
|
|
|
email, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then((res) => { |
|
|
|
|
|
|
|
if (res.status) { |
|
|
|
|
|
|
|
setCacheEmailPass([...cacheEmailPass, email]); |
|
|
|
|
|
|
|
nextStep(); |
|
|
|
|
|
|
|
setEmailError(""); |
|
|
|
|
|
|
|
setPwError(""); |
|
|
|
|
|
|
|
setRePwError(""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.catch((e) => { |
|
|
|
|
|
|
|
setCacheEmailFail([...cacheEmailFail, email]); |
|
|
|
|
|
|
|
if (e === "Email đã tồn tại trên hệ thống.") { |
|
|
|
|
|
|
|
setDisabledBtn(true); |
|
|
|
|
|
|
|
setEmailError( |
|
|
|
|
|
|
|
"Email đã tồn tại trên hệ thống. Vui lòng quay lại đăng nhập!" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
setEmailError(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
checkPhone() |
|
|
|
|
|
|
|
checkEmail(); |
|
|
|
|
|
|
|
checkPw(); |
|
|
|
|
|
|
|
checkRePw(); |
|
|
|
} |
|
|
|
} |
|
|
|
// checkEmail();
|
|
|
|
|
|
|
|
checkPw(); |
|
|
|
|
|
|
|
checkRePw(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const nextStep = () => { |
|
|
|
const nextStep = () => { |
|
|
|
props.setData({ |
|
|
|
props.setData({ |
|
|
|
|
|
|
|
phone, |
|
|
|
email, |
|
|
|
email, |
|
|
|
password, |
|
|
|
password, |
|
|
|
rePassword, |
|
|
|
rePassword, |
|
|
@ -81,8 +118,17 @@ const CreateAccount = (props) => { |
|
|
|
props.setStep(stepAuthConstants.STEP_ACCOUNT_TYPE); |
|
|
|
props.setStep(stepAuthConstants.STEP_ACCOUNT_TYPE); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkPhone = () => { |
|
|
|
|
|
|
|
if (phone.length !== 10 && phone.length !== 11) { |
|
|
|
|
|
|
|
setPhoneError("Số điện thoại hợp lệ có từ 10-11"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (phone && phone.length && phone[0] !== "0") { |
|
|
|
|
|
|
|
setPhoneError("Định dạng số điện thoại không đúng"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const checkEmail = () => { |
|
|
|
const checkEmail = () => { |
|
|
|
if (!isEmail(email)) { |
|
|
|
if (!!email.length && !isEmail(email)) { |
|
|
|
setEmailError("Định dạng email không đúng"); |
|
|
|
setEmailError("Định dạng email không đúng"); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -91,6 +137,9 @@ const CreateAccount = (props) => { |
|
|
|
if (password.length < 6 || password.trim() === "") { |
|
|
|
if (password.length < 6 || password.trim() === "") { |
|
|
|
setPwError("Mật khẩu cần tối thiểu 6 kí tự và không gồm toàn dấu cách"); |
|
|
|
setPwError("Mật khẩu cần tối thiểu 6 kí tự và không gồm toàn dấu cách"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (password.length > 30) { |
|
|
|
|
|
|
|
setPwError("Mật khẩu chứa tối đa 30 kí tự"); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const checkRePw = () => { |
|
|
|
const checkRePw = () => { |
|
|
@ -99,8 +148,20 @@ const CreateAccount = (props) => { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const changePhone = (value) => { |
|
|
|
|
|
|
|
const regex = /^[0-9\b]+$/; |
|
|
|
|
|
|
|
if(!regex.test(value) && value !== '') { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (phoneError) { |
|
|
|
|
|
|
|
if (isPhone(value)) { |
|
|
|
|
|
|
|
setPhoneError(""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
setPhone(value.trim()); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const changeEmail = (value) => { |
|
|
|
const changeEmail = (value) => { |
|
|
|
setDisabledBtn(false); |
|
|
|
|
|
|
|
if (emailError) { |
|
|
|
if (emailError) { |
|
|
|
if (!!value) { |
|
|
|
if (!!value) { |
|
|
|
setEmailError(""); |
|
|
|
setEmailError(""); |
|
|
@ -110,8 +171,9 @@ const CreateAccount = (props) => { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const changePassword = (value) => { |
|
|
|
const changePassword = (value) => { |
|
|
|
|
|
|
|
if(value?.length > 30) return; |
|
|
|
if (pwError) { |
|
|
|
if (pwError) { |
|
|
|
if (value.length >= 6) { |
|
|
|
if (value.length >= 6 && value.length <= 30) { |
|
|
|
setPwError(""); |
|
|
|
setPwError(""); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -133,9 +195,15 @@ const CreateAccount = (props) => { |
|
|
|
const onBlurField = (type) => { |
|
|
|
const onBlurField = (type) => { |
|
|
|
switch (type) { |
|
|
|
switch (type) { |
|
|
|
case "email": { |
|
|
|
case "email": { |
|
|
|
// if (email && !emailError) {
|
|
|
|
if (email && !emailError) { |
|
|
|
// checkEmail();
|
|
|
|
checkEmail(); |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case "phone": { |
|
|
|
|
|
|
|
if (phone && !phoneError) { |
|
|
|
|
|
|
|
checkPhone(); |
|
|
|
|
|
|
|
} |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
case "password": { |
|
|
|
case "password": { |
|
|
@ -159,15 +227,14 @@ const CreateAccount = (props) => { |
|
|
|
|
|
|
|
|
|
|
|
function validateParam() { |
|
|
|
function validateParam() { |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!!email && |
|
|
|
(isEmail(email) || !!phone.length) && |
|
|
|
password.length >= 6 && |
|
|
|
password.length >= 6 && |
|
|
|
rePassword === password && |
|
|
|
rePassword === password && |
|
|
|
password.trim() !== "" && |
|
|
|
password.trim() !== "" && |
|
|
|
|
|
|
|
!emailError && |
|
|
|
|
|
|
|
!phoneError && |
|
|
|
!disabledBtn |
|
|
|
!disabledBtn |
|
|
|
) { |
|
|
|
) { |
|
|
|
if (emailError) { |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -234,6 +301,21 @@ const CreateAccount = (props) => { |
|
|
|
}} |
|
|
|
}} |
|
|
|
autoFocus = {true} |
|
|
|
autoFocus = {true} |
|
|
|
></InputText> |
|
|
|
></InputText> |
|
|
|
|
|
|
|
<InputText |
|
|
|
|
|
|
|
value={phone} |
|
|
|
|
|
|
|
setValue={changePhone} |
|
|
|
|
|
|
|
type="text" |
|
|
|
|
|
|
|
name="phone" |
|
|
|
|
|
|
|
placeholder="Nhập số điện thoại xác thực tài khoản" |
|
|
|
|
|
|
|
required={true} |
|
|
|
|
|
|
|
renderLabelIcon={renderPhoneIcon} |
|
|
|
|
|
|
|
errorText={phoneError} |
|
|
|
|
|
|
|
setErrorText={setPhoneError} |
|
|
|
|
|
|
|
onBlur={() => { |
|
|
|
|
|
|
|
onBlurField("phone"); |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
autoFocus = {true} |
|
|
|
|
|
|
|
></InputText> |
|
|
|
<InputText |
|
|
|
<InputText |
|
|
|
value={password} |
|
|
|
value={password} |
|
|
|
setValue={changePassword} |
|
|
|
setValue={changePassword} |
|
|
|