-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
WeatherForecast.vue
253 lines (246 loc) · 7.16 KB
/
WeatherForecast.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<template>
<div class="weather-forecast">
<template v-if="weatherData.length > 0">
<!-- For each day, show the weather -->
<div
class="weather-day"
v-for="weather in weatherData"
:key="weather.index"
v-tooltip="tooltip(weather.description)"
@click="showMoreInfo(weather.info)"
>
<p class="date">{{ weather.date }}</p>
<p class="description">{{ weather.main }}</p>
<p class="temp">{{ weather.temp }}</p>
<i :class="`owi owi-${weather.icon}`"></i>
</div>
</template>
<!-- Show more details for a Clicked day -->
<div class="details" v-if="showDetails">
<div class="info-wrap" v-for="(section, indx) in moreInfo" :key="indx">
<p class="info-line" v-for="weather in section" :key="weather.label">
<span class="lbl">{{weather.label}}</span>
<span class="val">{{ weather.value }}</span>
</p>
</div>
</div>
<p class="more-details-btn" @click="toggleDetails" v-if="showDetails">
{{ $t('widgets.general.show-less') }}
</p>
</div>
</template>
<script>
import WidgetMixin from '@/mixins/WidgetMixin';
import { capitalize } from '@/utils/MiscHelpers';
import { widgetApiEndpoints } from '@/utils/defaults';
export default {
mixins: [WidgetMixin],
data() {
return {
loading: true,
showDetails: false,
weatherData: [],
moreInfo: [],
};
},
mounted() {
this.checkProps();
},
computed: {
units() {
return this.options.units || 'metric';
},
numDays() {
return this.options.numDays || 6;
},
endpoint() {
const { apiKey, city } = this.options;
const params = `?q=${city}&cnt=${this.numDays}&units=${this.units}&appid=${apiKey}`;
return `${widgetApiEndpoints.weatherForecast}${params}`;
},
tempDisplayUnits() {
switch (this.units) {
case ('metric'): return '°C';
case ('imperial'): return '°F';
default: return '';
}
},
speedDisplayUnits() {
switch (this.units) {
case ('metric'): return 'm/s';
case ('imperial'): return 'mph';
default: return '';
}
},
},
methods: {
/* Adds units symbol to temperature, depending on metric or imperial */
processTemp(temp) {
return `${Math.round(temp)}${this.tempDisplayUnits}`;
},
/* Convert timestamp to textual date, in users local format */
dateFromStamp(timestamp) {
const localFormat = navigator.language;
const dateFormat = { weekday: 'short', day: 'numeric', month: 'short' };
return new Date(timestamp * 1000).toLocaleDateString(localFormat, dateFormat);
},
fetchData() {
this.makeRequest(this.endpoint).then(this.processData);
},
/* Process the results from the Axios request */
processData(dataList) {
const uiWeatherData = [];
dataList.list.forEach((day, index) => {
uiWeatherData.push({
index,
date: this.dateFromStamp(day.dt),
icon: day.weather[0].icon,
main: day.weather[0].main,
description: day.weather[0].description,
temp: this.processTemp(day.main.temp),
info: this.makeWeatherData(day),
});
});
this.weatherData = uiWeatherData;
},
/* Process additional data, needed when user clicks a given day */
makeWeatherData(data) {
return [
[
{ label: 'Min Temp', value: this.processTemp(data.main.temp_min) },
{ label: 'Max Temp', value: this.processTemp(data.main.temp_max) },
{ label: 'Feels Like', value: this.processTemp(data.main.feels_like) },
],
[
{ label: 'Pressure', value: `${data.main.pressure}hPa` },
{ label: 'Humidity', value: `${data.main.humidity}%` },
{ label: 'wind', value: `${data.wind.speed}${this.speedDisplayUnits}` },
{ label: 'clouds', value: `${data.clouds.all}%` },
],
];
},
/* When a day is clicked, then show weather info on the UI */
showMoreInfo(moreInfo) {
if (this.showDetails && JSON.stringify(moreInfo) === JSON.stringify(this.moreInfo)) {
this.showDetails = false;
} else {
this.moreInfo = moreInfo;
this.showDetails = true;
}
},
/* Show/ hide additional weather info */
toggleDetails() {
this.showDetails = !this.showDetails;
},
/* Display weather description and Click for more note on hover */
tooltip(text) {
const content = `${text ? capitalize(text) : ''}\nClick for more Info`;
return { content, trigger: 'hover focus', delay: 250 };
},
/* Validate input props, and print warning if incorrect */
checkProps() {
const ops = this.options;
if (!ops.apiKey) this.error('Missing API key for OpenWeatherMap');
if (!ops.city) this.error('A city name is required to fetch weather');
if (ops.units && ops.units !== 'metric' && ops.units !== 'imperial') {
this.error('Invalid units specified, must be either \'metric\' or \'imperial\'');
}
},
},
};
</script>
<style scoped lang="scss">
@import '@/styles/weather-icons.scss';
p {
color: var(--widget-text-color);
}
.weather-forecast {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
.weather-day {
display: grid;
gap: 0.25rem;
grid-template-columns: repeat(2, minmax(0, 1fr));
width: 30%;
min-width: 6rem;
cursor: pointer;
text-align: center;
padding: 0.5rem 0.25rem;
border: 1px solid transparent;
border-radius: var(--curve-factor);
&:hover {
border: 1px dashed var(--widget-text-color);
}
.date {
grid-column-start: span 2;
opacity: var(--dimming-factor);
margin: 0;
text-decoration: underline;
}
.description {
text-transform: capitalize;
text-align: center;
margin: 0;
}
.owi {
font-size: 1.4rem;
color: var(--widget-text-color);
margin: 0;
}
.temp {
font-size: 1.8rem;
grid-row-start: span 2;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
}
}
// Show more details button
.more-details-btn {
grid-column-start: span 2;
cursor: pointer;
font-size: 0.9rem;
text-align: center;
width: fit-content;
margin: 0.25rem auto;
padding: 0.1rem 0.25rem;
border: 1px solid transparent;
opacity: var(--dimming-factor);
border-radius: var(--curve-factor);
&:hover {
border: 1px solid var(--widget-text-color);
}
&:focus, &:active {
background: var(--widget-text-color);
color: var(--widget-background-color);
}
}
// More weather details table
.details {
width: 100%;
grid-column-start: span 2;
display: flex;
.info-wrap {
display: flex;
flex-direction: column;
width: 100%;
opacity: var(--dimming-factor);
p.info-line {
display: flex;
justify-content: space-between;
margin: 0.1rem 0.5rem;
padding: 0.1rem 0;
color: var(--widget-text-color);
span.lbl {
text-transform: capitalize;
}
&:not(:last-child) {
border-bottom: 1px dashed var(--widget-text-color);
}
}
}
}
}
</style>