Created
March 14, 2022 17:36
-
-
Save FacundoEG/0210db0cfb1938ea79f700aeb11c9a82 to your computer and use it in GitHub Desktop.
MarketList Facu code
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
const form = document.getElementById("formProds"); | |
const sectionProd = document.getElementById("section__productos"); | |
function main() { | |
form.addEventListener("submit", (e) => { | |
e.preventDefault(); | |
// VALUES | |
const product = e.target.prod.value; | |
const quantity = e.target.cant.value; | |
const cost = e.target.cost.value; | |
if (product.trim() !== "") { | |
// URL PROMISE | |
const MLpromise = fetch( | |
"https://api.mercadolibre.com/sites/MLA/search?q=" + product | |
) | |
.then((promise) => { | |
return promise.json(); | |
}) | |
.then((json) => { | |
const firstResult = json.results[0]; | |
const image = firstResult.thumbnail; | |
return image; | |
}); | |
// PROMISE | |
MLpromise.then((imageURL) => { | |
let totalPrice = cost * quantity; | |
let section__prod = document.createElement("div"); | |
// ELEMENT CREATE | |
section__prod.className = "div__prod"; | |
section__prod.innerHTML = `<img src=${imageURL}><ul><li>Producto: ${product}</li><li>Cantidad: ${quantity}</li><li>Precio Total: $${totalPrice}</li></ul>`; | |
sectionProd.appendChild(section__prod); | |
}); | |
} else { | |
console.log("te falta el nombre "); | |
} | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment