/
_citycode.vue
61 lines (56 loc) · 1.46 KB
/
_citycode.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
<template>
<div class="container">
<h2>{{ city.title }} 6-Day Weather Forecast</h2>
<table class="forecast">
<tbody>
<tr v-for="w in weather" :key="w.id">
<td>{{ w.applicable_date | formatDate('dddd') }}</td>
<td>{{ w.weather_state_name }}</td>
<td><img class="icon" :src="`/img/weather/${w.weather_state_abbr}.svg`" :alt="w.weather_state_name"></td>
<td>{{ w.the_temp | round }}°</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
head () {
return {
title: `Forecast of ${this.city.title}`
}
},
async asyncData ({ app, error, params, store }) {
const citycode = store.state.cities.find((city) => city.toLowerCase() === params.citycode)
if (!citycode) {
return error({ customMessage: `City « ${params.citycode} » not found...`, statusCode: 404 })
}
store.commit('SET_CITY', citycode.toLowerCase())
const cities = await app.$axios.$get(`/api/location/search/?query=${citycode}`)
const city = cities[0]
const weather = await app.$axios.$get(`/api/location/${city.woeid}/`)
return {
city: city,
weather: weather.consolidated_weather
}
}
}
</script>
<style>
.icon {
width: 32px;
}
.forecast {
width: 100%;
border-collapse: collapse;
}
.forecast tr:nth-child(odd) {
background: #eee;
}
.forecast tr:last-child td {
border-bottom: 1px solid #eee;
}
.forecast td {
padding: 1px 5px;
}
</style>