-
Notifications
You must be signed in to change notification settings - Fork 4
/
quantityPanel.js
executable file
·245 lines (204 loc) · 5.14 KB
/
quantityPanel.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use strict';
//panel create item
//lies on top of main list, fills up bottom 90% of screen
var React = require('react');
var {
StyleSheet,
Image,
View,
TouchableHighlight,
TouchableOpacity,
ListView,
Text,
TextInput,
Animated,
Component,
NavigatorIOS,
AsyncStorage,
SwitchIOS
} = React;
var Dimensions = require('Dimensions')
var deviceSize = {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
}
var styles = StyleSheet.create({
textContainer: {
flex: 1
},
separator: {
height: 1,
backgroundColor: '#dddddd'
},
price: {
fontSize: 25,
fontWeight: 'bold',
color: '#48BBEC'
},
title: {
fontSize: 20,
color: '#656565'
},
label:{
fontSize: 14,
color: '#ADA96E'
},
inputContainer: {
flexDirection: 'column',
padding: 20
},
headerContainer:{
padding: 20,
justifyContent: 'center',
flexDirection: 'row',
},
inventoryContainer:{
flexDirection: 'row',
padding: 20,
justifyContent: 'center'
},
header: {
justifyContent: 'center',
},
searchBar: {
height: 40,
backgroundColor: '#EBF4FA',
padding: 20,
},
blackBox:{
position: 'absolute',
backgroundColor: 'rgba(0,0,0,0.5)',
top: 0, left: 0,
width: deviceSize.width,
height: deviceSize.height,
},
panel: {
position: 'absolute',
backgroundColor:'#FFFFFF',
width: deviceSize.width,
height: deviceSize.height
},
control:{
fontSize: 30,
padding:30,
color: '#656565'
},
buttons:{
marginLeft:30,
marginRight:30,
color: '#5CB3FF',
}
})
var CART_KEY = 'cartList';
class QuantityPanel extends Component {
//constructor
constructor(props){
super(props);
this.state={
//extract data
cartList: [],
quantity : this.props.item.quantityOrdered,
name: this.props.item.prod.product,
price: this.props.item.prod.price,
quant: this.props.item.prod.quantity,
id: this.props.item.prod.id,
fadeAnim: new Animated.Value(deviceSize.height),
}
}
componentDidMount() {
this._loadInitialState().done();
Animated.timing(
this.state.fadeAnim,
{toValue: 0},
).start();
}
async _loadInitialState() {
try {
var cartVal = await AsyncStorage.getItem(CART_KEY);
if (cartVal !== null){
this.setState({cartList: JSON.parse(cartVal)});
console.log('in cartList copy: ' + this.state.cartList);
console.log('cartlist - Recovered selection from disk: ' + cartVal);
console.log('name: ' +this.state.name)
this.state.cartList.forEach(function(i){
console.log('forEach '+ i.prod.product)
})
} else {
console.log('cartlist - Initialized with no selection on disk.');
}
} catch (error) {
console.log('cartlist - AsyncStorage error: ' + error.message);
}
}
addQuantity(){
//check if there are enough inventory items to order more
if(this.state.quantity < this.state.quant){
this.setState({ quantity: this.state.quantity + 1})
}
//else display out of stock
}
subtractQuantity(){
if(this.state.quantity > 0) {
this.setState({ quantity: this.state.quantity - 1})
}
}
submitChange(){
//update the cart list
//hide the panel
console.log("this.state.id"+ this.state.id);
console.log("cartList"+ JSON.stringify(this.state.cartList))
this.state.cartList.forEach(function(arrayItem){
if(arrayItem.prod.id === this.state.id){
//this is the item to update quantity on the list
arrayItem.quantityOrdered = this.state.quantity
console.log("quantity updated"+ arrayItem.quantityOrdered);
}
}.bind(this))
AsyncStorage.setItem(CART_KEY, JSON.stringify(this.state.cartList));
Animated.timing(
this.state.fadeAnim,
{toValue: deviceSize.height },
).start(() => this.props.hidePanel());
}
cancelChange(){
Animated.timing(
this.state.fadeAnim,
{toValue: deviceSize.height },
).start(() => this.props.hidePanel());
}
render(){
return(
<View style={styles.blackBox}>
<TouchableOpacity onPress={this.cancelChange.bind(this)}>
<View style={{marginTop: 0, height: 300}}/>
</TouchableOpacity>
<Animated.View style={{marginTop: this.state.fadeAnim}}>
<View style={styles.panel}>
<View style={styles.headerContainer}>
<TouchableOpacity onPress={this.cancelChange.bind(this)}>
<Text style={styles.buttons} >close</Text>
</TouchableOpacity>
<Text style={styles.title}>QUANTITY</Text>
<TouchableOpacity onPress={this.props.hidePanel}>
<Text style={styles.buttons} onPress={this.submitChange.bind(this)}>save</Text>
</TouchableOpacity>
</View>
<View style={styles.separator}/>
<View style={styles.inventoryContainer}>
<TouchableOpacity onPress={this.subtractQuantity.bind(this)}>
<Text style={styles.control}>-</Text>
</TouchableOpacity>
<View>
<Text style={styles.control}>{this.state.quantity}</Text>
</View>
<TouchableOpacity onPress={this.addQuantity.bind(this)}>
<Text style={styles.control}>+</Text>
</TouchableOpacity>
</View>
</View>
</Animated.View>
</View>
);
}
}
module.exports= QuantityPanel;