-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexercise-redux-dispatch-start.js
142 lines (129 loc) · 3.11 KB
/
exercise-redux-dispatch-start.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
// Run this with
// npm run exercise5
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>
);
}
}
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({});
}
continueShopping() {
this.props.dispatch({});
}
finish() {
this.props.dispatch({});
}
addProduct(product) {
this.props.dispatch({});
}
render() {
return (
<div>
{this.props.status === 'CHECKING_OUT' && (
<div>
<p>
You've started checking out with {this.props.basket.items.length}{' '}
items.
</p>
<button onClick={this.continueShopping}>Continue shopping</button>
<button onClick={this.finish}>Finish</button>
</div>
)}
{this.props.status === 'SHOPPING' && (
<Basket
items={this.props.basket.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
};
case 'CONTINUE_SHOPPING':
return {
...state
};
case 'DONE':
return {
...state
};
case 'ADD_PRODUCT':
return {
...state
};
default:
return state;
}
};
const store = createStore(appReducer);
const ConnectedApp = connect(state => state)(App);
ReactDOM.render(
<Provider store={store}>
<ConnectedApp />
</Provider>,
document.querySelector('#app')
);