File: /mnt/data/dreamsrent-wp-demo/wp-content/themes/dreamsrent/assets/js/register-otp.js
(function($){
function startCountdown(seconds, onTick, onDone){
var remaining = seconds;
onTick(remaining);
var timer = setInterval(function(){
remaining -= 1;
if (remaining <= 0){
clearInterval(timer);
onDone();
} else {
onTick(remaining);
}
}, 1000);
return function cancel(){ clearInterval(timer); };
}
$(function(){
if (typeof drOtp === 'undefined') return;
var $email = $('#email');
var $emailHidden = $('#email_hidden');
var $sendBtn = $('#send_otp');
var $otpBlock = $('#otp_block');
var $otpInput = $('#otp');
var $verifyBtn = $('#verify_otp');
var $submitBtn = $('#signup_button');
var verified = false;
var cancelCountdown = null;
function setSubmitEnabled(enable){
if (!$submitBtn.length) return;
$submitBtn.prop('disabled', !enable);
}
if (drOtp.otp_switch === '1') {
setSubmitEnabled(false);
}
function notify(msg, isError){
if (window.toastr) {
isError ? toastr.error(msg) : toastr.success(msg);
} else {
alert(msg);
}
}
function formatSeconds(s){
var m = Math.floor(s/60), r = s%60;
return (m>0? (m+':'+('0'+r).slice(-2)) : r+'s');
}
$sendBtn.on('click', function(){
var email = ($email.val()||'').trim();
if (!email) { notify('Please enter your email first.', true); return; }
$sendBtn.prop('disabled', true).text('Sending...');
$.post(drOtp.ajax_url, {
action: 'dreamsrent_send_email_otp',
nonce: drOtp.nonce,
email: email
}).done(function(res){
if (res && res.success){
notify('OTP sent to your email. Expires in '+Math.floor(res.data.expires_in/60)+' min.');
$otpBlock.show();
$email.prop('readonly', true);
$emailHidden.val('');
if (cancelCountdown) cancelCountdown();
var seconds = res.data.expires_in;
cancelCountdown = startCountdown(seconds, function(left){
$sendBtn.text('Resend ('+formatSeconds(left)+')');
}, function(){
$sendBtn.prop('disabled', false).text('Send OTP');
$email.prop('readonly', false);
verified = false;
setSubmitEnabled(false);
});
} else {
var msg = (res && res.data && res.data.message) ? res.data.message : 'Failed to send OTP';
notify(msg, true);
$sendBtn.prop('disabled', false).text('Send OTP');
}
}).fail(function(){
notify('Network error while sending OTP', true);
$sendBtn.prop('disabled', false).text('Send OTP');
});
});
$verifyBtn.on('click', function(){
var email = ($email.val()||'').trim();
var otp = ($otpInput.val()||'').trim();
if (!email || !otp) { notify('Enter email and OTP.', true); return; }
$verifyBtn.prop('disabled', true).text('Verifying...');
$.post(drOtp.ajax_url, {
action: 'dreamsrent_verify_email_otp',
nonce: drOtp.nonce,
email: email,
otp: otp
}).done(function(res){
if (res && res.success){
notify('Email verified successfully.');
verified = true;
$emailHidden.val(email);
setSubmitEnabled(true);
$verifyBtn.text('Verified');
$verifyBtn.prop('disabled', true);
if (cancelCountdown) { cancelCountdown(); cancelCountdown = null; }
$sendBtn.hide();
$otpInput.prop('readonly', true);
} else {
var msg = (res && res.data && res.data.message) ? res.data.message : 'Invalid OTP';
notify(msg, true);
$verifyBtn.prop('disabled', false).text('Verify');
}
}).fail(function(){
notify('Network error while verifying OTP', true);
$verifyBtn.prop('disabled', false).text('Verify');
});
});
// Prevent submit if OTP required and not verified
$('form').on('submit', function(e){
if (drOtp.otp_switch === '1'){
if (!verified || !$emailHidden.val()){
e.preventDefault();
notify('Please verify your email with the OTP before submitting.', true);
return false;
}
}
});
});
})(jQuery);