-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
399 lines (388 loc) · 16.7 KB
/
App.js
File metadata and controls
399 lines (388 loc) · 16.7 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import React, { Component } from 'react'
import { Text, View, StyleSheet, PermissionsAndroid, Image, Modal, Pressable, Dimensions, ScrollView, FlatList, TouchableOpacity } from 'react-native'
import MapView, {Marker} from 'react-native-maps';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import Geolocation from 'react-native-geolocation-service';
import { Header} from 'react-native-elements';
import { getDistance } from 'geolib';
//BEGINNING
export default class App extends Component {
constructor(props) {
super(props)
this.state={
//COORDINATES RETRIEVED BY GPS
latitude: null,
longitude: null,
//VIA COORDINATES, GETS ADDRESS
location: null,
postcode: null,
city: null,
//MODAL VISIBILITY AND TITLE
modalVisible: false,
modalTitle: null,
//TESTS WITH LONDON'S COORDINATES AND ADDRESS
testLat: 51.50727,
testLong: -0.1279706,
testPostCode: null,
testCity: null,
//ARRAY OF SHOP COORDINATES
shopsCoordinates: [],
//RECORD OF ALL PRODUCTS IN THE WEBSITE
allProducts: [],
//DICTIONARY CONTAINING THE PRODUCTS (VALUE) IN THE SHOPS (KEY)
data: {},
//SHOP CLICKED BY THE USER
shopClicked: null,
//LOADING DATA
isLoaded: false,
//TEST OF THE DICTIONARY OF SHOP/PRODUCTS
dataTest: {
"12543": [1231086010739, 1241086010739, 1270076013139, 1280176010738, 1230086000139, 1231476004039, 1233476010739, 1231676000139, 1231076000439, 1240286004039, 1240486000239, 1240086000139, 1241776004039, 1241676000139, 1241276000139, 1241376004039, 1270476004039, 1270676020339, 1242476000139, 1230976020339, 1242676000239, 1240176004039, 1240076000139, 1220476000139, 1231276000139, 1230476020239, 1230276003239, 1210066004039, 1200066004039, 1240476000139, 1243866000139, 1243466020239, 1240066000139, 1240976004039, 1240676004039],
"6958": [1231086010739, 1241086010739, 1270076013139, 1280176010738, 1230086000139, 1231476004039, 1233476010739, 1231076000439, 1240286004039, 1240486000239, 1240086000139, 1241776004039, 1241676000139, 1241276000139, 1241376004039, 1270476004039, 1270676020339, 1242476000139, 1230976020339, 1242676000239, 1240176004039, 1240076000139, 1220676020339, 1220476000139, 1231276000139, 1230476020239, 1230276003239, 1210066004039, 1200066004039, 1240476000139, 1243866000139, 1243466020239, 1240066000139, 1240676004039],
"8310": [1231086010739, 1241086010739, 1270076013139, 1270276004039, 1280176010738, 1280076004038, 1230086000139, 1231476004039, 1233476010739, 1231676000139, 1231076000439, 1240286004039, 1240486000239, 1240086000139, 1241776004039, 1241676000139, 1242376020339, 1241276000139, 1241376004039, 1270486000139, 1270476004039, 1270676020339, 1242876020339, 1243076000139, 1242476000139, 1241486000139, 1233076020339, 1231876004039, 1232876001239, 1232076004039, 1230976020339, 1242676000239, 1240176004039, 1240076000139, 1220676020339, 1220476000139, 1242586020239, 1231276000139, 1232476020339, 1230676020239, 1230476020239, 1230276003239, 1240876011739, 1210066004039, 1260076004039, 1210266004039, 1200066004039, 1240476000139, 1235266004039, 1243866000139, 1243466020239, 1240066000139, 1240976004039, 1240676004039]
}
}
}
//APP CHECKS IF USER HAS GPS ACTIVATED
hasLocationPermission = () => {
//SYNTAX TO DISPLAY THE POP UP OF PERMISSIONS
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'ReactNativeCode Location Permission',
'message': 'ReactNativeCode App needs access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true;
}
else {
return false
}
} catch (err) {
console.warn(err)
}
}
//BEFORE MOUNTING THE APP, THIS METHOD RECEIVES THE USER COORDINATES
async componentDidMount() {
//FOR TESTING PURPOSES, THE GPS IS DEACTIVATED AND CENTRAL LONDON'S COORDINATES ARE USED
/*if (this.hasLocationPermission) {
Geolocation.getCurrentPosition(
(position) => {
const latitude = JSON.stringify(position.coords.latitude);
const longitude = JSON.stringify(position.coords.longitude);
const location = JSON.stringify(position);
this.setState({ latitude, longitude });
this.getPostcodeAndCityFromApi();
},
(error) => {
// See error code charts below.
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
}*/
//THIS METHOD BELOW WILL GET THE POSTCODE (UK ONLY) FROM LATITUDE AND LONGITUDE
this.getPostcodeAndCityFromApi();
//--------------------------------
this.fetchData();
}
//FETCH THE DATA ASYNCHRONOUSLY (PRODUCTS AND STORES)
fetchData = async () => {
const a = await this.getStoresNearby();
const b = await this.getAllProducts();
this.setState({isLoaded: true})
//const c = await this.getProductsLocation();
}
//GET THE LOCATION OF ALL THE PRODUCTS FOUND
getProductsLocation = async () => {
//composition of the URL with the stores found and the dictionary of the products
var urlShops = "physicalStoreId="+(+this.state.shopsCoordinates[0].shopId.slice(1,-1));
var thisData = {}
thisData[(+this.state.shopsCoordinates[0].shopId.slice(1,-1))] = []
for(var i = 1; i < this.state.shopsCoordinates.length; i++)
{
urlShops = urlShops+"&physicalStoreId="+(+this.state.shopsCoordinates[i].shopId.slice(1,-1));
thisData[(+this.state.shopsCoordinates[i].shopId.slice(1,-1))] = []
}
//COMPOSITION OF THE DICTIONARY
this.setState({data: thisData})
/**
* THE DICTIONARY WILL HAVE THE FOLLOWING STRUCTURE
* {
* "shopID": [productsIDs],
* "shopID": [productsIDs],
* ...
* }
*
* */
//THE FOLLOWING WHILE LOOP WILL CHECK EVERY PRODUCTS PREVIOUSLY RETRIEVED
//IF THEY ARE INSIDE THE SHOPS FOUND AROUND THE USER LOCATION
var x = 0;
var counter;
var prod;
var test = this.state.data
//FOR EACH PRODUCT...
while(x < this.state.allProducts.length)
{
//THE API IS COMPOSED WITH THE PREVIOUSLY COMPOSED URL
await fetch("https://itxrest.inditex.com/LOMOServiciosRESTCommerce-ws/common/1/stock/campaign/V2021/product/part-number/"+(+this.state.allProducts[x].pn.slice(1,-1))+"?"+urlShops)
.then(response => response.json())
.then((response) => {
counter = 0;
console.log("Product: "+(+this.state.allProducts[x].pn.slice(1,-1)))
if(response.stocks.length > 0)
{
prod = (+this.state.allProducts[x].pn.slice(1,-1))
while(counter < response.stocks.length)
{
//SHOE SIZE = 8 (UK)
if(response.stocks[counter].sizeStocks.some((d) => Number(d.size) == (34+8)))
{
console.log("Available at: "+JSON.stringify(response.stocks[counter].physicalStoreId)+"\n")
test[JSON.stringify(response.stocks[counter].physicalStoreId)].push(prod)
}
else
{
console.log("Product not available with this size")
}
counter++;
}
}
else{
console.log("This product is not available around you")
}
})
.catch((error, response) => {
console.log(error)
console.log(response)
});
x=x+1;
}
console.log("Done with fetching data...")
console.log(test)
//ONCE ALL THE DATA ARE RETRIEVED AND STORED, THE LOADING STATE CHANGES TO "TRUE"
this.setState({isLoaded: true})
}
//GET STORE NEAR THE USER LOCATION
getStoresNearby = () => {
return fetch("https://www.bershka.com/itxrest/2/bam/store/44009506/physical-store?latitude="+this.state.testLat+"&longitude="+this.state.testLong+"&countryCode=GB&max=10&appId=2&languageId=-1")
.then(response => response.json())
.then((response) => {
var x = 0
var dis;
var newShop;
var newRecord;
//EACH STORE COLLECTED WILL BE SAVED WITH NAME AND ID AND COORDINATES INTO THE ARRAY OF DICTIONARY
while(x < response.closerStores.length)
{
//THIS METHOD BELOW WILL CALCULATE THE DISTANCE BETWEEN TWO COORDINATES
//MUST BE UPDATED BY THE USER INPUT MILES RANGE
dis = getDistance(
{latitude: this.state.testLat, longitude: this.state.testLong},
{latitude: Number(JSON.stringify(response.closerStores[x].latitude)), longitude: Number(JSON.stringify(response.closerStores[x].longitude))},
);
//ONLY THE STORES IN THE RANGE WILL BE DISPLAYED
if(dis < 8046)
{
newShop = {
shopId: JSON.stringify(response.closerStores[x].id),
shopName: response.closerStores[x].name,
shopLatitude: Number(JSON.stringify(response.closerStores[x].latitude)),
shopLongitude: Number(JSON.stringify(response.closerStores[x].longitude))
}
newRecord = this.state.shopsCoordinates
newRecord.push(newShop)
this.setState({shopsCoordinates: newRecord})
}
x=x+1
}
this.setState({shopClicked: newRecord[0].shopId})
})
.catch((error) => {
console.error(error);
});
}
//GET ALL PRODUCTS OFF THE WEBSITE
getAllProducts = () => {
return fetch("https://www.bershka.com/itxrest/2/catalog/store/44009506/40259534/category/1010193202/product?languageId=-1")
.then(response => response.json())
.then((response) => {
var x = 0;
var newProduct = [];
var allProd;
while(x < response.products.length)
{
//TESTING WITH SHOE SIZE = 8 (UK)
//TOGLI JSON STRINGIFY SE NON FA
try{
newProduct = {
pn: JSON.stringify(response.products[x].bundleProductSummaries[0].detail.colors[0].sizes[0].partnumber.substring(0,13)),
image: "https://static.bershka.net/4/photos2/"+response.products[x].bundleProductSummaries[0].detail.colors[0].image.url+"_1_1_3.jpg?t="+response.products[x].bundleProductSummaries[0].detail.colors[0].image.timestamp,
price: (response.products[x].bundleProductSummaries[0].detail.colors[0].sizes[0].price)/100
}
allProd = this.state.allProducts
allProd.push(newProduct)
x = x + 1;
}
catch(err){
x = x + 1;
}
}
//THE FOLLOWING FUNCTION WILL REMOVE DUPLICATES
allProd = Array.from(new Set(allProd.map(a => a.pn)))
.map(pn => {
return allProd.find(a => a.pn === pn)
})
//UPDATES THE STATE OF THE ARRAY OF PRODUCTS
this.setState({allProducts: allProd})
})
.catch((error) => {
console.error(error);
});
}
//THE METHOD BELOW WILL RETURN THE CITY AND POSTCODE FROM COORDINATES (UK ONLY)
getPostcodeAndCityFromApi = () => {
//*REMOVED FOR TESTING*
//return fetch('https://api.postcodes.io/postcodes?lon='+this.state.longitude+'&lat='+this.state.latitude+'')
return fetch('https://api.postcodes.io/postcodes?lon='+this.state.testLong+'&lat='+this.state.testLat+'')
.then(response => response.json())
.then((response) => {
//*REMOVED FOR TESTING*
//const postcode = JSON.stringify(response.result[0].postcode);
//const city = JSON.stringify(response.result[0].admin_district);
const testPostCode = JSON.stringify(response.result[0].postcode);
const testCity = JSON.stringify(response.result[0].admin_district);
//*REMOVED FOR TESTING*
//this.setState({postcode, city})
this.setState({testPostCode,testCity})
})
.catch((error) => {
console.error(error);
});
};
render() {
return (
<View style={styles.container}>
<Text style={styles.myText}>You are here !!</Text>
<View style={styles.textWrapper}>
{/* USER LOCATION DISPLAYED */}
<MapView
style={styles.map}
region={{
latitude: this.state.testLat,//Number(this.state.latitude),
longitude: this.state.testLong,//Number(this.state.longitude),
latitudeDelta: 0.0322,
longitudeDelta: 0.0321,
}}
showsUserLocation = {true}
>
{/* SHOPS FOUND NEAR THE USER LOCATION AND DISPLAYED WITH A MARKER */}
{this.state.shopsCoordinates.map((index,i) => (
<Marker
coordinate={{
latitude: index.shopLatitude,
longitude: index.shopLongitude
}}
title={index.shopName}
key={i}
tracksViewChanges={false}
onPress={()=>{this.setState({shopClicked:index.shopId, modalVisible: true, testLat: index.shopLatitude-0.004, testLong: index.shopLongitude, modalTitle: index.shopName})}}
/>
))}
</MapView>
</View>
{/*<Text style={{alignSelf: "center"}}>Postcode: {this.state.postcode} - Borough of: {this.state.city}</Text>*/}
{/* TEST */}
<Text style={{alignSelf: "center"}}>Postcode: {this.state.testPostCode} - Borough of: {this.state.testCity}</Text>
<Text style={{alignSelf: "center"}}>{this.state.isLoaded ? "Data collected successfully" : "Wait for the data to be retrieved"}</Text>
{/* MODAL OPENS WHEN USER CLICKS ON MARKERS */}
<View>
<Modal
animationType="slide"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
this.setState({modalVisible: false, testLat: ((this.state.testLat)+0.004)})
}}
>
{/* COMPONENTS CONTAINING THE PRODUCTS OF THE SHOP CLICKED */}
<View style={{backgroundColor: "white",flex:1, marginTop: 200, borderTopRightRadius: 10,borderTopLeftRadius: 10, borderLeftWidth:3, borderTopWidth:3,borderRightWidth: 3, borderLeftColor:"black",borderTopColor: "black",borderRightColor: "black"}}>
<Header
leftComponent={<TouchableOpacity onPress={() => {
this.setState({modalVisible: false, testLat: ((this.state.testLat)+0.004)})
}}><Text style={{ color:'white'}}>Close</Text></TouchableOpacity>}
centerComponent={{ text: this.state.modalTitle, style: { color: '#fff' } }}
rightComponent={{ text: 'Navigate', style: { color: '#fff' } }}
containerStyle={{
backgroundColor: '#3D6DCC',
justifyContent: 'space-around',
borderTopLeftRadius: 7,
borderTopRightRadius:7,
height:50
}}
/>
{/* THE PRODUCTS ARE DISPLAYED ONLY ONCE THE DATA ARE FETCHED.
IF THE USER CLICKS ON ONE OF THE STORES BEFORE THE DATA ARE FETCHED,
A TEXT "NO PRODUCTS" WILL BE DISPLAYED */}
{this.state.isLoaded ?
<FlatList
data={this.state.dataTest[this.state.shopClicked.slice(1,-1)]}
renderItem={({item}) => {
let obj = this.state.allProducts.find(o => Number(o.pn.slice(1,-1)) === item);
console.log(obj)
return (
<View
style={{
flex: 1,
flexDirection: 'column',
margin: 10,
alignItems: "center"
}}>
<Image
style={styles.tinyLogo}
source={{uri: obj.image}}
/>
<Text>£{obj.price}</Text>
</View>
)
}}
numColumns={3}
keyExtractor={(item, index) => index}
/>
: <Text>No Products</Text>}
</View>
</Modal>
</View>
</View>
)
}
}
//STYLING MUST BE IMPROVED
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
//justifyContent: "center",
//alignItems: "left"
},
textWrapper: {
height: hp('70%'), // 70% of height device screen
width: wp('80%'), // 80% of width device screen
justifyContent: "center",
alignItems: "center"
},
myText: {
fontSize: hp('4%'), // End result looks like the provided UI mockup
alignSelf: "center"
},
map: {
...StyleSheet.absoluteFillObject,
width: Dimensions.get('window').width
},
tinyLogo: {
width: 100,
height: 100,
},
});