Skip to content

Commit

Permalink
render list of products
Browse files Browse the repository at this point in the history
  • Loading branch information
trodrigues committed Apr 7, 2016
1 parent 7c12070 commit 3eab194
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
</main>
<script>
window.PC = {
pages: {}
pages: {},
utils: {}
}
</script>
<script src="https://npmcdn.com/[email protected]/browser-dist/contentful.min.js" charset="utf-8"></script>
<script src="https://npmcdn.com/[email protected]/marked.min.js" charset="utf-8"></script>
<script src="./utils.js" charset="utf-8"></script>
<script src="./app.js" charset="utf-8"></script>
<script src="./pages/products.js" charset="utf-8"></script>
<script src="./pages/categories.js" charset="utf-8"></script>
Expand Down
48 changes: 44 additions & 4 deletions pages/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,51 @@
PC.pages.products = function () {
PC.contentfulClient.getEntries({content_type: '2PqfXUJwE8qSYKuM0U6w8M'})
.then(function (entries) {
var products = entries.items.map(function (entry) {
return '<p>'+entry.fields.productName+'</p>'
})
PC.container.innerHTML = products.join('\n')
PC.container.innerHTML = renderProducts(entries.items)
})
}

function renderProducts(products) {
return '' +
'<h1>Products</h1>' +
products.map(renderSingleProduct).join('\n')
}

function renderSingleProduct(product) {
var fields = product.fields
console.log(fields.description)
return '<div>' +
'<div>' +
renderImage(fields.image[0], fields.slug) +
'</div>' +
'<div>' +
'<h2>' +
'<a href="/products/' + fields.slug + '">' +
fields.productName +
'</a>'+
'</h2>' +
' by ' +
'<a href="/brands/' + fields.brand.sys.id + '">' + fields.brand.fields.companyName + '</a>'
'</div>' +
'<p>' +
fields.categories.map(function (category) {
return category.fields.title
}).join(', ') +
'</p>' +
'<p>' + PC.utils.truncate(fields.productDescription, 100) + '</p>' +
'<p>' + fields.price + ' &euro;</p>' +
'<p>Tags: ' + fields.tags.join(', ')+ '</p>' +
'</div>'
}

function renderImage(image, slug) {
if(image && image.fields.file) {
return '<a href="/products/' + slug + '">' +
'<img src="' + image.fields.file.url + '" width="150" height="150" />' +
'</a>'
} else {
return ''
}
}

}());
11 changes: 11 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function () {

PC.utils.truncate = function (str, len) {
if(str && str.length > len) {
return str.substr(0, len) + '&hellip;'
} else {
return str
}
}

}());

0 comments on commit 3eab194

Please sign in to comment.