-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
229 lines (205 loc) · 6.93 KB
/
App.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
import React, {useState} from 'react';
import type {Node} from 'react';
import {
FlatList, Image,
SafeAreaView, StatusBar,
StyleSheet, Text,
TouchableOpacity, View,
} from 'react-native';
import CartItem from './components/CartItem';
import NfcManager, {NfcEvents} from 'react-native-nfc-manager';
import {Dialog, Portal} from 'react-native-paper';
import PreviewProduct from './components/PreviewProduct';
const allProductsInStore = {
'346FBB810DACC6': {
id: '346FBB810DACC6',
title: 'Shoulder rolls tee',
description: 'Shoulder rolls tee description',
url: 'https://storage.googleapis.com/material-vignettes.appspot.com/image/34-0.jpg',
priceInDollars: 27,
stock: 8,
quantity: 1,
},
'0E05B021': {
id: '0E05B021',
title: 'Cerise scallop tee',
description: 'Cerise scallop tee description',
url: 'https://storage.googleapis.com/material-vignettes.appspot.com/image/33-0.jpg',
priceInDollars: 42,
stock: 2,
quantity: 1,
},
'34A27F19CE96C6': {
id: '34A27F19CE96C6',
title: 'Grey slouch tank',
description: 'Grey slouch tank description',
url: 'https://storage.googleapis.com/material-vignettes.appspot.com/image/35-0.jpg',
priceInDollars: 24,
stock: 1,
quantity: 1,
},
'3': {
id: '3',
title: 'Sunshirt dress',
description: 'Sunshirt dress description',
url: 'https://storage.googleapis.com/material-vignettes.appspot.com/image/36-0.jpg',
priceInDollars: 58,
stock: 12,
quantity: 1,
}, '4': {
id: '4',
title: 'Fine lines tee',
description: 'Fine lines tee description',
url: 'https://storage.googleapis.com/material-vignettes.appspot.com/image/37-0.jpg',
priceInDollars: 58,
stock: 3,
quantity: 1,
},
};
const App: () => Node = () => {
const [productsInCart, updateProductsInCart] = useState({});
const [currentlyScannedProduct, setCurrentlyScannedProduct] = useState(null);
const [modalVisible, setModalVisible] = useState(false);
React.useEffect(() => {
NfcManager.start();
}, []);
const updateItemQuantity = (id, quantity) => {
productsInCart[id].quantity = quantity;
updateProductsInCart({...productsInCart});
};
const removeItem = (id) => {
delete productsInCart[id];
updateProductsInCart({...productsInCart});
};
const onShowAddProductModal = () => {
readNdef();
setModalVisible(true);
};
const addToCart = () => {
updateProductsInCart({...productsInCart, [currentlyScannedProduct.id]: currentlyScannedProduct});
onDismissAddProductModal();
};
function onTagFound(tagFound) {
let id = tagFound.id;
let product = allProductsInStore[id];
if (!product) {
console.log('Product not found for this tag');
}
if (productsInCart[id]) {
console.log('Product has been added to cart');
}
setCurrentlyScannedProduct(product);
}
const cleanUp = () => {
NfcManager.unregisterTagEvent().catch(() => 0);
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
NfcManager.setEventListener(NfcEvents.SessionClosed, null);
};
function readNdef() {
return new Promise((resolve) => {
let tagFound = null;
NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag) => {
tagFound = tag;
onTagFound(tagFound);
console.log('tag', tag);
resolve(tagFound);
});
NfcManager.setEventListener(NfcEvents.SessionClosed, () => {
if (!tagFound) {
resolve();
}
});
NfcManager.registerTagEvent();
});
}
const onDismissAddProductModal = () => {
cleanUp();
setModalVisible(false);
setCurrentlyScannedProduct(null);
};
const onDiscardScannedItem = () => {
setCurrentlyScannedProduct(null);
};
return (
<SafeAreaView style={styles.container}>
<StatusBar backgroundColor={'#FBB8AC'}/>
{!Object.keys(productsInCart).length &&
<View style={styles.noItemsContainer}>
<Text style={styles.noItemsText}>No items in cart.</Text>
<Text style={styles.noItemsText}>Tap on the plus button "+" to add items to your cart</Text>
</View>}
<FlatList
contentInsetAdjustmentBehavior="automatic" data={Object.values(productsInCart)}
renderItem={({item}) => <CartItem key={item.id} product={item}
updateItemQuantity={updateItemQuantity} removeItem={removeItem}/>}/>
<TouchableOpacity activeOpacity={0.5} style={styles.fabContainer} onPress={onShowAddProductModal}>
<Image source={require('./assets/icon_plus_fab.png')} style={[styles.icon, styles.fabIcon]}/>
</TouchableOpacity>
<Portal>
<Dialog
style={{height: '80%'}}
dismissable={true}
onDismiss={onDismissAddProductModal}
visible={modalVisible}>
<Dialog.Content
style={styles.dialogContainer}>
{!currentlyScannedProduct &&
<Text style={styles.directiveText}>Scan the NFC tag located on the product</Text>}
{currentlyScannedProduct &&
<PreviewProduct product={currentlyScannedProduct} addToCart={addToCart}
discard={onDiscardScannedItem}/>}
</Dialog.Content>
</Dialog>
</Portal>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
height: '100%',
backgroundColor: 'white',
},
icon: {
height: 30,
width: 30,
},
fabContainer: {
width: 60,
height: 60,
borderRadius: 30,
backgroundColor: '#FEDBD0',
position: 'absolute',
bottom: 10,
right: 10,
justifyContent: 'center',
},
fabIcon: {
alignSelf: 'center',
},
noItemsContainer: {
height: '100%',
alignSelf: 'center',
justifyContent: 'center',
paddingBottom: 50,
flexDirection: 'column',
display: 'flex',
},
noItemsText: {
textAlign: 'center',
fontSize: 22,
padding: 8,
alignSelf: 'center',
},
directiveText: {
fontSize: 24,
textAlign: 'center',
},
dialogContainer: {
flexDirection: 'row',
alignContent: 'center',
backgroundColor: 'white',
height: '100%',
borderRadius: 4,
},
});
export default App;