Skip to content

Commit 3f37941

Browse files
authored
Merge pull request #31 from Wayfinder-CS262-2020/Not-found-fix
Fixed issue when classroom didn't exist
2 parents 644eb83 + ba94877 commit 3f37941

File tree

1 file changed

+148
-134
lines changed

1 file changed

+148
-134
lines changed

screens/mapScreen.js

Lines changed: 148 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState, useEffect } from "react";
2-
import calvinmap from "../assets/calvin-map-01.png";
1+
import React, { useState, useEffect } from 'react';
2+
import calvinmap from '../assets/calvin-map-01.png';
33
import {
44
StyleSheet,
55
View,
@@ -12,10 +12,10 @@ import {
1212
Alert,
1313
Text,
1414
Keyboard,
15-
} from "react-native";
16-
import { Icon } from "react-native-elements";
17-
import ImageZoom from "react-native-image-pan-zoom";
18-
import Fuse from "fuse.js";
15+
} from 'react-native';
16+
import { Icon } from 'react-native-elements';
17+
import ImageZoom from 'react-native-image-pan-zoom';
18+
import Fuse from 'fuse.js';
1919

2020
const imageWidth = 4285;
2121
const imageHeight = 3001;
@@ -35,34 +35,34 @@ export default function mapScreen({ navigation }) {
3535
const [waypointY, setWaypointY] = useState(-100);
3636

3737
// Search and building info
38-
const [searchText, setSearchText] = useState("");
39-
const [buildingName, setBuildingName] = useState("Default");
40-
const [buildingCode, setBuildingCode] = useState("NAN");
38+
const [searchText, setSearchText] = useState('');
39+
const [buildingName, setBuildingName] = useState('Default');
40+
const [buildingCode, setBuildingCode] = useState('NAN');
4141
const [roomNumber, setRoomNumber] = useState();
4242

4343
const debug = false;
4444

4545
// This is what the fuzzy search looks for matches in
4646
const buildings = [
4747
{
48-
name: "Science Building",
49-
code: "SB",
48+
name: 'Science Building',
49+
code: 'SB',
5050
},
5151
{
52-
name: "North Hall",
53-
code: "NH",
52+
name: 'North Hall',
53+
code: 'NH',
5454
},
5555
{
56-
name: "Heimenga Hall",
57-
code: "HH",
56+
name: 'Heimenga Hall',
57+
code: 'HH',
5858
},
5959
{
60-
name: "Devies Hall",
61-
code: "DH",
60+
name: 'Devries Hall',
61+
code: 'DH',
6262
},
6363
{
64-
name: "Spoelhof Center",
65-
code: "SC",
64+
name: 'Spoelhof Center',
65+
code: 'SC',
6666
},
6767
];
6868

@@ -94,12 +94,12 @@ export default function mapScreen({ navigation }) {
9494
// It takes the input data and scrapes everything up until the first number to fuzzy search,
9595
// then it fetches the building + room from the database and passes info to the interior screen if needed.
9696
async function parse(input) {
97-
debug && console.log("------ New Fetch ---------", counter);
97+
debug && console.log('------ New Fetch ---------', counter);
9898
debug && setCounter(counter + 1);
9999

100100
const options = {
101101
includeScore: true,
102-
keys: ["name", "code"],
102+
keys: ['name', 'code'],
103103
threshold: 0.8,
104104
};
105105

@@ -122,21 +122,35 @@ export default function mapScreen({ navigation }) {
122122
// Dynamically (for building/building+room) fetch results from the database
123123
let endURL;
124124
if (room === undefined) {
125-
endURL = "building/" + building;
125+
endURL = 'building/' + building;
126126
} else {
127-
endURL = "room/" + building + "+" + room;
127+
endURL = 'room/' + building + '+' + room;
128128
}
129129

130130
debug && console.log(endURL);
131131

132-
await fetch("https://wayfinder-service.herokuapp.com/" + endURL)
133-
// await fetch(
134-
// 'https://wayfinder-de-sb-coordin-ndr6ow.herokuapp.com/' + endURL
135-
// )
136-
.then((response) => response.json())
132+
// Get the data from the service
133+
await fetch('https://wayfinder-service.herokuapp.com/' + endURL)
134+
.then((response) => {
135+
if (response.ok) {
136+
// Checks if the response is ok
137+
return response;
138+
} else {
139+
return null;
140+
}
141+
}).then((response) => {
142+
if (response) {
143+
return response.json()
144+
} else { return response }
145+
})
137146
.then((json) => {
138-
debug && console.log("JSON Data", json);
139-
setRoomData(json);
147+
if (json) {
148+
debug && console.log('JSON Data', json);
149+
setRoomData(json);
150+
} else {
151+
console.log('No result found');
152+
Alert.alert('Classroom not found!');
153+
}
140154
})
141155
.catch((error) => console.error(error))
142156
.finally(() => setLoading(false));
@@ -213,153 +227,153 @@ export default function mapScreen({ navigation }) {
213227
<ActivityIndicator animating={true} />
214228
</View>
215229
) : (
216-
<View>
217-
{/* Footer for search bar and buttons */}
218-
<View style={styles.footer}>
219-
{/* Search Bar */}
220-
<TextInput
221-
style={styles.searchBar}
222-
placeholder="Enter a classroom..."
223-
placeholderTextColor="#C4C4C4"
224-
onChangeText={(text) => setSearchText(text)}
225-
></TextInput>
226-
227-
{/* Search Button */}
228-
<TouchableOpacity
229-
placeholder="Search"
230-
style={styles.searchButton}
231-
onPress={() =>
232-
searchText === ""
233-
? Alert.alert("Please enter search text!")
234-
: parse(searchText)
235-
}
236-
>
237-
<Icon name="send" />
238-
</TouchableOpacity>
239-
</View>
230+
<View>
231+
{/* Footer for search bar and buttons */}
232+
<View style={styles.footer}>
233+
{/* Search Bar */}
234+
<TextInput
235+
style={styles.searchBar}
236+
placeholder="Enter a classroom..."
237+
placeholderTextColor="#C4C4C4"
238+
onChangeText={(text) => setSearchText(text)}
239+
></TextInput>
240+
241+
{/* Search Button */}
242+
<TouchableOpacity
243+
placeholder="Search"
244+
style={styles.searchButton}
245+
onPress={() =>
246+
searchText === ''
247+
? Alert.alert('Please enter search text!')
248+
: parse(searchText)
249+
}
250+
>
251+
<Icon name="send" />
252+
</TouchableOpacity>
253+
</View>
240254

241-
{/* ImageZoom for the map background */}
242-
<ImageZoom
243-
cropWidth={Dimensions.get("window").width}
244-
cropHeight={Dimensions.get("window").height}
245-
imageWidth={imageWidth}
246-
imageHeight={imageHeight}
247-
panToMove={true}
248-
pinchToZoom={true}
249-
enableCenterFocus={false}
250-
minScale={0.15}
251-
centerOn={{ x: 1100, y: -300, scale: 0.3, duration: 2 }}
252-
>
253-
{/* Main map image */}
254-
<Image style={styles.map} source={calvinmap} />
255-
256-
{/* Waypoint for Searched Waypoint */}
257-
<Pressable
258-
style={[
259-
styles.waypoint,
260-
{
261-
marginTop: waypointY - 64,
262-
marginLeft: waypointX - 40 - buildingName.length,
263-
}, // Waypoint locations offset by icon size
264-
]}
265-
onPress={() =>
266-
navigation.navigate("Interior", {
267-
name: buildingName,
268-
code: buildingCode,
269-
xCoord: roomData.interiorcoordinatesx,
270-
yCoord: roomData.interiorcoordinatesy,
271-
floor: roomData.floornumber,
272-
room: roomNumber,
273-
})
274-
}
255+
{/* ImageZoom for the map background */}
256+
<ImageZoom
257+
cropWidth={Dimensions.get('window').width}
258+
cropHeight={Dimensions.get('window').height}
259+
imageWidth={imageWidth}
260+
imageHeight={imageHeight}
261+
panToMove={true}
262+
pinchToZoom={true}
263+
enableCenterFocus={false}
264+
minScale={0.15}
265+
centerOn={{ x: 1100, y: -300, scale: 0.3, duration: 2 }}
275266
>
276-
<Text style={{ color: "#fff" }}> {buildingName} </Text>
277-
<Icon name="location-on" size={64} color="#F0CB02"></Icon>
278-
</Pressable>
279-
280-
{/* User position (and heading) dot */}
281-
<View
282-
style={[
283-
styles.dot,
284-
{
285-
marginTop: pointY - 10, // pointY offest by image size
286-
marginLeft: pointX - 10, // pointX offest by image size
287-
transform: [{ rotateZ: String(heading) + "deg" }],
288-
},
289-
]}
290-
>
291-
<Image
292-
style={{ width: 20, height: 20 }}
293-
source={require("../assets/wayfinder-logo-yellow.png")}
294-
/>
295-
</View>
296-
</ImageZoom>
297-
</View>
298-
)}
267+
{/* Main map image */}
268+
<Image style={styles.map} source={calvinmap} />
269+
270+
{/* Waypoint for Searched Waypoint */}
271+
<Pressable
272+
style={[
273+
styles.waypoint,
274+
{
275+
marginTop: waypointY - 64,
276+
marginLeft: waypointX - 40 - buildingName.length,
277+
}, // Waypoint locations offset by icon size
278+
]}
279+
onPress={() =>
280+
navigation.navigate('Interior', {
281+
name: buildingName,
282+
code: buildingCode,
283+
xCoord: roomData.interiorcoordinatesx,
284+
yCoord: roomData.interiorcoordinatesy,
285+
floor: roomData.floornumber,
286+
room: roomNumber,
287+
})
288+
}
289+
>
290+
<Text style={{ color: '#fff' }}> {buildingName} </Text>
291+
<Icon name="location-on" size={64} color="#F0CB02"></Icon>
292+
</Pressable>
293+
294+
{/* User position (and heading) dot */}
295+
<View
296+
style={[
297+
styles.dot,
298+
{
299+
marginTop: pointY - 10, // pointY offest by image size
300+
marginLeft: pointX - 10, // pointX offest by image size
301+
transform: [{ rotateZ: String(heading) + 'deg' }],
302+
},
303+
]}
304+
>
305+
<Image
306+
style={{ width: 20, height: 20 }}
307+
source={require('../assets/wayfinder-logo-yellow.png')}
308+
/>
309+
</View>
310+
</ImageZoom>
311+
</View>
312+
)}
299313
</View>
300314
);
301315
}
302316

