-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexercise-items-selector.js
164 lines (150 loc) · 3.61 KB
/
exercise-items-selector.js
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
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider, connect} from 'react-redux';
import {createStore} from 'redux';
class Basket extends React.Component {
render() {
return (
<div>
<p>You have {this.props.items.length} items in your basket</p>
<div>{this.props.items.map(item => this.props.renderItem(item))}</div>
<button onClick={() => this.props.onCheckout(this.props.items)}>
Proceed to checkout
</button>
</div>
);
}
}
const selectBasketItems = state =>
(state && state.basket && state.basket.items) || [];
class App extends React.Component {
constructor() {
super();
this.handleCheckout = this.handleCheckout.bind(this);
this.continueShopping = this.continueShopping.bind(this);
this.finish = this.finish.bind(this);
this.addProduct = this.addProduct.bind(this);
}
handleCheckout(items) {
this.props.dispatch({
type: 'START_CHECKOUT',
basket: {
items
}
});
}
continueShopping() {
this.props.dispatch({
type: 'CONTINUE_SHOPPING'
});
}
finish() {
this.props.dispatch({
type: 'DONE'
});
}
addProduct(product) {
this.props.dispatch({
type: 'ADD_PRODUCT',
newProduct: {
name: product.name,
price: product.price,
quantity: 1
}
});
}
render() {
// Full state in currently in props
const items = selectBasketItems(this.props);
return (
<div>
{this.props.status === 'CHECKING_OUT' && (
<div>
<p>You've started checking out with {items.length} items.</p>
<button onClick={this.continueShopping}>Continue shopping</button>
<button onClick={this.finish}>Finish</button>
</div>
)}
{this.props.status === 'SHOPPING' && (
<Basket
items={items}
renderItem={item => (
<div>
x{item.quantity} - {item.name} - $
{(item.price / 100).toFixed(2)} each{' '}
</div>
)}
onCheckout={this.handleCheckout}
/>
)}
{this.props.status === 'DONE' && <p>Done</p>}
{this.props.status === 'SHOPPING' && (
<div style={{marginTop: 50}}>
<h2>{this.props.product.name}</h2>
<p>Price: ${this.props.product.price / 100}</p>
<button onClick={() => this.addProduct(this.props.product)}>
Add to Basket
</button>
</div>
)}
</div>
);
}
}
const defaultState = {
status: 'SHOPPING',
product: {
price: 499,
name: 'Biscuits'
},
basket: {
items: [
{
quantity: 1,
price: 199,
name: 'Soda bottle'
},
{
quantity: 2,
price: 2499,
name: 'Kitchenware kits'
}
]
}
};
const appReducer = (state = defaultState, action) => {
switch (action.type) {
case 'START_CHECKOUT':
return {
...state,
status: 'CHECKING_OUT'
};
case 'CONTINUE_SHOPPING':
return {
...state,
status: 'SHOPPING'
};
case 'DONE':
return {
...state,
status: 'DONE'
};
case 'ADD_PRODUCT':
return {
...state,
basket: {
items: selectBasketItems(state).concat(action.newProduct)
}
};
default:
return state;
}
};
const store = createStore(appReducer);
const ConnectedApp = connect(state => state)(App);
ReactDOM.render(
<Provider store={store}>
<ConnectedApp />
</Provider>,
document.querySelector('#app')
);