-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathApp.js
More file actions
119 lines (109 loc) · 3.22 KB
/
App.js
File metadata and controls
119 lines (109 loc) · 3.22 KB
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
import React from "react";
import Photo from "./Photo";
import Info from "./Info";
import Thumbs from "./Thumbs";
import Search from "./Search";
import Header from "./Header";
import { weatherApi, imagesApi } from "./apis";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
currentTypeCity: "",
currentCity: "London",
currentWeather: {
location: "",
description: "",
temp: {
current: "",
min: "",
max: ""
}
},
currentCityImages: [],
currentBackground: {
id: "",
description: "",
color: "",
user: {
name: "",
url: ""
},
image:
"https://images.unsplash.com/photo-1508711046474-2f4c2d3d30ca?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjI4ODk1fQ&s=fd58555505fbe94b05eb33ec5874fc5d"
}
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.receiveCity = this.receiveCity.bind(this);
this.getWeather = this.getWeather.bind(this);
this.getImages = this.getImages.bind(this);
this.handleClick = this.handleClick.bind(this);
}
getWeather(city) {
const { weather } = this.props.config.api;
weatherApi(city, weather.apiKey, weather.url)
.then(reply => this.setState({ currentWeather: reply }))
.catch(error => console.log(error));
}
getImages(city) {
const { unsplash } = this.props.config.api;
imagesApi(city, unsplash.apiKey, unsplash.url)
.then(result =>
this.setState({
currentCityImages: result,
currentBackground: result[0]
})
)
.catch(error => console.log(error));
}
componentDidMount() {
const { weather } = this.props.config.api;
this.getWeather(this.state.currentCity);
this.getImages(this.state.currentCity);
}
receiveCity(currentCityFromSearch) {
this.setState({ currentCity: currentCityFromSearch }, () => {
this.getWeather(this.state.currentCity);
this.getImages(this.state.currentCity);
});
}
handleChange(event) {
this.setState({ currentTypeCity: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
this.receiveCity(this.state.currentTypeCity);
}
handleClick(event, thumbnailData) {
event.preventDefault();
this.setState({ currentBackground: thumbnailData });
}
render() {
return (
<main
className="content"
style={{ backgroundColor: `${this.state.currentBackground.color}` }}
>
<Header />
<Photo data={this.state.currentBackground} />
<Info
description={this.state.currentWeather.description}
temp={this.state.currentWeather.temp}
currentCity={this.state.currentCity}
user={this.state.currentBackground.user}
/>
<Thumbs
photos={this.state.currentCityImages}
handleClick={this.handleClick}
/>
<Search
handleChange={this.handleChange}
handleSubmit={this.handleSubmit}
currentCity={this.state.currentTypeCity}
/>
</main>
);
}
}
export default App;