From 3f8b180553814f0da8ebb16cd0566521e044d7aa Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Fri, 3 Dec 2021 16:56:33 +0000 Subject: [PATCH] :construction: Nearly finished the weather widget --- src/components/Widgets/Weather.vue | 151 ++++++++++++++------------ src/components/Widgets/WidgetBase.vue | 3 + 2 files changed, 87 insertions(+), 67 deletions(-) diff --git a/src/components/Widgets/Weather.vue b/src/components/Widgets/Weather.vue index 02aea987..cb6d2d68 100644 --- a/src/components/Widgets/Weather.vue +++ b/src/components/Widgets/Weather.vue @@ -7,44 +7,18 @@

{{ description }}

- -
-

- Minimum - {{ minTemp }} -

-

- Maximum - {{ maxTemp }} -

-

- Feels Like - {{ feelsLike }} -

-
- -
-

- Wind - {{ wind }} -

-

- Clouds - {{ clouds }} -

-

- Humidity - {{ humidity }} -

-

- Pressure - {{ pressure }} -

-

- Visibility - {{ visibility }} -

+
+
+

+ {{weather.label}} + {{ weather.value }} +

+
+ +

+ {{ showDetails ? 'Show Less' : ' Expand Details' }} +

@@ -63,14 +37,8 @@ export default { icon: null, description: null, temp: null, - minTemp: null, - maxTemp: null, - feelsLike: null, - pressure: null, - visibility: null, - humidity: null, - wind: null, - clouds: null, + showDetails: false, + weatherDetails: [], }; }, computed: { @@ -97,9 +65,11 @@ export default { }, }, methods: { + /* Adds units symbol to temperature, depending on metric or imperial */ processTemp(temp) { return `${Math.round(temp)}${this.tempDisplayUnits}`; }, + /* Fetches the weather from OpenWeatherMap, and processes results */ fetchWeather() { axios.get(this.endpoint) .then((response) => { @@ -107,19 +77,36 @@ export default { this.icon = data.weather[0].icon; this.description = data.weather[0].description; this.temp = this.processTemp(data.main.temp); - this.minTemp = this.processTemp(data.main.temp_min); - this.maxTemp = this.processTemp(data.main.temp_max); - this.feelsLike = this.processTemp(data.main.feels_like); - this.pressure = `${data.main.pressure}hPa`; - this.humidity = `${data.main.humidity}%`; - this.visibility = data.visibility; - this.wind = `${data.wind.speed}${this.speedDisplayUnits}`; - this.clouds = `${data.clouds.all}%`; + if (!this.options.hideDetails) { + this.makeWeatherData(data); + } }) .catch(() => { this.throwError('Failed to fetch weather'); }); }, + /* If showing additional info, then generate this data too */ + makeWeatherData(data) { + this.weatherDetails = [ + [ + { 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: 'visibility', value: data.visibility }, + { label: 'wind', value: `${data.wind.speed}${this.speedDisplayUnits}` }, + { label: 'clouds', value: `${data.clouds.all}%` }, + ], + ]; + }, + /* Show/ hide additional weather info */ + toggleDetails() { + this.showDetails = !this.showDetails; + }, + /* Validate input props, and print warning if incorrect */ checkProps() { const ops = this.options; let valid = true; @@ -137,6 +124,7 @@ export default { } return valid; }, + /* Just outputs an error message */ throwError(error) { ErrorHandler(error); this.error = true; @@ -153,9 +141,14 @@ export default {