css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <header>
	<h1>Emoji Shop</h1>
</header>

<main>
	<!-- The list of emoji products -->
	<ul id="products"></ul>
	
	<aside>
		<div class="sticky-wrapper">
			<h2>Shopping Cart</h2>
			
			<!-- The list of products in our cart -->
			<ul id="cart"></ul>
			
			<!-- Our cart's total -->
			<div id="total">
				Total: 
				<span id="cart-total" class="price">0</span>
			</div>
		</div>
	</aside>
	
</main>



            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Open+Sans')

// Variables
$headingGutter: 0.6em
$gutter: 20px
$primaryColor: #ff7a59
$secondaryColor: #cbd6e2
$tertiaryColor: #7fd0dd
$lightestColor: #fff
$textColorOnMid: #2d3e50
$baseFontSize: 15px
$headingScale: 1.5
$boxShadow: inset 0 0 0 0px #00a4bd

$contextFont: (header: 6, main: 0.6, aside: 0.1)

// Mixins
@mixin card 
	transition: 0.3s box-shadow ease, 0.3s transform ease
	padding: $gutter
	border: 1px solid $secondaryColor
	padding: 2em
	border-radius: 5px
	display: flex
	align-items: center
	justify-content: space-around
	flex-direction: column

	&:hover
		box-shadow: 0 10px 20px -5px $secondaryColor
		transform: translateY(-0.2em)

@mixin cta
	display: inline-block
	border: 1px solid $primaryColor
	background-color: $primaryColor
	border-radius: 3px
	padding: 1em 2.5em
	text-decoration: none
	font-size: 0.9em
	font-weight: 600
	color: $lightestColor
	transition: color .35s ease, background-color .35s ease

@mixin ctaAlternate
	background-color: $lightestColor
	color: $primaryColor

body
	font-size: $baseFontSize
	padding-top: 8em
	text-align: center
	font-family: 'Open Sans', sans-serif

// Typography
@each $tagName, $value in $contextFont
	#{$tagName}
		@for $i from 1 through 6
			h#{$i}
				font-size: $baseFontSize + ($i * $value)			
a
	+cta()

	&:hover
		+ctaAlternate()

p
	margin: 0.5em 0

aside
	a
		+ctaAlternate()

		&:hover
			+cta()

// Price currencies
.price
	&::before
		content: '$'

// Layout
main
	display: flex
	flex-direction: row
	position: relative

header
	position: fixed
	top: 0
	left: 0
	width: 100%
	z-index: 1
	padding: 1em
	border-bottom: 2px solid $secondaryColor
	background-color: $lightestColor

aside
	position: sticky
	top: 0
	left: auto
	right: 0
	flex: 1

.sticky-wrapper
	position: sticky
	top: #{$gutter * 5}

// Products List
#products
	list-style: none
	display: grid
	grid-template-columns: 30% 30% 30%
	grid-template-columns: repeat(3, 1fr)
	grid-gap: $gutter
	padding: 0 $gutter
	width: 70%

	li
		+card()

		.emoji
			font-size: 4em
			margin: 0

// Shopping Cart
#cart
	padding: 0 $gutter
	list-style: none

	li
		+card()
		font-size: 80%
		padding: #{$gutter / 2}
		margin: #{$gutter / 2} 0
            
          
!
            
              /**
 * Represents a product.
 * @constructor
 */
class Product {
 	constructor(options) {
		// ANSWER TO 5a:
		const { id, emoji, description, price, inStock } = options;
		this.id = id;
		this.emoji = emoji;
		this.description = description;
		this.price = price;
		this.inStock = inStock;
		
		/* ANSWER TO 5b:
		 * constructor({ id, emoji, description, price, inStock }) {
		 *     this.id = id;
		 *     this.emoji = emoji;
		 *     this.description = description;
		 *     this.price = price;
		 *     this.inStock = inStock;
		 * }
		*/
	}

