- fetchStatus: async function () {
- const url = `https://api.openweathermap.org/data/2.5/weather?q=${this.item.location}&appid=${this.item.apiKey}&units=${this.item.units}`;
- this.api = await fetch(url)
- .then((response) => response.json())
- .catch((e) => {
- this.error = true;
- console.log(e)
+ fetchWeather: async function () {
+ let locationQuery;
+
+ // Use location ID if specified, otherwise retrieve value from location (name).
+ if (this.item.locationId) {
+ locationQuery = `id=${this.item.locationId}`;
+ } else {
+ locationQuery = `q=${this.item.location}`;
+ }
+
+ const apiKey = this.item.apikey || this.item.apiKey;
+ const url = `https://api.openweathermap.org/data/2.5/weather?${locationQuery}&appid=${apiKey}&units=${this.item.units}`;
+ fetch(url)
+ .then((response) => {
+ if (!response.ok) {
+ throw Error(response.statusText);
+ }
+ return response.json();
+ })
+ .then((weather) => {
+ this.id = weather.id;
+ this.name = weather.name;
+ this.temp = parseInt(weather.main.temp).toFixed(1);
+ this.icon = weather.weather[0].icon;
+ this.conditions = weather.weather[0].description;
+ })
+ .catch((e) => {
+ console.log(e);
+ this.error = true;