function notificationProduct($btn, $product_id, $company_id, $action, $where_trigger_btn){ debugger if($product_id && $company_id && $action) { $.ajax({ url: "https://staging-fabrics.piacenza1733.com/assets/ajax/notifications.php", type: "POST", data: { product_id: $product_id, company_id: $company_id, action: $action }, dataType: "json", success: function (data) { if (data.status == "success") { updateBtnNotifications($btn, data, $where_trigger_btn); } else if (data.status == "error") { swal({ text: data.message, type: "error", showConfirmButton: true }); return false; } }, }); } } function updateBtnNotifications(btn, data, $where_trigger_btn){ switch (data.action.toLowerCase().trim()){ case 'add': infoAggiornate(data.message); $(btn).html('Disable availability notification   '); $(btn).data('notification-action', 'delete'); const el = document.querySelector('a[onclick*="notificationProduct"]'); if (el && el.hasAttribute('onclick')) { if(typeof $where_trigger_btn != "undefined" && $where_trigger_btn != '' && $where_trigger_btn.toLowerCase().trim() == "notifications-page") { el.setAttribute('onclick', "event.stopPropagation(); notificationProduct(this, " + data.product_id + ", 0, 'delete', 'notifications-page');"); } else if (typeof $where_trigger_btn != "undefined" && $where_trigger_btn != '' && $where_trigger_btn.toLowerCase().trim() == "quick-order"){ el.setAttribute('onclick', "event.stopPropagation(); notificationProduct(this, " + data.product_id + ", 0, 'delete', 'quick-order');"); } else { el.setAttribute('onclick', "event.stopPropagation(); notificationProduct(this, " + data.product_id + ", 0, 'delete');"); } } break; case 'delete': if(typeof $where_trigger_btn != "undefined" && $where_trigger_btn != '' && $where_trigger_btn.toLowerCase().trim() == "notifications-page"){ if( $("#tr-notifications-prod-"+data.product_id).length > 0 ){ $("#tr-notifications-prod-"+data.product_id).fadeOut(); } } else if(typeof $where_trigger_btn != "undefined" && $where_trigger_btn != '' && $where_trigger_btn.toLowerCase().trim() == "quick-order") { let restock_date = $("#rowQuickOrderProductId"+data.product_id).data('restock-date'); let btnTitleNotification = "Notify me when the item is available"; let btnTitleRestockDate = "Restock date: "; let btnTitleAvvisami = "Notify me"; if(typeof restock_date != "undefined" && restock_date != null && restock_date.trim() != ""){ btnTitleNotification = btnTitleRestockDate + ' ' + restock_date + ' - ' + btnTitleAvvisami; } infoAggiornate(data.message); $(btn).html(btnTitleNotification); $(btn).data('notification-action', 'add'); const el = document.querySelector('a[onclick*="notificationProduct"]'); if (el && el.hasAttribute('onclick')) { el.setAttribute('onclick', "event.stopPropagation(); notificationProduct(this, "+data.product_id+", 0, 'add', 'quick-order');"); } } else { infoAggiornate(data.message); $(btn).html('Notify me when the item is available   '); $(btn).data('notification-action', 'add'); const el = document.querySelector('a[onclick*="notificationProduct"]'); if (el && el.hasAttribute('onclick')) { el.setAttribute('onclick', "event.stopPropagation(); notificationProduct(this, "+data.product_id+", 0, 'add');"); } } break; } } $( document ).ready(function() { $(".js-notification").on('click', function (){ let $btn = $(this); let $product_id = $(this).data('notification-product-id'); let $company_id = $(this).data('notification-company-id'); let $where_trigger_btn = $(this).data('notification-where-trigger'); let $action = $(this).data('notification-action'); notificationProduct($btn, $product_id, $company_id, $action, $where_trigger_btn); }); $(".js-products-trash-notifications").on('click', function (){ let $btn = $(this); let $product_id = $(this).data('notification-product-id'); let $company_id = $(this).data('notification-company-id'); let $where_trigger_btn = $(this).data('notification-where-trigger'); let $action = "delete"; notificationProduct($btn, $product_id, $company_id, $action, $where_trigger_btn); }); });