/
Search.tsx
135 lines (125 loc) · 3.21 KB
/
Search.tsx
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
import {
MappedinLocation,
MapViewStore,
MiMapView,
TGetVenueOptions,
} from '@mappedin/react-native-sdk';
import React from 'react';
import {
FlatList,
ListRenderItem,
Pressable,
SafeAreaView,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
const venueOptions: TGetVenueOptions = {
venue: 'mappedin-demo-mall',
clientId: '5eab30aa91b055001a68e996',
clientSecret: 'RJyRXKcryCMy4erZqqCbuB1NbR66QTGNXVE0x3Pg6oCIlUR1',
};
const SearchResultFlatList = ({
locations,
onPress,
}: {
locations: MappedinLocation[];
onPress: (location: MappedinLocation[]) => void;
}) => {
const renderItem: ListRenderItem<MappedinLocation> = ({item}) => (
<Pressable style={styles.item} onPress={() => onPress(item)}>
<Text style={styles.name}>{item.name}</Text>
</Pressable>
);
return (
<FlatList
data={locations}
renderItem={renderItem}
keyExtractor={(item: MappedinLocation) => item.id}
/>
);
};
const Search = () => {
const mapView = React.useRef<MapViewStore>(null);
const [searchQuery, setSearchQuery] = React.useState<string>('');
const [results, setResults] = React.useState<MappedinLocation[]>([]);
React.useEffect(() => {
const generateSearchResults = async () => {
const searchResults = await mapView?.current?.OfflineSearch.search(
searchQuery,
);
setResults(
searchResults
?.filter(r => r.type === 'MappedinLocation')
.map(r => r.object as MappedinLocation)
.filter(l => l.type === 'tenant') || [],
);
};
generateSearchResults();
}, [searchQuery]);
const handlePress = async (location: MappedinLocation) => {
const floor = location.polygons[0].map;
//Switch to the appropriate floor if it is not currently displayed.
if (floor.id !== mapView.current?.currentMap?.id) {
await mapView.current?.setMap(floor);
}
mapView.current?.clearAllPolygonColors();
mapView.current?.setPolygonColor(location.polygons[0], 'red');
mapView.current?.Camera.focusOn({polygons: location.polygons});
};
return (
<SafeAreaView style={styles.fullSafeAreaView}>
<View style={styles.searchView}>
<TextInput
onChangeText={input => setSearchQuery(input)}
value={searchQuery}
style={styles.input}
placeholder="Search..."
/>
<SearchResultFlatList locations={results} onPress={handlePress} />
</View>
<MiMapView
style={styles.mapView}
key="mappedin"
ref={mapView}
onFirstMapLoaded={() => {
setResults(
mapView.current?.venueData?.locations?.filter(
(location: MappedinLocation) => location.type === 'tenant',
) || [],
);
}}
options={venueOptions}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
item: {
flex: 1,
backgroundColor: 'whitesmoke',
padding: 16,
marginVertical: 4,
},
name: {
fontSize: 16,
},
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
fullSafeAreaView: {
flex: 1,
backgroundColor: 'white',
},
searchView: {
flex: 1,
},
mapView: {
flex: 1,
},
});
export default Search;