-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchBar.js
121 lines (114 loc) · 3.52 KB
/
SearchBar.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
import React, { useEffect, useState } from 'react';
import {
SafeAreaView,
StyleSheet,
TextInput,
ActivityIndicator,
FlatList,
Text,
View,
Keyboard,
Button,
Touchable,
TouchableHighlight,
KeyboardAvoidingView,
Alert
} from 'react-native';
const baatoToken = 'baato_token';
function SearchBar(props) {
const [text, onChangeText] = useState('');
const [refreshFlatlist, setRefreshFlatList] = useState(false);
// const [isLoading, setLoading] = useState(true);
// const [searchQuery, setSearchQuery] = useState('');
const [result, setResult] = useState([]);
const SearchListRendrer = ({item}) => {
return (<TouchableHighlight onPress={() => {
getItem(item);
setResult(null);
}
} underlayColor="white"><View>
<Text style={styles.itemTitle}>{item.name}</Text>
<Text style={styles.itemSub}>{item.address}</Text><View style={styles.separator} />
</View></TouchableHighlight>);
};
const getItem = (item) => {
props.onData(item);
setResult([]);
setRefreshFlatList(!refreshFlatlist);
}
useEffect(() => {
const delayDebounceFn = setTimeout(() => {
console.log(text)
// Send Axios request here
}, 1000)
if (text.trim !== '') {
fetch(`https://api.baato.io/api/v1/search?key=${baatoToken}&q=${text}&limit=20`)
.then(response => response.json()
.then(data => {
setResult(data.data);
}).catch(error => {
console.error(error);
}))
} else {
console.log("Empty");
setResult([]);
setRefreshFlatList(!refreshFlatlist);
}
return () => clearTimeout(delayDebounceFn)
}, [text]);
return (
<KeyboardAvoidingView style={result != null && result.length > 0 ? styles.detailContainer : styles.container}>
<SafeAreaView>
<TextInput
style={styles.input}
onChangeText={onChangeText}
placeholder='Search here ...'
value={text}
/>
<FlatList
contentContainerStyle={result != null && result.length > 0 ? {paddingBottom: 400} : {paddingBottom: 0}}
data={result}
extraData={refreshFlatlist}
keyExtractor={({ placeId }) => placeId}
renderItem={({ item }) => <SearchListRendrer item={item} />}
// renderItem={({item}) => <View><Text style={styles.itemTitle}>{item.name}</Text>
// <Text style={styles.itemSub}>{item.address}</Text><View style={styles.separator}/></View>}
/>
</SafeAreaView>
</KeyboardAvoidingView>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10
},
container: {
flex: 1,
paddingTop: 22,
paddingBottom: 22
},
detailContainer: {
flexDirection: 'column',
paddingBottom: 20
},
itemTitle: {
fontSize: 16,
fontWeight: 'bold',
padding: 10,
},
itemSub: {
fontSize: 12,
fontWeight: 'normal',
padding: 10,
marginBottom: 10
},
separator: {
flex: 1,
borderWidth: 1,
borderColor: 'grey'
}
});
export default SearchBar;