From 76600e434face2a94554a005e4262a81fc47e937 Mon Sep 17 00:00:00 2001 From: Annd Date: Thu, 19 Oct 2023 15:42:30 -0300 Subject: [PATCH] feat: convert temperature from celsius to fahrenheit --- index.html | 30 +++++++++++++++++------------- script.js | 35 +++++++++++++++++++++++++++++++++-- style.css | 20 ++++++++++++-------- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/index.html b/index.html index 8184cee..acbf9bb 100644 --- a/index.html +++ b/index.html @@ -64,21 +64,25 @@

-
-
+
+

+
+ + °C | + °F + + +

+ 💧 🌬️ + +

+
+
-
-

- °C | - °F -

-

- 💧 🌬️ - -

+
diff --git a/script.js b/script.js index e162547..d36a2ff 100644 --- a/script.js +++ b/script.js @@ -22,6 +22,7 @@ function formatDate(timestamp) { // get temperature by searching for a city function showTemp(response) { + celsiusTemp = response.data.main.temp; let city = (document.querySelector( "#city" ).innerHTML = `${response.data.name}`); @@ -46,12 +47,32 @@ function search(city) { axios.get(apiUrl).then(showTemp); } -function searchCity(event) { +function handleSubmit(event) { event.preventDefault(); let city = document.querySelector("#search").value; search(city); } +// convert temperature from celsius to fahrenheit +function showFahrenheitTemp(event) { + event.preventDefault(); + let fahrenheitTemp = Math.round((celsiusTemp * 9) / 5 + 32); + let temperature = (document.querySelector("#temperature").innerHTML = fahrenheitTemp); + celsius.classList.remove("active"); + fahrenheit.classList.add("active"); + console.log(celsiusTemp); +} + +function showCelsiusTemp(event) { + event.preventDefault(); + console.log(celsiusTemp) + let temperature = (document.querySelector("#temperature").innerHTML = Math.round(celsiusTemp)); + fahrenheit.classList.remove("active"); + celsius.classList.add("active"); + console.log(celsiusTemp); +} + + // get user's current location temperature function getLocation(position) { let apiKey = "b782a5a0cba928a80029c0fd1ea418bd"; @@ -68,8 +89,18 @@ let currentLocal = document .querySelector("#current-btn") .addEventListener("click", getPosition); + let form = document .querySelector(".search-form") - .addEventListener("submit", searchCity); + .addEventListener("submit", handleSubmit); + +let celsiusTemp = null; +console.log(celsiusTemp); + +let fahrenheit = document.querySelector("#fahrenheit"); +fahrenheit.addEventListener("click", showFahrenheitTemp); + +let celsius = document.querySelector("#celsius"); +celsius.addEventListener("click", showCelsiusTemp); search("Sao Paulo"); diff --git a/style.css b/style.css index 53ce7cf..b91a2f4 100644 --- a/style.css +++ b/style.css @@ -42,7 +42,7 @@ h1 { font-weight: 400; } -h2 { +#temperature { font-size: 8.5rem; } @@ -87,13 +87,17 @@ img { font-size: 1.9rem; } +.units a { + cursor: pointer; + color: #E86348; +} + .weather-data { - font-size: 1.3rem; + font-size: 1.4rem; } a { text-decoration: none; - color: inherit; } .btn { @@ -119,11 +123,6 @@ input[type="search"] { font-weight: 700; } -#celsius:hover, -#fahrenheit:hover { - color: #E86348 -} - footer { margin-top: 20px; font-size: 1.5rem; @@ -135,4 +134,9 @@ footer span { footer a { color: #E86348; +} + +.units .active { + cursor: default; + color: #615f5e; } \ No newline at end of file