303317
const styles = StyleSheet.create({
304318
main: {
305319
flex: 1,
306-
flexDirection: "row",
307-
alignContent: "space-around",
308-
backgroundColor: "#121212",
320+
flexDirection: 'row',
321+
alignContent: 'space-around',
322+
backgroundColor: '#121212',
309323
},
310324
imageViewWrapper: {
311325
flex: 1,
312-
resizeMode: "cover",
313-
justifyContent: "center",
314-
width: "100%",
315-
height: "100%",
326+
resizeMode: 'cover',
327+
justifyContent: 'center',
328+
width: '100%',
329+
height: '100%',
316330
},
317331
map: {
318332
width: imageWidth * 1,
319333
height: imageHeight * 1,
320334
zIndex: 0,
321335
},
322336
waypoint: {
323-
position: "absolute",
337+
position: 'absolute',
324338
},
325339
dot: {
326340
width: 20,
327341
height: 20,
328-
position: "absolute",
342+
position: 'absolute',
329343
},
330344
footer: {
331-
backgroundColor: "#2D2D2D",
332-
color: "#2D2D2D",
345+
backgroundColor: '#2D2D2D',
346+
color: '#2D2D2D',
333347
bottom: 0,
334348
zIndex: 5,
335-
position: "absolute",
336-
maxWidth: "100%",
337-
minWidth: "100%",
338-
justifyContent: "flex-start",
339-
flexDirection: "row",
349+
position: 'absolute',
350+
maxWidth: '100%',
351+
minWidth: '100%',
352+
justifyContent: 'flex-start',
353+
flexDirection: 'row',
340354
},
341355
searchBar: {
342356
borderRadius: 35,
343-
borderColor: "#C4C4C4",
344-
backgroundColor: "#2D2D2D",
357+
borderColor: '#C4C4C4',
358+
backgroundColor: '#2D2D2D',
345359
borderWidth: 1,
346360
paddingHorizontal: 30,
347361
maxWidth: 220,
348362
minWidth: 220,
349363
minHeight: 40,
350364
maxHeight: 40,
351365
fontSize: 16,
352-
backgroundColor: "#2D2D2D",
366+
backgroundColor: '#2D2D2D',
353367
marginTop: 25,
354368
marginBottom: 25,
355369
marginHorizontal: 10,
356-
color: "#C4C4C4",
370+
color: '#C4C4C4',
357371
},
358372
searchButton: {
359-
backgroundColor: "#f0cb02",
373+
backgroundColor: '#f0cb02',
360374
borderWidth: 1,
361375
borderRadius: 35,
362-
borderColor: "#f0cb02",
376+
borderColor: '#f0cb02',
363377
maxWidth: 100,
364378
minWidth: 100,
365379
marginTop: 25,
@@ -368,6 +382,6 @@ const styles = StyleSheet.create({
368382
marginHorizontal: 10,
369383
},
370384
waypointMarker: {
371-
color: "#F0CB02",
385+
color: '#F0CB02',
372386
},
373387
});

0 commit comments

Comments
 (0)