<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/ivanmarkovic/pen/yaWJBd?depth=everything&order=popularity&page=36&q=react&show_forks=false" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css'>
<style class="cp-pen-styles">* {
box-sizing: border-box;
}
#app {
margin: 0;
padding: 0;
}
.container {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.links {
display: flex;
align-items: center;
justify-content: center;
}
.links > span {
padding: 15px;
width: 120px;
background-color: orangered;
display: flex;
align-items: center;
justify-content: center;
color: whitesmoke;
opacity: 0.9;
cursor: pointer;
}
.links > span:hover {
opacity: 1;
}
.links > span > i {
font-size: 2em;
}
.cats-all {
display: flex;
flex-wrap: wrap;
padding: 10px;
align-items: center;
justify-content: center;
}
.single-cat {
width: 30%;
margin: 10px;
padding: 10px;
box-shadow: 5px 10px 20px -15px #000;
color: dimgray;
}
.single-cat > img {
width: 100%;
}
.single-cat > span {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
background-color: orangered;
border-radius: 50%;
opacity: 0.9;
color: whitesmoke;
cursor: pointer;
}
.single-cat > span:hover {
opacity: 1;
}
.single-cat > span > i {
font-size: 1.5em;
}
.cart {
display: flex;
flex-wrap: wrap;
flex-direction: column;
padding: 10px;
align-items: center;
justify-content: center;
color: dimgray;
}
.cart > .cart-cats-wrapper {
display: flex;
flex-wrap: wrap;
padding: 10px;
align-items: center;
justify-content: center;
}
.cart > .cart-cats-wrapper .single-cat {
flex: 1 2 45%
}
.comments {
width: 500px;
max-width: 100%;
display: flex;
flex-direction: column;
padding: 10px;
align-items: center;
justify-content: center;
color: dimgray;
}
.comments > input {
outline: none;
font-size: 1.2em;
padding: 7px 10px;
}
.comments > span {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
background-color: orangered;
border-radius: 50%;
opacity: 0.9;
color: whitesmoke;
cursor: pointer;
margin-top: 10px;
}
.comments > div,
.comments p {
margin: 0;
padding: 0px;
}
.comments > div {
margin: 15px;
padding: 2px;
box-shadow: 5px 10px 20px -15px #000;
}
.rotateBasket {
animation-name: rotateBasket;
animation-duration: 2s;
animation-delay: 0s;
animation-direction: normal;
animation-iteration-count: 2;
animation-fill-mode: forwards;
animation-timing-function: linear;
}
@keyframes rotateBasket {
0% {
transform: rotate(0deg);
}
20% {
transform: rotate(45deg);
}
40% {
transform: rotate(-45deg);
}
60% {
transform: rotate(45deg);
}
80% {
transform: rotate(-45deg);
}
100% {
transform: rotate(0deg);
}
}
@media all and (max-width: 900px) {
.single-cat {
width: 45%;
}
.cart > .cart-cats-wrapper .single-cat {
flex: 1 2 45%;
}
}
@media all and (max-width: 700px) {
.single-cat {
width: 90%;
}
.cart > .cart-cats-wrapper .single-cat {
flex: 1 0 90%;
}
}</style></head><body>
<div id="app">
</div>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js'></script>
<script >"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var cats = [{
"id": "1",
"name": "Cat with a tie",
"description": "Cat with a tie",
"price": "12.99",
"src": "https://raw.githubusercontent.com/icamarkov/reactjs-cat-shop/master/images/326H.jpg",
"count": "3"
}, {
"id": "2",
"name": "Black cat",
"description": "Black cat description",
"price": "22.99",
"src": "https://raw.githubusercontent.com/icamarkov/reactjs-cat-shop/master/images/animal-eyes-animals-black.jpg",
"count": "2"
}, {
"id": "3",
"name": "Black-white cat",
"description": "Black-white cat description goes here",
"price": "17.99",
"src": "https://raw.githubusercontent.com/icamarkov/reactjs-cat-shop/master/images/black-white.jpg",
"count": "5"
}, {
"id": "4",
"name": "Blue cat",
"description": "Blue cat description goes here",
"price": "27.99",
"src": "https://raw.githubusercontent.com/icamarkov/reactjs-cat-shop/master/images/blue.jpeg",
"count": "3"
}];
var comments = ["Do you have a cat with a hat?", "Nice"];
var App = function (_React$Component) {
_inherits(App, _React$Component);
function App(props) {
_classCallCheck(this, App);
var _this = _possibleConstructorReturn(this, _React$Component.call(this));
_this.state = {
cats: cats,
cart: [],
display: {
cats: true,
cart: false,
comments: false
},
comments: comments,
cartLinkClassName: "material-icons"
};
return _this;
}
App.prototype.addToCart = function addToCart(id) {
var _this2 = this;
console.log("Added :", id);
var cats = this.state.cats;
var cart = this.state.cart;
var _loop = function _loop(i) {
if (cats[i].id == id && cats[i].count > 0) {
if (cart.length === 0) {
cats[i].count = cats[i].count - 1;
var cat = Object.assign({}, cats[i]);
delete cat.count;
cat.howMany = typeof cat.howMany === "undefined" ? 1 : cat.howMany + 1;
cart.push(cat);
} else {
var result = cart.filter(function (item) {
return item.id == cats[i].id;
});
if (result.length === 1) {
for (var j = 0; j < cart.length; j++) {if (window.CP.shouldStopExecution(1)){break;}
if (cart[j].id == cats[i].id) {
cats[i].count = cats[i].count - 1;
cart[j].howMany++;
}
}
window.CP.exitedLoop(1);
} else {
cats[i].count--;
var cat = Object.assign({}, cats[i]);
delete cat.count;
cat.howMany = typeof cat.howMany === "undefined" ? 1 : cat.howMany + 1;
cart.push(cat);
}
}
}
};
for (var i = 0; i < cats.length; i++) {if (window.CP.shouldStopExecution(2)){break;}
_loop(i);
}
window.CP.exitedLoop(2);
this.setState({
cats: cats,
cart: cart,
cartLinkClassName: "material-icons rotateBasket"
});
setTimeout(function () {
return _this2.setState({
cartLinkClassName: "material-icons"
});
}, 2000);
};
App.prototype.removeItem = function removeItem(id) {
var cats = this.state.cats;
var cart = this.state.cart;
var itemsToGetBack = 0;
for (var i = 0; i < cart.length; i++) {if (window.CP.shouldStopExecution(3)){break;}
if (cart[i].id == id) {
var cartIndex = i;
itemsToGetBack = cart[i].howMany;
cart.splice(cartIndex, 1);
}
}
window.CP.exitedLoop(3);
for (var i = 0; i < cats.length; i++) {if (window.CP.shouldStopExecution(4)){break;}
if (cats[i].id == id) {
cats[i].count = cats[i].count + itemsToGetBack;
}
}
window.CP.exitedLoop(4);
this.setState({
cats: cats,
cart: cart
});
};
App.prototype.displayThisComponent = function displayThisComponent(comp) {
switch (comp) {
case "cats":
this.setState({
display: {
cats: true,
cart: false,
comments: false
}
});
break;
case "cart":
this.setState({
display: {
cats: false,
cart: true,
comments: false
}
});
break;
case "comments":
this.setState({
display: {
cats: false,
cart: false,
comments: true
}
});
break;
}
};
App.prototype.addComment = function addComment(comment) {
var comments = this.state.comments;
comments.push(comment);
this.setState({
comments: comments
});
};
App.prototype.render = function render() {
var _this3 = this;
var cats = this.state.display.cats ? React.createElement(Cats, { cats: this.state.cats, addToCart: this.addToCart.bind(this) }) : "";
var cart = this.state.display.cart ? React.createElement(Cart, { cart: this.state.cart, removeItem: this.removeItem.bind(this) }) : "";
var comments = this.state.display.comments ? React.createElement(Comments, { comments: this.state.comments, addComment: this.addComment.bind(this) }) : "";
return React.createElement(
"div",
{ className: "container" },
React.createElement(
"h1",
null,
"Hello!"
),
React.createElement(
"div",
{ className: "links" },
React.createElement(
"span",
{ onClick: function onClick() {
return _this3.displayThisComponent("cats");
} },
React.createElement(
"i",
{ className: "material-icons" },
"store"
)
),
React.createElement(
"span",
{ onClick: function onClick() {
return _this3.displayThisComponent("cart");
} },
React.createElement(
"i",
{ className: this.state.cartLinkClassName },
"add_shopping_cart"
)
),
React.createElement(
"span",
{ onClick: function onClick() {
return _this3.displayThisComponent("comments");
} },
React.createElement(
"i",
{ className: "material-icons" },
"chat_bubble"
)
)
),
cats,
cart,
comments
);
};
return App;
}(React.Component);
var Cats = function (_React$Component2) {
_inherits(Cats, _React$Component2);
function Cats() {
_classCallCheck(this, Cats);
return _possibleConstructorReturn(this, _React$Component2.apply(this, arguments));
}
Cats.prototype.render = function render() {
var addToCart = this.props.addToCart;
var catsNodes = this.props.cats.map(function (cat, i) {
return React.createElement(Cat, { key: i, cat: cat, addToCart: addToCart });
});
return React.createElement(
"div",
{ className: "cats-all" },
catsNodes
);
};
return Cats;
}(React.Component);
;
var Cat = function (_React$Component3) {
_inherits(Cat, _React$Component3);
function Cat() {
_classCallCheck(this, Cat);
return _possibleConstructorReturn(this, _React$Component3.apply(this, arguments));
}
Cat.prototype.render = function render() {
var _this6 = this;
return React.createElement(
"div",
{ className: "single-cat" },
React.createElement(
"h3",
null,
this.props.cat.name
),
React.createElement("img", { src: this.props.cat.src }),
React.createElement(
"p",
null,
this.props.cat.description
),
React.createElement(
"p",
null,
React.createElement(
"b",
null,
"Price"
),
" ",
this.props.cat.price,
" | ",
this.props.cat.count,
" ",
this.props.cat.name,
" available"
),
React.createElement(
"span",
{ onClick: function onClick() {
return _this6.props.addToCart(_this6.props.cat.id);
} },
React.createElement(
"i",
{ className: "material-icons" },
"add_shopping_cart"
)
)
);
};
return Cat;
}(React.Component);
;
var Cart = function (_React$Component4) {
_inherits(Cart, _React$Component4);
function Cart() {
_classCallCheck(this, Cart);
return _possibleConstructorReturn(this, _React$Component4.apply(this, arguments));
}
Cart.prototype.removeItem = function removeItem(id) {
this.props.removeItem(id);
};
Cart.prototype.render = function render() {
var removeItem = this.removeItem.bind(this);
var totalPrice = 0;
for (var i = 0; i < this.props.cart.length; i++) {if (window.CP.shouldStopExecution(5)){break;}
totalPrice = totalPrice + this.props.cart[i].price * this.props.cart[i].howMany;
}
window.CP.exitedLoop(5);
totalPrice = totalPrice.toFixed(2);
var itemsNodes = this.props.cart.map(function (item, i) {
var singleCatTotal = (item.price * item.howMany).toFixed(2);
return React.createElement(
"div",
{ key: i, className: "single-cat" },
React.createElement(
"h3",
null,
item.name
),
React.createElement("img", { src: item.src }),
React.createElement(
"p",
null,
item.description
),
React.createElement(
"p",
null,
"Items ",
item.howMany,
" | price ",
item.price,
" | total ",
singleCatTotal
),
React.createElement(
"span",
{ onClick: function onClick() {
return removeItem(item.id);
} },
React.createElement(
"i",
{ className: "material-icons" },
"delete"
)
)
);
});
return React.createElement(
"div",
{ className: "cart" },
React.createElement(
"div",
{ className: "cart-cats-wrapper" },
itemsNodes
),
React.createElement(
"h3",
null,
"Total : ",
totalPrice
)
);
};
return Cart;
}(React.Component);
;
var Comments = function (_React$Component5) {
_inherits(Comments, _React$Component5);
function Comments(props) {
_classCallCheck(this, Comments);
var _this8 = _possibleConstructorReturn(this, _React$Component5.call(this));
_this8.state = {
comment: ""
};
return _this8;
}
Comments.prototype.getComment = function getComment(event) {
this.setState({
comment: event.target.value
});
};
Comments.prototype.addComment = function addComment() {
if (this.state.comment != "") {
this.props.addComment(this.state.comment);
}
this.setState({
comment: ""
});
};
Comments.prototype.render = function render() {
var commentsNodes = this.props.comments.map(function (comment, i) {
return React.createElement(
"div",
{ key: i },
React.createElement(
"p",
null,
comment
)
);
});
return React.createElement(
"div",
{ className: "comments" },
React.createElement(
"h1",
null,
"Comments"
),
React.createElement("input", { type: "text", onChange: this.getComment.bind(this), value: this.state.comment }),
React.createElement(
"span",
null,
React.createElement(
"i",
{ className: "material-icons", onClick: this.addComment.bind(this) },
"message"
)
),
commentsNodes
);
};
return Comments;
}(React.Component);
;
ReactDOM.render(React.createElement(App, { cats: cats }), document.getElementById("app"));
//# sourceURL=pen.js
</script>
</body></html>