It seems that the main issue in your code is due to nested DOMContentLoaded event listeners and missing DOM elements like the spinner and qrcode for the first modal. Here's the corrected and full code incorporating the necessary changes:
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<!-- ... (rest of your head content) -->
<style>
/* ... (your existing styles) */
</style>
</head>
<body>
<main>
<!-- ... (your main content) -->
</main>
<div id="deleteConfirmationModal" class="custom-modal">
<!-- ... (delete confirmation modal content) -->
</div>
<!-- First Modal (Add Session) -->
<div class="overflow"></div>
<div class="model_poup" data-model="1">
<div class="content">
<div class="head">
<h5 class="title_model">اضافه جلسه</h5>
<div class="close_model">
<i class="fa-solid fa-xmark" onclick="stopInterval();"></i>
</div>
</div>
<form action="" id="whatsapp_phone_form">
<div class="input_item">
<label for="">رقم هاتف مع كود الدوله</label>
<input type="text" id="whatsapp_phone" />
<p style="color: red" id="whatsapp_phone_error" class="d-none"></p>
</div>
<button type="button" class="btn_submit" onclick="addPhoneRequest();">{{ __('Add') }}</button>
</form>
<!-- Added spinner and qrcode for the first modal -->
<span class="loader d-none" id="spinner"></span>
<img class="d-none m-auto" src="" id="qrcode" alt="qrcode">
</div>
</div>
<!-- Second Modal (Connect Session) -->
<div class="overflow"></div>
<div class="model_poup" data-model="2">
<div class="content">
<div class="head">
<h5 class="title_model">{{ __('connect') }}</h5>
<label for="myCheckbox">Set as a default Phone Number!</label>
<input type="checkbox" id="defCheckbox" name="myCheckbox" checked>
<div class="close_model">
<i class="fa-solid fa-xmark" onclick="stopInterval2();"></i>
</div>
</div>
<div class="alert alert-danger m-3 d-none" role="alert" id="error_phone">
{{ __('Choose a phone number to reconnect') }}
</div>
<!-- Spinner and qrcode for the second modal -->
<span class="loader d-none" id="spinner2"></span>
<img class="d-none m-auto" src="" id="qrcode2" alt="qrcode">
</div>
</div>
<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<!-- ... (other script includes) -->
<script>
document.addEventListener('DOMContentLoaded', function() {
// Define addPhoneRequest function
window.addPhoneRequest = function() {
var whatsapp_phone = $('#whatsapp_phone').val();
var tokenAuth = $("#myToken").val();
$.ajax({
url: "{{ route('whatsapp.phone') }}",
method: "post",
data: {
phone: whatsapp_phone,
},
headers: {
'X-CSRF-TOKEN': tokenAuth
},
success: function(data) {
if (data.success == true) {
$("#whatsapp_phone_error").addClass("d-none");
$("#spinner").removeClass("d-none");
$("#whatsapp_phone_form").addClass("d-none");
$("#qrcode_phone").val(data.phone);
runInterval();
} else {
$("#whatsapp_phone_error").removeClass("d-none").html(data.message);
}
}
});
};
// Define qrcodeRequest function
window.qrcodeRequest = function() {
var phone = $("#qrcode_phone").val();
var myToken = $("#myToken").val();
$.ajax({
method: "get",
url: "{{ route('whatsapp.qrcode') }}",
data: {
phone: phone,
},
headers: {
'X-CSRF-TOKEN': myToken
},
success: function(data) {
if (data.status == 'success') {
if (data.qrcode.startsWith('data')) {
$("#qrcode").removeClass("d-none").addClass("d-block").attr("src", data.qrcode);
$("#spinner").addClass("d-none");
}
}
if (data.status == 'connected') {
$.ajax({
method: "put",
url: "{{ route('whatsapp.session.status') }}",
data: {
status: "connected",
phone: phone,
},
headers: {
'X-CSRF-TOKEN': myToken
},
success: function(data) {
location.reload();
}
});
}
}
});
};
window.runInterval = function() {
window.intervalId = setInterval(function() {
qrcodeRequest();
}, 5000);
};
window.stopInterval = function() {
clearInterval(window.intervalId);
};
// Functions for the 'Connect' modal
window.runInterval2 = function() {
var selectedPhone = getSelectedPhone();
if (!selectedPhone) {
$("#error_phone").removeClass("d-none");
return;
}
$("#qrcode_phone").val(selectedPhone);
$("#spinner2").removeClass("d-none");
runIntervalForConnect();
};
function runIntervalForConnect() {
window.intervalId2 = setInterval(function() {
qrcodeRequest2();
}, 5000);
}
window.stopInterval2 = function() {
clearInterval(window.intervalId2);
};
function qrcodeRequest2() {
var phone = $("#qrcode_phone").val();
var myToken = $("#myToken").val();
$.ajax({
method: "get",
url: "{{ route('whatsapp.qrcode') }}",
data: {
phone: phone,
},
headers: {
'X-CSRF-TOKEN': myToken
},
success: function(data) {
if (data.status == 'success') {
if (data.qrcode.startsWith('data')) {
$("#qrcode2").removeClass("d-none").addClass("d-block").attr("src", data.qrcode);
$("#spinner2").addClass("d-none");
}
}
if (data.status == 'connected') {
$.ajax({
method: "put",
url: "{{ route('whatsapp.session.status') }}",
data: {
status: "connected",
phone: phone,
},
headers: {
'X-CSRF-TOKEN': myToken
},
success: function(data) {
location.reload();
}
});
}
}
});
}
function getSelectedPhone() {
var selectedPhonesArray = Array.from(selectedPhones);
if (selectedPhonesArray.length > 0) {
return selectedPhonesArray[0];
}
return null;
}
// ... (rest of your script, including modal handling, checkbox setups, etc.)
$(document).ready(function() {
$("#table").DataTable({
pagingType: "full_numbers",
scrollX: true,
autoWidth: false,
});
});
});
</script>
</body>
</html>
Key Changes Made:
-
Removed Nested DOMContentLoaded: The nested DOMContentLoaded event listener was causing your functions to not be accessible. By removing the nested listener, the functions are properly defined and available when needed.
-
Added Missing DOM Elements:
- In the first modal (Add Session), added the
spinner and qrcode elements with the correct IDs (spinner and qrcode).
- In the second modal (Connect Session), ensured that the
spinner2 and qrcode2 elements are correctly referenced in the JavaScript code.
-
Corrected ID References in JavaScript:
- Ensured that the IDs used in your JavaScript (
spinner, qrcode, spinner2, qrcode2) match the IDs in your HTML elements.
- Used jQuery for consistency and simplicity in the code (e.g.,
$("#spinner").removeClass("d-none");).
-
Defined Missing Functions for the Second Modal:
- Implemented
runInterval2, stopInterval2, and qrcodeRequest2 functions for handling the 'Connect' modal functionality.
- Ensured these functions are correctly interacting with DOM elements and making the necessary AJAX requests.
-
Implemented getSelectedPhone Function:
- Added a
getSelectedPhone function that retrieves the first selected phone number from your selectedPhones set.
- This function is used in
runInterval2 to initiate the QR code request for the selected phone.
-
Adjusted AJAX Requests:
- Corrected the AJAX headers to include the CSRF token using
headers: { 'X-CSRF-TOKEN': myToken }.
- Made sure the data being sent in the AJAX requests is accurate.
-
Consistent Use of Variables:
- Ensured that variables like
myToken and selectedPhone are consistently used and correctly passed to functions.
Additional Notes:
-
Ensure that all your routes (whatsapp.phone, whatsapp.qrcode, whatsapp.session.status) are correctly set up in your Laravel backend to handle these AJAX requests.
-
Make sure that your CSRF token is correctly included in your <meta> tags and that $("#myToken").val() retrieves the correct value.
-
Verify that the functions interacting with the table (e.g., setupDeleteModal, setupCheckboxes) are correctly handling user interactions as per your application's requirements.
By making these adjustments, your 'qrcode request' should now be sent correctly when the user interacts with the modals, and the QR code will display as expected.