Skip to content

Commit

Permalink
Demonstrate cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaswitek committed Jun 13, 2018
1 parent f16759e commit 9381842
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
92 changes: 92 additions & 0 deletions cart/components/Cart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { Component } from "react";
import Button from "@panter/core/components/Button";

class Cart extends Component {
state = {
items: [
{
name: "Short coat",
price: 229.49,
quantity: 1,
image: "product-1.jpeg",
size: "44"
}
]
};

incrementQuantity = index => {
const item = this.state.items[index];
const updatedItem = { ...item, quantity: item.quantity + 1 };
this.setState({ items: [updatedItem] });
};

decrementQuantity = index => {
const item = this.state.items[index];
const updatedItem = { ...item, quantity: item.quantity - 1 };
this.setState({ items: [updatedItem] });
};

render() {
const { items } = this.state;

return (
<div>
<table cellPadding="0" cellSpacing="0">
<thead>
<tr>
<th>Image</th>
<th>Product</th>
<th>Size</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
{items.map(({ image, name, size, price, quantity }, index) => (
<tr key={index}>
<td width="300">
<img src={`/static/images/${image}`} width="200" />
</td>
<td>{name}</td>
<td>{size}</td>
<td>{price}</td>
<td>
{quantity}
<button onClick={() => this.incrementQuantity(index)}>
+
</button>
<button onClick={() => this.decrementQuantity(index)}>
-
</button>
</td>
<td>{price * quantity}</td>
</tr>
))}
</tbody>
</table>

<Button>Go to checkout</Button>

<style jsx>{`
div {
text-align: center;
}
table {
width: 100%;
text-align: left;
}
td {
padding-top: 12px;
}
th {
padding-bottom: 12px;
border-bottom: 1px solid gray;
}
`}</style>
</div>
);
}
}

export default Cart;
17 changes: 17 additions & 0 deletions cart/components/Title.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";

const Title = ({ children }) => {
return (
<h1>
{children}
<style jsx>{`
h1 {
text-align: center;
padding-bottom: 24px;
}
`}</style>
</h1>
);
};

export default Title;
4 changes: 2 additions & 2 deletions cart/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const withTypescript = require("@zeit/next-typescript");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");

// Update these to match your package scope name.
const internalNodeModulesRegExp = /@tomaswitek(?!.*node_modules)/;
const externalNodeModulesRegExp = /node_modules(?!\/@tomaswitek(?!.*node_modules))/;
const internalNodeModulesRegExp = /@panter(?!.*node_modules)/;
const externalNodeModulesRegExp = /node_modules(?!\/@panter(?!.*node_modules))/;

module.exports = withBundleAnalyzer(
withTypescript(
Expand Down
3 changes: 2 additions & 1 deletion cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"private": true,
"dependencies": {
"@tomaswitek/core": "1.0.0",
"@panter/core": "1.0.0",
"@types/styled-jsx": "^2.2.3",
"decimal.js": "^10.0.0",
"next": "^5.1.0",
"react": "^16.3.1",
Expand Down
17 changes: 7 additions & 10 deletions cart/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import React from "react";
import { Components } from "@tomaswitek/core";

const { Header, Footer } = Components;
// FYI you can also use separate import
//import Header from "@tomaswitek/core/components/Header";
import Layout from "@panter/core/components/Layout";
import Title from "../components/Title";
import Cart from "../components/Cart";

const index = () => {
return (
<div>
<Header />
Cart App
<Footer />
</div>
<Layout>
<Title>Your Cart</Title>
<Cart />
</Layout>
);
};

Expand Down
Binary file added cart/static/images/product-1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions core/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";

const Button = ({ children }) => {
return (
<span>
{children}
<style jsx>{`
span {
padding: 12px;
background: #67b88f;
color: white;
text-transform: uppercase;
cursor: pointer;
}
`}</style>
</span>
);
};

export default Button;
7 changes: 0 additions & 7 deletions core/components/Footer.tsx

This file was deleted.

61 changes: 58 additions & 3 deletions core/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
import React from "react";
import Logo from "./Logo";

const Header = () => {
return <div>Header</div>;
};
const Header = () => (
<div>
<Logo />
<ul>
<li>
<a>Sale</a>
</li>
<li>
<a>Female</a>
</li>
<li>
<a>Male</a>
</li>
<li>
<a>Home & Living</a>
</li>
<li>
<a>Children</a>
</li>
<li>
<a>Beauty</a>
</li>
<li>
<a>Delicatessa</a>
</li>
<li>
<a>Wine & drinks</a>
</li>
<li>
<a>Gifts</a>
</li>
</ul>

<style jsx>{`
div {
background: black;
color: white;
padding: 24px;
}
ul {
display: flex;
padding: 0;
margin: 24px 0 0 0;
justify-content: center;
}
li {
list-style: none;
text-transform: uppercase;
padding: 10px;
}
li:hover {
text-decoration: underline;
cursor: pointer;
}
`}</style>
</div>
);

export default Header;
25 changes: 25 additions & 0 deletions core/components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import Header from "./Header";

const Layout = ({ children }) => {
return (
<div>
<Header />
<div className="layout">{children}</div>
<style jsx global>{`
body {
margin: 0;
padding: 0;
}
`}</style>

<style jsx>{`
.layout {
padding: 24px 48px;
}
`}</style>
</div>
);
};

export default Layout;
20 changes: 20 additions & 0 deletions core/components/Logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";

const Logo = () => {
return (
<div>
Shop
<style jsx>{`
div {
color: white;
text-transform: uppercase;
font-weight: bold;
font-size: 40px;
text-align: center;
}
`}</style>
</div>
);
};

export default Logo;
6 changes: 3 additions & 3 deletions core/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Header from "./Header";
import Footer from "./Footer";
import Layout from "./Layout";
import Button from "./Button";

export default { Header, Footer };
export default { Layout, Button };
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tomaswitek/core",
"name": "@panter/core",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
dependencies:
csstype "^2.2.0"

"@types/styled-jsx@^2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@types/styled-jsx/-/styled-jsx-2.2.3.tgz#c97ed7ad8d7b4e6ffbe84e2d4c24a49eb1b9fe54"
dependencies:
"@types/react" "*"

"@zeit/[email protected]":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@zeit/check-updates/-/check-updates-1.1.1.tgz#1bee858fd3f9b8633b0fc23dff53cb17343331c0"
Expand Down

0 comments on commit 9381842

Please sign in to comment.