-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
App.vue
64 lines (61 loc) · 1.56 KB
/
App.vue
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
<template>
<div id="app">
<WeatherSearchBar @search="handleSearch"/>
<WeatherDisplay :weatherData="weatherData"/>
<FavoritesList :favorites="favorites" @select="handleFavoriteSelect"/>
</div>
</template>
<script>
import WeatherSearchBar from './components/WeatherSearchBar.vue';
import WeatherDisplay from './components/WeatherDisplay.vue';
import FavoritesList from './components/FavoritesList.vue';
export default {
name: 'App',
components: {
WeatherSearchBar,
WeatherDisplay,
FavoritesList
},
data() {
return {
weatherData: null,
favorites: this.loadFavorites()
};
},
methods: {
handleSearch(city) {
let url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.VUE_APP_API_KEY}&units=imperial`;
fetch(url)
.then(response => response.json())
.then(data => {
this.weatherData = data;
})
.catch(error => {
console.log(error);
});
},
handleFavoriteSelect(city) {
let url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.VUE_APP_API_KEY}&units=imperial`;
fetch(url)
.then(response => response.json())
.then(data => {
this.weatherData = data;
})
.catch(error => {
console.log(error);
});
},
loadFavorites() {
let favorites = localStorage.getItem('favorites');
if (favorites) {
return JSON.parse(favorites);
}
else {
return [];
}
}
}
};
</script>
<style>
</style>