window.lombardo = { done: 0, }; const round = in_price => { let price = String(in_price); let end = price.substr(-3); let fifties = price.substr(-2); let add = 0; if (fifties !== '00') { let g = parseInt(end.substr(-2)); add = 100 - g; } return String(parseInt(in_price) + add); }; const formatPrice = p => { let price = p.split('').reverse(); let formatted = []; let threes = 0; for (let i = 0; i < price.length; i++) { ++threes; if (threes === 4) { formatted.push(','); threes = 1; } formatted.push(price[i]); } return formatted.reverse().join(''); }; const formatAskingPrice = p => { p *= 1.03; p = String(Math.round(parseFloat(p,10) / 5000) * 5000); let price = p.split('').reverse(); let formatted = []; let threes = 0; for (let i = 0; i < price.length; i++) { ++threes; if (threes === 4) { formatted.push(','); threes = 1; } formatted.push(price[i]); } return formatted.reverse().join(''); }; (function () { const showLoading = () => { const div = document.getElementById('lombardo-loading'); div.classList.remove('lombardo-hidden'); const backdrop = document.getElementById('lombardo-backdrop'); backdrop.classList.remove('lombardo-hidden'); const doc = document.getElementById('lombardo-document'); doc.remove(); }; window.onload = () => { console.debug(window.lombardo); const $ = a => document.getElementById(a); let u = new URL(window.location.href); let listingId = ''; let match = []; if(u.pathname.indexOf('/test-listing') === 0){ match = u.pathname.match(/^\/test\-listing\/([0-9-A-Za-z]+)/); }else{ match = u.pathname.match(/^\/([0-9-A-Z-a-z]+)/); } const addy = $('propertyAddress'); const recv = $('propertyReceive'); const price = $('propertyPrice'); const btn = $('agree-button'); listingId = match[1]; let listing = {}; console.debug({ listingId }); console.log('-- initialized --'); fetch(`/api/v1/property/${listingId}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, cache: 'no-cache', }).then(async data => { let json = await data.json(); listing = json.data[0]; //addy.value = listing.address.includes(', NY') ? listing.address.split(', NY')[0] : listing.address; // const matchAddress = listing.address.match(/([^,]+),\s*([\s\S]+)/); // if (matchAddress) { // const streetAddress = matchAddress[1].trim(); // const zipCodeMatch = matchAddress[2].match(/\b\d+\b/); // const zipCode = zipCodeMatch ? zipCodeMatch[0] : null; // console.log("Street Address:", streetAddress); // console.log("Zip Code:", zipCode); // addy.value = streetAddress + ", " + zipCode; // } else { // addy.value = listing.address; // } var address = listing.address; var index = address.indexOf(" NY"); var addressToUse = listing.address if (index !== -1) { addressToUse = address.substring(0, index); } addressToUse = String(addressToUse).replace(/\,[\s]*$/,''); addy.value = addressToUse; recv.value = formatPrice(String(listing.unformattedPrice)); price.value = formatAskingPrice( listing.unformattedPrice ); window.lombardo.done = true; }).catch(() => { window.lombardo.done = true; }); btn.onclick = () => { console.debug('click'); showLoading(); window.setTimeout(() => { fetch('/api/v1/agree', { method: 'POST', headers: { 'Content-Type': 'application/json', }, cache: 'no-cache', body: JSON.stringify({ listingID: listingId, }), }).finally(() => { window.location.href = '/thank-you'; }); }, 1500); }; }; })();