This repository was archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathapp.jsx
More file actions
171 lines (155 loc) · 4.58 KB
/
app.jsx
File metadata and controls
171 lines (155 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { createHashHistory, useBasename} from 'history'
import React from 'react'
import ReactDOM from 'react-dom'
import { Router, Route, IndexRoute, Link, IndexLink } from 'react-router'
const history = createHashHistory({
})
const PRODUCTS = [
{ id: 0, src: 'images/proexpress-cover.jpg', title: 'Pro Express.js', url: 'http://amzn.to/1D6qiqk' },
{ id: 1, src: 'images/practicalnode-cover.jpeg', title: 'Practical Node.js', url: 'http://amzn.to/NuQ0fM' },
{ id: 2, src: 'images/expressapiref-cover.jpg', title: 'Express API Reference', url: 'http://amzn.to/1xcHanf' },
{ id: 3, src: 'images/reactquickly-cover.jpg', title: 'React Quickly', url: 'https://www.manning.com/books/react-quickly'},
{ id: 4, src: 'images/fullstack-cover.png', title: 'Full Stack JavaScript', url: 'http://www.apress.com/9781484217504'}
]
let CartItems = {}
const Modal = React.createClass({
styles: {
position: 'fixed',
top: '20%',
right: '20%',
bottom: '20%',
left: '20%',
width: 450,
height: 400,
padding: 20,
boxShadow: '0px 0px 150px 130px rgba(0, 0, 0, 0.5)',
overflow: 'auto',
background: '#fff'
},
render() {
return (
<div style={this.styles}>
<p><Link to={this.props.returnTo}>Back</Link></p>
{this.props.children}
</div>
)
}
})
const Heading = () => {
return <h1>Nile Book Store</h1>
}
const Copy = () => {
return <p>Please click on a book to view details in a modal. You can copy/paste the link of the modal. The link will open book on a separate page.</p>
}
const Cart = React.createClass ({
render() {
return <div>
{(Object.keys(CartItems).length == 0) ? <p>Your cart is empty</p> : '' }
<ul>
{Object.keys(CartItems).map((item, index, list)=>{
return <li key={item}>{PRODUCTS[item].title} - {CartItems[item]}</li>
})}
</ul>
<Link to="/checkout" className="btn btn-primary">Checkout</Link>
<Link to="/" className="btn btn-info">Home</Link>
</div>
}
})
const Checkout = React.createClass({
render() {
let count = 0
return <div><h1>Invoice</h1><table className="table table-bordered"><tbody>
{Object.keys(CartItems).map((item, index, list)=>{
count += CartItems[item]
return <tr key={item}>
<td>{PRODUCTS[item].title}</td>
<td>{CartItems[item]}</td>
</tr>
})}
</tbody></table><p>Total: {count}</p></div>
}
})
const App = React.createClass({
componentWillReceiveProps(nextProps) {
if (nextProps.location.key !== this.props.location.key &&
nextProps.location.state &&
nextProps.location.state.modal
) {
this.previousChildren = this.props.children
}
},
render() {
let isModal = (this.props.location.state &&
this.props.location.state.modal &&
this.previousChildren
)
return (
<div className="well">
<Heading/>
<div>
{this.props.children}
{(isModal)?
<Modal isOpen={true} returnTo={this.props.location.state.returnTo}>
{this.props.children}
</Modal> : ''
}
</div>
</div>
)
}
})
const Index = React.createClass ({
render() {
return (
<div>
<Copy/>
<p><Link to="/cart" className="btn btn-danger">Cart</Link></p>
<div>
{PRODUCTS.map(picture => (
<Link key={picture.id}
to={`/products/${picture.id}`}
state={{ modal: true, returnTo: this.props.location.pathname }}>
<img style={{ margin: 10 }} src={picture.src} height="100" />
</Link>
))}
</div>
</div>
)
}
})
const Product = React.createClass({
handlerBuy () {
this.props.route.handlerBuy(this.props.params.id)
},
render() {
return (
<div>
<img src={PRODUCTS[this.props.params.id].src} style={{ height: '80%' }} />
<p>{PRODUCTS[this.props.params.id].title}</p>
<Link
to={`/cart`}
onClick={this.handlerBuy}
state={{ productId: this.props.params.id}}
className="btn btn-primary">
Buy
</Link>
</div>
)
}
})
const handlerBuy = (id) => {
if (CartItems[id])
CartItems[id] += 1
else
CartItems[id] = 1
}
ReactDOM.render((
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="/products/:id" component={Product} handlerBuy={handlerBuy} />
<Route path="/cart" component={Cart}/>
</Route>
<Route path="/checkout" component={Checkout}/>
</Router>
), document.getElementById('content'))