- Het kiezen van een selectie resulteert in een volledige paginavernieuwing.
- Opent in een nieuw venster.
`;
document.querySelector('body').insertAdjacentHTML('beforeend', snackBarHtml);
window.addEventListener('message', toasterEventListener, false);
function toasterEventListener(event) {
if (!isAppstleLoyaltyMessage(event)) return;
if (typeof event.data === 'string' && event.data.startsWith('appstle_message_for_notification|')) {
var message = event?.data?.split('|')[1];
showAppstleLoyaltyToaster(message);
}
}
let snackBarIndex = 0;
async function showAppstleLoyaltyToaster(msg) {
const purifier = await window.appstleLoyaltyLoadSanitizer();
var snackBar = document.getElementById('snackbar');
snackBar.classList.add('show');
snackBar.insertAdjacentHTML('beforeend', `
`);
const description = document.getElementById(`elem${snackBarIndex}`).querySelector('.description');
if (purifier) description.innerHTML = purifier.sanitize(String(msg), {
ALLOWED_TAGS: ['a', 'b', 'br', 'em', 'i', 'span', 'strong'],
ALLOWED_ATTR: ['href', 'title', 'class'],
});
else description.textContent = String(msg);
const el = snackBarIndex;
setTimeout(function() {
if (document.getElementById(`elem${el}`))
document.getElementById(`elem${el}`).remove();
}, 5000);
snackBarIndex++;
}
const closeSnackBar = (el) => document.getElementById(el).remove();
document.addEventListener('DOMContentLoaded', () => {
});
const executeLoyaltyWidget = () => {
window.appstleLoyaltyLauncher?.mount({
escapeAttribute: escapeLoyaltyAttribute,
navigationUrl: loyaltyNavigationUrl,
sanitizeTitle: title => window.appstleLoyaltyLoadSanitizer().then(p => (p ? sanitizeLoyaltyLabel(title, p, false) : null)),
preparePopupWindow: win => {
win.__st = window.__st;
win.Shopify = { shop: location.host };
win.appstle_public_domain = location.host;
}
});
}
const storeCreditMismatch = _ALConfig?.customerId && _ALConfig?.customerStoreCreditBalance >= 0 && _ALConfig?.customerLoyalty?.storeCreditBalance >= 0 && _ALConfig?.customerLoyalty?.storeCreditBalance !== _ALConfig?.customerStoreCreditBalance;
if (storeCreditMismatch && _ALConfig.shop === 'olineloyalty27.myshopify.com') {
const updateCustomerUrl = `/${_ALConfig?.proxy_path_prefix || 'apps/loyalty'}/cp/api/update-customer`;
try {
fetch(updateCustomerUrl, { method: 'POST', headers: { 'X-Requested-With': 'XMLHttpRequest' } })
.then((response) => {
if(response.ok) {}
})
.catch(() => {});
} catch (err) {}
}
const dedicatedPageURL = new URL(window.location.href);
const decodeUrl = (content) => {
if (typeof content !== 'string') return content;
try {
const htmlDecoded = content?.replace(/\\\//g, '/');
return JSON.parse(`"${htmlDecoded}"`);
} catch (error) {
console.error('Error parsing content:', error);
return content;
}
};
if (dedicatedPageURL?.searchParams?.get('appstle_referral') && _ALConfig?.widget_setting?.widgetType === 'DEDICATED_PAGE') {
const customUrl = decodeUrl(_ALConfig?.widget_setting?.referralCustomRedirectUrl);
if (customUrl) {
const normalizedCustomPath = customUrl.startsWith('/') ? customUrl : `/${customUrl}`;
if (window.location.pathname !== normalizedCustomPath) {
const redirectUrl = new URL(window.location.href);
redirectUrl.pathname = normalizedCustomPath;
window.open(redirectUrl.toString(), '_self');
}
}
else if (dedicatedPageURL?.searchParams?.get('appstle_referral') && _ALConfig?.widget_setting?.widgetType === 'DEDICATED_PAGE' && !window.location.href.includes(_ALConfig?.proxy_path_prefix)) {
window.open(`${window.location.origin}/${_ALConfig?.proxy_path_prefix}${window.location.search}`, '_self');
}
}
const dedicatedPageEventListener = (event) => {
if (event.origin !== window.location.origin || event.source !== window) return;
if (event.data === 'refresh_appstle_loyalty_page') {
initiateAppstleLoyaltyPage();
console.log('REFRESH PAGE');
}
};
window.addEventListener('message', dedicatedPageEventListener, false);
function initiateAppstleLoyaltyPage() {
if (document.querySelector('#appstleLoyaltyPage')) {
const appstleLoyaltyPageElement = document.getElementById('appstleLoyaltyPage');
while (!appstleLoyaltyPageElement.__appstleReactRoot && appstleLoyaltyPageElement.firstChild) {
appstleLoyaltyPageElement.firstChild.remove();
}
document.title = decodeUrl(_ALConfig?.shop_labels?.loyaltyPageMetaTitle) || '';
setTimeout(() => {
let metaDescription = document.querySelector('meta[name="description"]');
if (metaDescription) {
metaDescription.content = decodeUrl(_ALConfig?.shop_labels?.loyaltyPageMetaDescription) || '';
} else {
metaDescription = document.createElement('meta');
metaDescription.name = 'description';
metaDescription.content = decodeUrl(_ALConfig?.shop_labels?.loyaltyPageMetaDescription) || '';
document.head.appendChild(metaDescription);
}
}, 500);
if (appstleLoyaltyPageElement) {
const loadDedicatedPageAsset = (createAsset, attempt, onSettle) => {
const asset = createAsset();
let settled = false;
const settle = () => {
if (settled) return;
settled = true;
if (typeof onSettle === 'function') {
onSettle();
}
};
asset.onload = settle;
asset.onerror = () => {
asset.remove();
if (attempt < 4) {
setTimeout(() => loadDedicatedPageAsset(createAsset, attempt + 1, onSettle), 1000 * Math.pow(2, attempt - 1));
} else {
settle();
}
};
}
const appendDedicatedPageWidgetScript = () => {
loadDedicatedPageAsset(() => {
const s = document.createElement('script');
s.setAttribute('src', _ALConfig?.widgetJsPath);
s.async = false;
document.body.appendChild(s);
return s;
}, 1);
}
const executeDedicatedPageScript = () => {
let widgetScriptAppended = false;
const appendWidgetScriptOnce = () => {
if (widgetScriptAppended) return;
widgetScriptAppended = true;
appendDedicatedPageWidgetScript();
};
try {
const preload = document.createElement('link');
preload.rel = 'preload';
preload.as = 'script';
preload.href = _ALConfig?.widgetJsPath;
document.head.appendChild(preload);
} catch (e) {}
loadDedicatedPageAsset(() => {
const link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = _ALConfig?.widgetCssPath;
document.head.appendChild(link);
return link;
}, 1, appendWidgetScriptOnce);
setTimeout(appendWidgetScriptOnce, 8000);
}
try {
const versionUrl = `${location.origin}/${_ALConfig?.proxy_path_prefix || 'apps/loyalty'}/app-loyalty-version`;
fetch(versionUrl).then(res => res.json()).then(data => {
if (data) {
const widgetJsUrl = new URL(_ALConfig.widgetJsPath);
const widgetCssUrl = new URL(_ALConfig.widgetCssPath);
widgetJsUrl.searchParams.set('v', data);
widgetCssUrl.searchParams.set('v', data);
_ALConfig.widgetJsPath = widgetJsUrl.toString();
_ALConfig.widgetCssPath = widgetCssUrl.toString();
}
}).catch(() => {}).finally(() => {
executeDedicatedPageScript();
});
} catch (err) {
executeDedicatedPageScript();
}
}
} else {
setTimeout(() => {
initiateAppstleLoyaltyPage();
}, 2000);
}
}
const dedicatedPageReferralUrl = new URL(window.location.href)?.searchParams.get('appstle_referral');
if (_ALConfig?.widget_setting?.showWidget === true) {
initiateAppstleLoyaltyPage();
}
(async function() {
const isAvailableVisitShop = _ALConfig?.pointEarnRules?.length > 0 && _ALConfig?.pointEarnRules?.find((rule) => rule?.type === 'VISIT_SHOP' && rule?.status === 'ACTIVE');
if (_ALConfig?.customerId && isAvailableVisitShop) {
if (!sessionStorage.getItem('syncVisitStore') && ((_ALConfig?.customerId && isAvailableVisitShop && isAvailableVisitShop?.id) || (_ALConfig?.referralEnabled && _ALConfig?.referralLoyalty?.referralType === 'VISIT_SHOP'))) {
try {
const response = await fetch(`/${_ALConfig?.proxy_path_prefix || 'apps/loyalty'}/cp/api/customer-visit-store`, { method: 'POST', headers: {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'}, });
if (response?.ok) {
sessionStorage.setItem('syncVisitStore', 'true');
}
} catch (error) {
console.error('Error:', error);
}
}
}
})();
const loyaltyCustomStyles = document.createElement('style');
loyaltyCustomStyles.id = 'appstle-loyalty-custom-styles';
loyaltyCustomStyles.textContent = _ALConfig?.widget_setting?.customCss || '';
document.body.appendChild(loyaltyCustomStyles);