Created
August 8, 2019 04:22
-
-
Save boblitex/83c36b854748f5c136f579e0f84fd49f to your computer and use it in GitHub Desktop.
says displayTotalCart from the fetch is undefined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Mini App</title> | |
<style> | |
body { | |
margin: 0; | |
padding: 1em; | |
background-color: white; | |
} | |
[data-cart-info] span { | |
display: inline-block; | |
vertical-align: middle; | |
} | |
span.material-icons { | |
font-size: 150px; | |
} | |
[data-credit-card] { | |
width: 435px; | |
min-height: 240px; | |
border-radius: 10px; | |
background-color: #5d6874 | |
} | |
img[data-card-type] { | |
display: block; | |
height: 60px; | |
width: 120px; | |
} | |
[data-cc-digits] { | |
margin-top: 2em; | |
display: flex; | |
} | |
[data-cc-digits] input { | |
color: white; | |
font-size: 2em; | |
line-height: 2em; | |
background: none; | |
border: none; | |
margin-right: 0.5em; | |
width: 100% | |
} | |
[data-cc-info] { | |
margin-top: 1em; | |
} | |
[data-cc-info] input { | |
color: white; | |
font-size: 1.2em; | |
border: none; | |
background: none; | |
} | |
[data-cc-info] :nth-child(2) { | |
padding-right: 10px; | |
float: right; | |
} | |
button[data-pay-btn] { | |
position: fixed; | |
width: 90%; | |
border: 1px solid; | |
bottom: 20px; | |
} | |
[data-cart-info], | |
[data-credit-card] { | |
transform: scale(0.78); | |
margin-left: -3.4em; | |
} | |
[data-cc-info] input:focus, | |
[data-cc-digits] input:focus { | |
outline: none; | |
} | |
.mdc-card__primary-action, | |
.mdc-card__primary-action:hover { | |
cursor: auto; | |
padding: 20px; | |
min-height: inherit; | |
} | |
[data-credit-card] [data-card-type] { | |
transition: width 1.5s; | |
margin-left: calc(100% - 130px); | |
} | |
[data-credit-card].is-visa { | |
background: linear-gradient(135deg, #622774 0%, #c53364 100%); | |
} | |
[data-credit-card].is-mastercard { | |
background: linear-gradient(135deg, #65799b 0%, #5e2563 100%); | |
} | |
.is-visa [data-card-type], | |
.is-mastercard [data-card-type] { | |
width: auto; | |
} | |
input.is-invalid, | |
.is-invalid input { | |
text-decoration: line-through; | |
} | |
::placeholder { | |
color: #fff; | |
} | |
/* Add Your CSS From Here */ | |
</style> | |
</head> | |
<body> | |
<!-- your HTML goes here --> | |
<div data-cart-info=""> | |
<h1 class="mdc-typography--headline4"> | |
<span class="material-icons"> | |
shopping_cart | |
</span> | |
<span data-bill=""> | |
</span> | |
</h1> | |
</div> | |
<div data-credit-card="" class="mdc-card mdc-card--outlined"> | |
<div class="mdc-card__primary-action"> | |
<img data-card-type="" src="placehold.it/120x60.png?text-card"> | |
<div data-cc-digits=""> | |
<input type="text" placeholder="----" size="4"> | |
<input type="text" placeholder="----" size="4"> | |
<input type="text" placeholder="----" size="4"> | |
<input type="text" placeholder="----" size="4"> | |
</div> | |
<div data-cc-info=""> | |
<input type="text" placeholder="Name Surname" size="20"> | |
<input type="text" placeholder="MM/YY" size="6"> | |
</div> | |
</div> | |
</div> | |
<button data-pay-btn="" class="mdc-button">Pay Now </button> | |
<script> | |
const supportedCards = { | |
visa, mastercard | |
}; | |
const countries = [ | |
{ | |
code: "US", | |
currency: "USD", | |
currencyName: '', | |
country: 'United States' | |
}, | |
{ | |
code: "NG", | |
currency: "NGN", | |
currencyName: '', | |
country: 'Nigeria' | |
}, | |
{ | |
code: 'KE', | |
currency: 'KES', | |
currencyName: '', | |
country: 'Kenya' | |
}, | |
{ | |
code: 'UG', | |
currency: 'UGX', | |
currencyName: '', | |
country: 'Uganda' | |
}, | |
{ | |
code: 'RW', | |
currency: 'RWF', | |
currencyName: '', | |
country: 'Rwanda' | |
}, | |
{ | |
code: 'TZ', | |
currency: 'TZS', | |
currencyName: '', | |
country: 'Tanzania' | |
}, | |
{ | |
code: 'ZA', | |
currency: 'ZAR', | |
currencyName: '', | |
country: 'South Africa' | |
}, | |
{ | |
code: 'CM', | |
currency: 'XAF', | |
currencyName: '', | |
country: 'Cameroon' | |
}, | |
{ | |
code: 'GH', | |
currency: 'GHS', | |
currencyName: '', | |
country: 'Ghana' | |
} | |
]; | |
const billHype = () => { | |
const billDisplay = document.querySelector('.mdc-typography--headline4'); | |
if (!billDisplay) return; | |
const appState = {}; | |
const formatAsMoney = (amount, buyerCountry)=>{ | |
let apicountry = countries.find(country => country.country === buyerCountry); | |
if (apicountry){ | |
return amount.toLocaleString(`en-${apicountry.code}`, { | |
style: "currency", | |
currency: apicountry.currency | |
}); | |
} | |
else{ | |
return amount.toLocaleString(`en-US`,{ | |
style: "currency", | |
currency: countries[0].currency | |
}); | |
} | |
} | |
const flagIfInvalid = (field, isValid)=>{ | |
if(isValid){ | |
field.classList.remove('is-invalid'); | |
}else{ | |
field.classList.add('is-invalid'); | |
} | |
} | |
const expiryDateFormatIsValid =(field)=>{ | |
// const field = document.querySelector("[data-cc-info] :nth-child(2)") | |
const fieldDate = field.value; | |
let regex = /^(0[1-9]|1[0-2])\/?(\d{2})$/g | |
if (regex.test(fieldDate)){ | |
const date = new Date(); | |
let fieldDateArray = fieldDate.split("/"); | |
let [month, year] = fieldDateArray; | |
year = `20${year}`; | |
month = month-1; | |
if((Number(year) >= date.getFullYear()) && (Number(month) >= date.getMonth())){ | |
console.log(month, year); | |
return true; | |
} | |
else { | |
console.log("samurai card"); | |
return false; | |
} | |
} | |
else{ | |
console.log("put some respek on the format mm/yy") | |
return false; | |
} | |
} | |
const detectCardType = (first4Digits)=>{ | |
} | |
const validateCardExpiryDate =()=>{ | |
if(expiryDateFormatIsValid(field)){ | |
flagIfInvalid(field, true); | |
return true; | |
} | |
else{ | |
flagIfInvalid(field, false) | |
return false; | |
} | |
} | |
const validateCardHolderName = ()=>{ | |
const nameField = document.querySelector('[data-cc-info :nth-child(1)]'); | |
const fullName = nameField.value; | |
let regexn = /^([a-z]{3,})\s([a-z]{3,})$/i | |
if(regexn.test(fullName)){ | |
flagIfInvalid(nameField, true); | |
return true; | |
} | |
else{ | |
flagIfInvalid(nameField, false); | |
return false; | |
} | |
} | |
const validateCardNumber = ()=>{ | |
} | |
const validatePayment = ()=>{ | |
validateCardNumber(); | |
validateCardHolderName(); | |
validateCardExpiryDate(); | |
} | |
const smartInput = (event, fieldIndex)=>{ | |
} | |
const uiCanInteract =()=>{ | |
const payButton = document.querySelector("[data-pay-btn]"); | |
payButton.addEventListener("click", validatePayment); | |
document.querySelector("[data-cc-digits :nth-child(1)]").focus(); | |
billHype(); | |
}; | |
const displayCartTotal = ({results})=>{ | |
const [data] = results; | |
const {itemsInCart, buyerCountry} = data; | |
appState.items = itemsInCart; | |
appState.country = buyerCountry; | |
appState.bill = itemsInCart.reduce((a,b)=> a + (b.price * b.qty),0); | |
appState.billFormatted = formatAsMoney(appState.bill, appState.country); | |
document.querySelector('span[data-bill]').textContent = appState.billFormatted; | |
appState.cardDigits =[]; | |
uiCanInteract(); | |
}; | |
billDisplay.addEventListener('click', () => { | |
const billSpan = document.querySelector("[data-bill]"); | |
if (billSpan && | |
appState.bill && | |
appState.billFormatted && | |
appState.billFormatted === billSpan.textContent) { | |
window.speechSynthesis.speak( | |
new SpeechSynthesisUtterance(appState.billFormatted) | |
); | |
} | |
}); | |
}; | |
const fetchBill = () => { | |
const apiHost = 'https://randomapi.com/api'; | |
const apiKey = '006b08a801d82d0c9824dcfdfdfa3b3c'; | |
const apiEndpoint = `${apiHost}/${apiKey}`; | |
fetch(apiEndpoint).then(response => response.json()) | |
.then(data => displayCartTotal(data)) | |
.catch((error) => console.log(error)); | |
}; | |
const startApp = () => { | |
fetchBill(); | |
}; | |
startApp(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment