Hack-a-thon #3: Building an Async Bookstore
const books = [ { "id": 1, "name": "Lasagna: A Retrospective", "author": "Garfield" "picture_url": "http://graphics8.nytimes.com/images/2015/10/15/ dining/15RECIPE20DIN/15RECIPE20DIN-articleLarge.jpg", "price": 24, "selling_points": [ "Lasagna is delicious.", "The essential guide to Italian casseroles of all types.", "Real G's move silent, like Lasagna. -Lil Wayne" ] }, // plus a few more Objects ];function render(state) { root.innerHTML = ` ${Navigation()} ${Header()} ${Content(state)} ${Form()} ${Footer()} `; } render(books);import Book from "./Book"; export default function Content(books) { return ` <div id="content"> ${Book(books[0])} ${Book(books[1])} ${Book(books[2])} </div> `; }import Book from "./Book"; function mapBooks(books) { return books.map(book => Book(book)).join(""); } export default function Content(books) { return ` <div id="content"> ${mapBooks(books)} </div> `; }function mapSellingPoints(sellingPoints) { return sellingPoints.map(point => `<li>${point}</li>`).join(""); } export default function Book(book) { return ` <div> <h1>${book.name}</h1> <h2>${book.author}</h2> <h3>${book.price}</h3> <ol> ${mapSellingPoints(book.sellingPoints)} </ol> <img src="${book.pictureUrl}"> </div> `; }// other form submit event stuff here sellingPoints: data[4].value.split(","); // even more form submit stuff heredocument .querySelector('form') .addEventListener( 'submit', (event) => { const newProduct = event .target .elements .reduce( (acc, product) => { if(product.name === 'sellingPoints'){ acc.sellingPoints = product.value.split(','); } else { acc[product.name] = product.value; } return acc; } {}, ) } books.push(newProduct); render(books); );
Last updated