-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchPage.js
152 lines (137 loc) · 4.05 KB
/
SearchPage.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
'use strict';
import React, { Component } from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
Button,
ActivityIndicator,
Image,
} from 'react-native';
type Props = {};
function urlForQueyAndPage(key, value, pageNumber){
const data = {
country: 'uk',
pretty:'1',
encoding:'json',
listing_type: 'buy',
action: 'search_listings',
page: pageNumber,
};
data[key]=value;
const queryString = Object.keys(data)
.map(key=>key+'='+encodeURIComponent(data[key]))
.join('&');
return 'https://api.nestoria.co.uk/api?'+ queryString;
}
export default class SearchPage extends Component<Props>{
static navigationOptions = {
title: 'Property Finder',
};
constructor(props){
super(props);
this.state ={
searchString: 'london',
isLoading: false,
message:'',
};
}
onSearchTextChanged=(event)=>{
console.log("search changed");
this.setState({searchString: event.nativeEvent.text});
console.log("Current: "+ this.state.searchString+ ', Next: '+event.nativeEvent.text);
}
executeQuery=(query)=>{
console.log(query);
this.setState({isLoading:true})
fetch(query)
.then(response=> response.json())
.then(json => this.handleResponse(json.response))
.catch(error=>
this.setState({
isLoading: false,
message: 'Something bad happened' + error
}));
}
handleResponse = (response)=>{
this.setState({isLoading: false, message:''});
if(response.application_response_code.substr(0,1)==='1'){
this.props.navigation.navigate(
'Results',{listings: response.listings});
// console.log('Properties found: '+ response.listings.length);
}else{
this.setState({message:'Location not recognized; please try again.'})
}
};
onSearchPressed=()=>{
const query = urlForQueyAndPage('place_name', this.state.searchString,1);
this.executeQuery(query);
}
render() {
// console.log("SearchPage.render");
const spinner = this.state.isLoading?<ActivityIndicator size='large'/>:null;
return (
<View style={styles.container}>
<Text style={styles.description}>
Search for houses to buy!
</Text>
<Text style={styles.description}>
Search by place name or postcode.
</Text>
<View style={styles.flowRight}>
<TextInput
underlineColorAndroid={'transparent'}
style={styles.searchInput}
value={this.state.searchString}
placeholder='Search via name or postcode'
onChange={this.onSearchTextChanged} />
<Button
onPress={this.onSearchPressed}
color='#48BBEC'
title='Go'
/>
</View>
<Image source ={require('./Resources/house.png')}
style={styles.image} />
{spinner}
<Text style={styles.description}>
{this.state.message}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
description: {
marginBottom: 20,
fontSize: 18,
textAlign: 'center',
color: '#656565'
},
container: {
padding: 30,
marginTop: 65,
alignItems: 'center'
},
flowRight: {
flexDirection: 'row',
alignItems: 'center',
alignSelf: 'stretch',
},
searchInput: {
height: 36,
padding: 4,
marginRight: 5,
flexGrow: 1,
fontSize: 18,
borderWidth: 1,
borderColor: '#48BBEC',
borderRadius: 8,
color: '#48BBEC',
},
image:{
width: 217,
height: 138,
},
});