const CACHEROOTLOCATION = 'https://site.truefaithjewelry.com/ytimes/dynamic-content/'; $(document).ready(function () { const query = `close-out=yes`; if (query) { if(!document.getElementById('dynamicContentStyle')) { $("head").append(` `); $("#ytimesFilteredContent .contents").html(""); } $("#ytimesFilteredContent .contents").addClass("loading"); let cacheData = { "cmd": "r", "query": query } getCache(cacheData) .then(function(data) { if(data) { console.log("Cache hit"); useData(data); } else { loadData(query); } }) .catch(function(data) { loadData(query); }); } else { $("#ytimesFilteredContent .contents").removeClass("loading"); console.log("query parameter is not found in the URL."); } }); function loadData(query) { let url = `https://www.ytimes.net/catalog/store-folders/yhst-17707300211888/custom/query.php?query=${query}`; fetch(url) .then((response) => { if (!response.ok) { throw new Error( `Network response was not ok: ${response.status}` ); } return response.json(); // Parse the JSON response }) .then((data) => { // Process the JSON data here if(typeof(data) == 'string') { $("#ytimesFilteredContent").prepend(data); $("#ytimesFilteredContent .contents").removeClass("loading"); } else { writeCache(query, data); useData(data); } }) .catch((error) => { $("#ytimesFilteredContent .contents").removeClass("loading"); console.error("Error:", error); }); } function useData(data) { YTIMES.pageObjects = data; $("#ytimesFilteredContent .contents").removeClass("loading"); $("#ytimesFilteredContent .contents").html(buildPage(data)); //$.getScript("https://turbifycdn.com/ty/cdn/yhst-17707300211888/ytimes-filtered-contents.js?v=53"); $.getScript("https://www.ytimes.net/catalog/store-folders/yhst-17707300211888/custom/ytimes-filtered-contents.js?v=53"); } async function getCache(data) { const url = `${CACHEROOTLOCATION}cache.php`; try { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (response.ok) { return await response.json(); } else { return null; } } catch (error) { return null; } } function writeCache(query, data) { const dataToSend = { "cmd" : "w", "query" : query, "data" : data } const jsonData = JSON.stringify(dataToSend); const url = `${CACHEROOTLOCATION}cache.php`; // Create a fetch request to send the data asynchronously fetch(url, { method: 'POST', body: jsonData, headers: { 'Content-Type': 'application/json', }, }).then(response => { if (response.ok) { console.log('Data sent to cache successfully.'); } else { console.error('Failed to send data to cache.'); } }) .catch(error => { console.error('writeCache Error:', error); }); } function buildPage(data) { let itemsContainer = ''; const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2 }); data.forEach(element => { const CANBEPERSONALIZED = 2; const INIMAGEAREA = 1; const NOTINIMAGEAREA = 0; let flagsTop = getFlags(element, INIMAGEAREA); let flagsBottom = getFlags(element, NOTINIMAGEAREA); let canBePersonalized = element.flags[CANBEPERSONALIZED] != undefined ? element.flags[CANBEPERSONALIZED] : ''; let formattedSalePrice = formatter.format(element['sale'] || element['price']); let formattedRegPrice = element['sale'] && element['price'] ? `
${formatter.format(element['price'])}
` : ''; itemsContainer += `
${element['name']} ${flagsTop}
${element['name']} Item #: ${element['code']}
${formattedSalePrice}
${formattedRegPrice}
${flagsBottom} ${canBePersonalized}
`; }); return itemsContainer; } function getFlags(data, inImageArea) { if(data.flags[inImageArea] != undefined && data.flags[inImageArea] != '') { return (`
${data.flags[inImageArea]}
`); } else { return ''; } }