Merge pull request 'TIST-39: added ajax to daily meals select' (#33) from TIST-39 into develop

Reviewed-on: #33
Reviewed-by: steve_dekart <stevedekart2020@gmail.com>
This commit is contained in:
greendavid004 2025-07-05 20:52:28 +02:00
commit f336917b62
2 changed files with 41 additions and 12 deletions

View File

@ -12,6 +12,7 @@ the_header(
?> ?>
<div id="recipe-id" hidden><?= $context['recipe']->get_id() ?></div> <div id="recipe-id" hidden><?= $context['recipe']->get_id() ?></div>
<div id="in-usermenu" hidden><?= $context['recipe']->in_usermenu; ?></div>
<div class="container"> <div class="container">
<div class="single-recipe"> <div class="single-recipe">

View File

@ -15,13 +15,41 @@ document.addEventListener('click', (e) => {
const options = document.querySelectorAll('.dropdown-item'); const options = document.querySelectorAll('.dropdown-item');
const inUsermenu = document.getElementById('in-usermenu').textContent;
if (inUsermenu) {
options.forEach(option => {
if (option.getAttribute('data-value') === inUsermenu && option.getAttribute('data-value') !== 'remove') {
option.classList.add('dropdown-selected');
toggleBtn.textContent = option.textContent;
}
});
}
options.forEach(option => { options.forEach(option => {
option.addEventListener('click', () => { option.addEventListener('click', async (e) => {
const selectedValue = option.getAttribute('data-value'); const selectedValue = option.getAttribute('data-value');
options.forEach(opt => opt.classList.remove('dropdown-selected')); options.forEach(opt => opt.classList.remove('dropdown-selected'));
const formData = new FormData();
formData.append('action', 'usermenu');
formData.append('recipe_id', recipeId);
formData.append('dayofweek', selectedValue);
const response = await fetch('/ajax', {
method: 'POST',
body: formData
});
const json = await response.json();
if (!response.ok) {
const message = json.error;
showToastify(message, 'error');
return;
}
if (selectedValue === 'remove') { if (selectedValue === 'remove') {
toggleBtn.textContent = 'Add to list'; toggleBtn.textContent = 'Add to list';
} else { } else {
@ -31,7 +59,7 @@ options.forEach(option => {
dropdown.classList.add('hidden'); dropdown.classList.add('hidden');
alert(`You selected: ${selectedValue}`); showToastify(json.success, 'success');
}); });
}); });
@ -42,20 +70,20 @@ const qrPopup = document.querySelector(".qr-popup");
const overlay = document.getElementById('overlay'); const overlay = document.getElementById('overlay');
qrBtn.addEventListener("click", () => { qrBtn.addEventListener("click", () => {
qrPopup.classList.toggle("hidden") qrPopup.classList.toggle("hidden")
overlay.classList.toggle("hidden") overlay.classList.toggle("hidden")
qrContainer.innerHTML = ""; qrContainer.innerHTML = "";
new QRCode(qrContainer, { new QRCode(qrContainer, {
text: window.location.href, text: window.location.href,
width: 200, width: 200,
height: 200 height: 200
}); });
setTimeout(() => { setTimeout(() => {
const qrImg = qrContainer.querySelector("img"); const qrImg = qrContainer.querySelector("img");
if (qrImg) { if (qrImg) {
@ -64,8 +92,8 @@ qrBtn.addEventListener("click", () => {
}, 500); }, 500);
}); });
document.addEventListener('click', (e) =>{ document.addEventListener('click', (e) => {
if(!qrPopup.contains(e.target) && !qrBtn.contains(e.target)){ if (!qrPopup.contains(e.target) && !qrBtn.contains(e.target)) {
qrPopup.classList.add("hidden"); qrPopup.classList.add("hidden");
overlay.classList.add("hidden"); overlay.classList.add("hidden");
} }
@ -85,7 +113,7 @@ favoriteBtn.addEventListener('click', async (e) => {
formData.append('type', type); formData.append('type', type);
favoriteBtn.disabled = true; favoriteBtn.disabled = true;
const response = await fetch('/ajax', { const response = await fetch('/ajax', {
method: 'POST', method: 'POST',
body: formData body: formData
}); });
@ -93,20 +121,20 @@ favoriteBtn.addEventListener('click', async (e) => {
const json = await response.json(); const json = await response.json();
if(!response.ok) { if (!response.ok) {
const message = json.error; const message = json.error;
showToastify(message, 'error'); showToastify(message, 'error');
return; return;
} }
favoriteBtn.classList.toggle('active'); favoriteBtn.classList.toggle('active');
if(type == 'add') { if (type == 'add') {
favoriteIcon.classList.remove('fa-regular'); favoriteIcon.classList.remove('fa-regular');
favoriteIcon.classList.add('fa-solid'); favoriteIcon.classList.add('fa-solid');
} else { } else {
favoriteIcon.classList.add('fa-regular'); favoriteIcon.classList.add('fa-regular');
favoriteIcon.classList.remove('fa-solid'); favoriteIcon.classList.remove('fa-solid');
} }
showToastify(json.success, 'success'); showToastify(json.success, 'success');
}); });