-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.js
83 lines (74 loc) · 2.9 KB
/
main.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
import "./style.css"
import { getWeather } from "./weather"
import { ICON_MAP } from "./iconMap"
navigator.geolocation.getCurrentPosition(positionSuccess, positionError)
function positionSuccess({ coords }) {
getWeather(
coords.latitude,
coords.longitude,
Intl.DateTimeFormat().resolvedOptions().timeZone
)
.then(renderWeather)
.catch(e => {
console.error(e)
alert("Error getting weather.")
})
}
function positionError() {
alert(
"There was an error getting your location. Please allow us to use your location and refresh the page."
)
}
function renderWeather({ current, daily, hourly }) {
renderCurrentWeather(current)
renderDailyWeather(daily)
renderHourlyWeather(hourly)
document.body.classList.remove("blurred")
}
function setValue(selector, value, { parent = document } = {}) {
parent.querySelector(`[data-${selector}]`).textContent = value
}
function getIconUrl(iconCode) {
return `icons/${ICON_MAP.get(iconCode)}.svg`
}
const currentIcon = document.querySelector("[data-current-icon]")
function renderCurrentWeather(current) {
currentIcon.src = getIconUrl(current.iconCode)
setValue("current-temp", current.currentTemp)
setValue("current-high", current.highTemp)
setValue("current-low", current.lowTemp)
setValue("current-fl-high", current.highFeelsLike)
setValue("current-fl-low", current.lowFeelsLike)
setValue("current-wind", current.windSpeed)
setValue("current-precip", current.precip)
}
const DAY_FORMATTER = new Intl.DateTimeFormat(undefined, { weekday: "long" })
const dailySection = document.querySelector("[data-day-section]")
const dayCardTemplate = document.getElementById("day-card-template")
function renderDailyWeather(daily) {
dailySection.innerHTML = ""
daily.forEach(day => {
const element = dayCardTemplate.content.cloneNode(true)
setValue("temp", day.maxTemp, { parent: element })
setValue("date", DAY_FORMATTER.format(day.timestamp), { parent: element })
element.querySelector("[data-icon]").src = getIconUrl(day.iconCode)
dailySection.append(element)
})
}
const HOUR_FORMATTER = new Intl.DateTimeFormat(undefined, { hour: "numeric" })
const hourlySection = document.querySelector("[data-hour-section]")
const hourRowTemplate = document.getElementById("hour-row-template")
function renderHourlyWeather(hourly) {
hourlySection.innerHTML = ""
hourly.forEach(hour => {
const element = hourRowTemplate.content.cloneNode(true)
setValue("temp", hour.temp, { parent: element })
setValue("fl-temp", hour.feelsLike, { parent: element })
setValue("wind", hour.windSpeed, { parent: element })
setValue("precip", hour.precip, { parent: element })
setValue("day", DAY_FORMATTER.format(hour.timestamp), { parent: element })
setValue("time", HOUR_FORMATTER.format(hour.timestamp), { parent: element })
element.querySelector("[data-icon]").src = getIconUrl(hour.iconCode)
hourlySection.append(element)
})
}