	// ANSWER TO 6a:
	getHTML(inCart = false) {
	
		if (this.inStock) {
			// ANSWER TO 7a AND 7b:
 			const { id, emoji, description, price, inStock } = this;
			
			// ANSWER TO 8a AND 8b:
			const html = 
				`<li>
					<h3>${description}</h3>

					${!inCart ? `<p class="emoji">${emoji}</p>` : ''}

					${this.quantity > 1
						? `<p>${this.quantity} x
							<span class="price">
								${this.cartPrice}
							</span>
						   </p>`
						: ''}

					<p>
						<span class="price">${price}</span>
					</p>

					${inCart
						? `<a href="#" onClick="cart.removeProduct(${id});">
							  Remove
						   </a>`
						: `<a href="#" data-id="${id}">
							  Add to Cart
						   </a>` 
					}
			    </li>`;
			return html;
		}
		return '';
	}
}

/**
 * Represents a shopping cart.
 * @constructor
 */
class ShoppingCart {
	// ANSWER TO 9a:
	constructor(items = []) {
    	this.items = items;
  	}
  
	addProduct(item) {  
		// ANSWER TO 10a, 10b, AND 10c:
		const matchingItem = this.items.find(currentElement => item.id === currentElement.id);

		if (typeof matchingItem !== 'undefined') {
			const { price } = matchingItem;
			matchingItem.quantity++;
			matchingItem.cartPrice = matchingItem.quantity * price;
			this.updateCart();
		} else {
			const cartItem = new ProductInCart(item);
			this.items.push(cartItem);
			this.updateCart();
		}
	}
  
	removeProduct(id) {
		const matchingItem = this.items.find(item => id === item.id);
		
		if (typeof matchingItem !== 'undefined') {
			const matchingItemIndex = this.items.indexOf(matchingItem);
			this.items.splice(matchingItemIndex, 1);
			this.updateCart();
		}
	}
  
	updateCart() {
		// called whenever an item is added or removed
		const cart = document.getElementById('cart');
		cart.innerHTML = '';
		
		// ANSWER TO 11a AND 11b:
		this.items.forEach(item => cart.innerHTML += item.getHTML(true));
		this.updateTotal();
	}

	updateTotal() {
		// ANSWER TO 12a AND 12b:
		const total = this.items.reduce((currentSum, item) => currentSum + item.cartPrice, 0);
		document.getElementById('cart-total').innerText = total;
	}
}

/**
 * Represents a product in a shopping cart.
 * @constructor
 * @extends Product
 * @param {integer} cartPrice - The total price of this product in the cart.
 * @param {integer} quantity - The quantity of this product in the cart.
 */
class ProductInCart extends Product {
	constructor(options) {
		super(options);
		this.cartPrice = options.price;
		this.quantity = 1;
	}
}

/**
  * Initializes our application.
  */
const url = 'https://raw.githubusercontent.com/anythingcodes/emoji-shop/master/data/emoji.json';
const cart = new ShoppingCart();
// ANSWER TO 13a:
fetch(url)
	.then(serverResponse => serverResponse.json())
	.then(response => {
		const productContainer = document.getElementById('products');
	
		// ANSWER TO 14a:
		const products = response.map(responseItem => {
			const newProduct = new Product(responseItem);
			productContainer.innerHTML += newProduct.getHTML();
			return newProduct;
		});
	
		const links = productContainer.querySelectorAll('a');
	
		// ANSWER TO 15a AND 15c:
		links.forEach(link => {
			// ANSWER TO 13b:
			link.addEventListener('click', evt => {
				evt.preventDefault();
				const id = parseInt(evt.target.getAttribute('data-id'), 10);
				
				// ANSWER TO 15b AND 15bi:
				const matchingProduct = products.find(product => product.id === id);
				
				cart.addProduct(matchingProduct);
			});
		})
	})
	.catch(err => console.log(err));


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console