-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.js
92 lines (79 loc) · 3.01 KB
/
Home.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
import React, { Component } from 'react';
import {withRouter} from 'react-router-dom';
const lurl = "https://developerfunnelrestapi.herokuapp.com/restaurant"
const curl = "https://developerfunnelrestapi.herokuapp.com/restaurant?city="
///class component
class Home extends Component{
constructor(){
super()
this.state ={
location:'',
restaurant:''
}
}
//display of data from api for city names
Cityrender = (data) => {
if(data){
return data.map((item) => {
return(
<option value={item.city}>
{item.city_name}
</option>
)
})
}
}
//display of data from api for restaurant names
Restaurantrender = (data) => {
if(data){
return data.map((item) => {
return(
<option value={item.city}>
{item.name}
</option>
)
})
}
}
ResHandle = (event) => {
console.log(this.props)
this.props.history.push(`/restaurantdetails/${event.target.value}`)
}
// fetching data from api url
Handle = (event) => {
const cityid = event.target.value
const rurl = `${curl}${cityid}`
fetch(rurl,{Method:'GET'})
.then((res) => res.json())
.then((data) => {this.setState({restaurant:data})})
}
render(){
console.log(this.state.location);
return(
<React.Fragment>
<div class="picplace">
<div id="texthead">Find Restaurants in your locations</div>
<div id="selector">
<select id="options" onChange={this.Handle}>
<option>--------------SELECT CITY-----------------------</option>
{this.Cityrender(this.state.location)}
</select>
<select id="options" onChange={this.ResHandle}>
<option>--------------SELECT RESTAURANT-----------------------</option>
{this.Restaurantrender(this.state.restaurant)}
</select>
{/* <input id="inputbox" type="datalist" onChange={this.Handle}placeholder="---------------------Search Restaurants----------------------"/>
{this.Restaurantrender(this.state.restaurant)} */}
</div>
</div>
</React.Fragment>
)
}
//when we want to get city api data when on page load
componentDidMount(){
fetch(lurl,{Method:'GET'})
.then((res) => res.json())
.then((data) => {this.setState({location:data})})
}
}
export default withRouter(Home);