-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (82 loc) · 2.93 KB
/
script.js
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
"use strict";
const search = document.querySelector("#search");
const card = document.getElementById("weather-card");
const weatherMeasurementUnit = document.querySelector(
".weather-card__measurement-unit h6"
);
const weatherIcon = document.querySelector(".weather-card__icon img");
const weatherTemperature = document.querySelector(".weather-card__temperature h1");
const weatherDescription = document.querySelector(".weather-card__description h3");
const weatherLocation = document.querySelector(".weather-card__location h4");
const weatherFeelsLike = document.querySelector(".weather-card__feels-like span");
const weatherHumidity = document.querySelector(".weather-card__humidity span");
const apiKey = "72b932e7cec2a6fa6b1c0b4afa0136e5";
let city = "";
// Default Measurement Unit
let measurementUnit = "imperial";
search.addEventListener("keypress", (e) => {
if (e.key === "Enter" && search.value !== "") {
city = search.value;
weatherMeasurementUnit.textContent = measurementUnit;
fetchWeatherDetails(city);
}
});
weatherMeasurementUnit.addEventListener("click", () => {
measurementUnitToggle();
});
const measurementUnitToggle = () => {
if (measurementUnit === "imperial") {
measurementUnit = "metric";
weatherMeasurementUnit.textContent = measurementUnit;
} else {
measurementUnit = "imperial";
weatherMeasurementUnit.textContent = measurementUnit;
}
fetchWeatherDetails(city);
};
const fetchWeatherDetails = async (city) => {
try {
const api = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=${measurementUnit}&appid=${apiKey}`;
const response = await fetch(api);
const data = await response.json();
const weather = {
temperature: data.main.temp,
location: data.name,
lat: data.coord.lat,
lon: data.coord.lon,
country: data.sys.country,
feels_like: data.main.feels_like,
description: data.weather[0].description,
icon: data.weather[0].icon,
humidity: data.main.humidity,
};
formatWeather(weather);
return data;
} catch (error) {
console.log(error);
}
};
const formatWeather = (weather) => {
const {
temperature,
location,
lat,
lon,
country,
description,
feels_like,
icon,
humidity,
} = weather;
const googleMapsUrl = `https://maps.google.com/maps?q=${lat},${lon}`;
// Displays the card after being initially set to none
card.style.display = "grid";
document.body.style.backgroundImage =
"url('https://source.unsplash.com/1920x1080/? " + city + "')";
weatherIcon.src = `https://openweathermap.org/img/wn/${icon}@4x.png`;
weatherLocation.innerHTML = `<a href="${googleMapsUrl}" target="_blank">${location}, ${country}</a>`;
weatherTemperature.textContent = Math.round(`${temperature}`) + "°";
weatherDescription.textContent = description;
weatherFeelsLike.textContent = Math.round(`${feels_like}`) + "°";
weatherHumidity.textContent = `${humidity}%`;
};