$(function(){
var wishlistProducts = null;
// function to determine if recipe is favorised
$(this).find(".wish-product").each(function(){
if (wishlistProducts){
var productId = "";
var self = this;
$(self).find("input[type=hidden]").each(function (){
productId = $(this).val();
});
wishlistProducts.forEach(function (id){
if (productId == id){
$(self).find(".fa-heart").each(function (){
$(this).removeClass('far');
$(this).addClass('fas');
});
}
});
}
});
$(".wish-product").click(function(){
var xmlhttp = new XMLHttpRequest();
var productId = "";
var self = this;
$(self).find("input[type=hidden]").each(function (){
productId = $(this).val();
});
var query = "?toggle="+ productId;
xmlhttp.open("POST", "/product/product.php" + query, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var json = JSON.parse(this.responseText);
if ( json.response.info == "Not authenticated" ){
$('#login-overlay-header').text('Bitte melden Sie sich an, damit das ausgewählte Produkt Ihrem persönlichen Wunschzettel hinzugefügt werden kann.');
$('#logindialog').modal('show');
}else{
$(self).find(".fa-heart").each(function (){
$(this).toggleClass('fas');
$(this).toggleClass('far');
});
}
}
}
xmlhttp.send();
});
$(".fav-wish-product").click(function(){
var self = this;
var xmlhttp = new XMLHttpRequest();
var productId = "";
$(self).find("input[type=hidden]").each(function (){
productId = $(this).val();
});
var query = "?remove="+ productId;
if (confirm("Produkt wirklich aus dem Wunschzettel entfernen?")) {
xmlhttp.open("POST", "/product/product.php" + query, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var json = JSON.parse(this.responseText);
if ( json.response.result == "OK" )
$(".favorited-product-" + productId).remove();
var cntProducts = $('[class*="favorited-product"]').length;
if ( cntProducts < 1)
$( ".fav-product-null" ).removeClass("d-none");
}
}
xmlhttp.send();
}
});
});