Last active
October 2, 2022 07:42
-
-
Save edoves/e5dc27bf18209f8941f37d5ff84a7997 to your computer and use it in GitHub Desktop.
map function in pure js rendering HTML
This file contains 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
import Rating from '../components/Rating'; | |
import { getProducts } from '../api'; | |
import { parseRequestUrl } from '../utils'; | |
const HomeScreen = { | |
render: async () => { | |
const { value } = parseRequestUrl(); | |
const products = await getProducts({ searchKeyword: value }); | |
if (products.error) { | |
return `<div class="error">${products.error}</div>`; | |
} | |
return ` | |
<ul class="products"> | |
${products | |
.map( | |
(product) => ` | |
<li> | |
<div class="product"> | |
<a href="/#/product/${product._id}"> | |
<img src="${product.image}" alt="${product.name}" /> | |
</a> | |
<div class="product-name"> | |
<a href="/#/product/1"> | |
${product.name} | |
</a> | |
</div> | |
<div class="product-rating"> | |
${Rating.render({ | |
value: product.rating, | |
text: `${product.numReviews} reviews`, | |
})} | |
</div> | |
<div class="product-brand"> | |
${product.brand} | |
</div> | |
<div class="product-price"> | |
$${product.price} | |
</div> | |
</div> | |
</li> | |
` | |
) | |
.join('\n')} | |
`; | |
}, | |
}; | |
export default HomeScreen; | |
to render | |
const main = document.getElementById('#main'); | |
main.innerHTML = HomeScreen.render() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment