function load() { if (typeof frontAPI === "undefined" || document.readyState !== 'complete') { setTimeout(load, 250) return } var userId = null; var phone = null; frontAPI.getUser(function (userInfo) { if (userInfo && userInfo.user_id) { userId = userInfo.user_id; if (userInfo.billing_address && userInfo.billing_address.phone) { phone = userInfo.billing_address.phone; } if (!phone && userInfo.shipping_address && userInfo.shipping_address.phone) { phone = userInfo.shipping_address.phone; } var getStatusRequest = new XMLHttpRequest(); getStatusRequest.onreadystatechange = function () { if (parseInt(getStatusRequest.readyState) === 4 && parseInt(getStatusRequest.status) === 200) { if (parseInt(getStatusRequest.responseText) === 0) { if (phone) { appendButtonForLoggedUser(userId, phone); } else { createLink(userId); } } else { appendSavedAlertText(); } } } getStatusRequest.open("GET", "https://shoper.smsapi.com/products/3830/alerts/d0ee49100b0f4aaebf6ff2bf4da6dce558d44ffb/" + userId, true); getStatusRequest.send(); } else { createLink(null); } }, { lang: 'pl_PL' }); } function appendButtonForLoggedUser(userId, phone) { document.head.insertAdjacentHTML("beforeend", ``) var link = document.createElement('li'); link.id = 'availability-notice'; link.innerHTML = 'Powiadom mnie kiedy produkt będzie dostępny'; link.onclick = function(event) { event.stopPropagation; event.preventDefault(); var data = new FormData(); data.append('user_id', userId); data.append('shop', 'd0ee49100b0f4aaebf6ff2bf4da6dce558d44ffb'); data.append('product_id', '3830'); data.append('phone_number', phone); var xhttp = new XMLHttpRequest(); xhttp.open("POST", "https://shoper.smsapi.com/products/availability", true); xhttp.send(data); link.remove(); appendSavedAlertText(); openSuccessModal(); } document.getElementsByClassName('links-q')[0].appendChild(link); } function appendSavedAlertText() { document.head.insertAdjacentHTML("beforeend", ``) var li = document.createElement('li'); li.id = 'availability-notice-info'; li.innerHTML = 'Zostaniesz powiadomiony, gdy produkt będzie dostępny'; document.getElementsByClassName('links-q')[0].appendChild(li); } function openSuccessModal() { var modal = new Shop.Modal({ showMask: true, position: 'center', positionType: 'absolute', offset: 20, header: '', content: '\
\
\
\
\

Dziękujemy za zapisanie do bazy. Jak tylko produkt będzie dostępny, wyślemy Ci wiadomość SMS.

\
\
\
\
' }); modal.createModal(); } function openModal(userId) { var modal = new Shop.Modal({ showMask: true, position: 'center', positionType: 'absolute', offset: 20, header: 'Powiadom mnie o dostępności', content: '\
\
\
\
\ \ \
* - pole wymagane
\
\
\ \
\
\
\
\
' }); modal.createModal(); var form = document.getElementById('product-availability-form'); form.onsubmit = function(event) { event.preventDefault(); var data = new FormData(); if (userId) { data.append('user_id', userId); } data.append('shop', 'd0ee49100b0f4aaebf6ff2bf4da6dce558d44ffb'); data.append('product_id', '3830'); data.append('phone_number', document.getElementById('phone_number').value); var xhttp = new XMLHttpRequest(); xhttp.open("POST", "https://shoper.smsapi.com/products/availability", true); xhttp.send(data); modal.destroyModal(); var link = document.getElementById('availability-notice'); link.remove(); appendSavedAlertText(); openSuccessModal(); } } function createLink(userId) { var link = document.createElement('li'); link.id = 'availability-notice'; link.innerHTML = ' Powiadom mnie kiedy produkt będzie dostępny'; link.onclick = function(event) { event.preventDefault(); openModal(userId) } document.getElementsByClassName('links-q')[0].appendChild(link); } load()