-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (62 loc) · 2.48 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
const APIKEY = "98eaf8d3fb414253a82134104221301"
const inputEl = document.getElementById("location-input")
const searchBtnEl = document.querySelector(".search-btn")
const locationBtn = document.querySelector(".location-btn")
const locationEl = document.getElementById("location")
const tempEl = document.getElementById("temp")
const conditionEl = document.getElementById("condition")
const humidityEl = document.getElementById("humidity")
const windEl = document.getElementById("wind")
const condIcon = document.getElementById("condition-icon")
const errorEl = document.querySelector(".error")
const infoEl = document.querySelector(".info-section")
// Fetches weather data from api
async function getWeather(location) {
const response = await fetch(`https://api.weatherapi.com/v1/current.json?key=${APIKEY}&q=${location}&aqi=no1`)
if (response.status !== 200) {
return displayError(await response.json())
} else {
const data = await response.json()
// Stores last entered location to local storage to be retrieved later
localStorage.setItem("location", location)
return displayWeather(data)
}
}
function displayWeather(data) {
const { location, current } = data
const { condition } = current
locationEl.textContent = location.name
tempEl.innerHTML = current.temp_c + "°C"
conditionEl.textContent = condition.text
humidityEl.textContent = `Humidity: ${current.humidity}%`
windEl.textContent = `Wind: ${current.wind_kph}kmp`
condIcon.src = condition.icon
document.body.style.backgroundImage = `url("https://source.unsplash.com/random/1920x1080/?${condition.text}")`
infoEl.style.display = "flex"
errorEl.style.display = "none"
}
function displayError(error) {
const { message } = error.error
errorEl.style.display = "block"
infoEl.style.display = "none"
errorEl.innerHTML = `<h3>${message}</h3>`
}
searchBtnEl.addEventListener("click", () => {
getWeather(inputEl.value)
})
// Retrieves the user location
locationBtn.addEventListener("click", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
getWeather(`${position.coords.latitude}, ${position.coords.longitude}`)
})
}
})
inputEl.addEventListener("keypress", event => {
if (event.key === "Enter") {
event.preventDefault()
getWeather(inputEl.value)
}
})
// Default location or last
getWeather(localStorage.getItem("location") || "chennai")