-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
101 lines (94 loc) · 3.06 KB
/
main.ts
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
import {
CurrentWeather,
DailyWeather,
HourlyWeather,
WeatherForecastRes,
} from "../types";
import "./style.css";
import { DAY_FORMATTER, getIconUrl, HOUR_FORMATTER, setValue } from "./utils";
import { getWeather } from "./weather";
const currentIcon: HTMLImageElement = document.querySelector(
"[data-current-icon]"
)!;
const renderCurrentWeather = (current: CurrentWeather) => {
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 dailySection: HTMLElement = document.querySelector("[data-day-section]")!;
const dayCardTemplate = document.getElementById(
"day-card-template"
)! as HTMLTemplateElement;
const renderDailyWeather = (daily: DailyWeather[]) => {
dailySection.innerHTML = "";
daily.forEach(day => {
// Clone the template content
const element = dayCardTemplate.content.cloneNode(
true
) as HTMLDivElement;
setValue("temp", day.maxTemp, { parent: element });
setValue("date", DAY_FORMATTER.format(day.timestamp), {
parent: element,
});
const dayIcon = element.querySelector(
"[data-icon]"
) as HTMLImageElement;
dayIcon.src = getIconUrl(day.iconCode);
dailySection.append(element);
});
};
const hourlySection: HTMLTableSectionElement = document.querySelector(
"[data-hour-section]"
)!;
const hourRowTemplate = document.getElementById(
"hour-row-template"
)! as HTMLTemplateElement;
function renderHourlyWeather(hourly: HourlyWeather[]) {
hourlySection.innerHTML = "";
hourly.forEach(hour => {
const element = hourRowTemplate.content.cloneNode(true) as HTMLElement;
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,
});
const hourIcon: HTMLImageElement =
element.querySelector("[data-icon]")!;
hourIcon.src = getIconUrl(hour.iconCode);
hourlySection.append(element);
});
}
const renderWeather = ({ current, daily, hourly }: WeatherForecastRes) => {
renderCurrentWeather(current);
renderDailyWeather(daily);
renderHourlyWeather(hourly);
document.body.classList.remove("blurred");
};
const positionSuccess = ({ coords }: GeolocationPosition) => {
getWeather(
coords.latitude,
coords.longitude,
Intl.DateTimeFormat().resolvedOptions().timeZone
)
.then(renderWeather)
.catch(err => {
alert("Error getting weather data: ");
console.log(err);
});
};
navigator.geolocation.getCurrentPosition(positionSuccess, error => {
alert(
"There was an error getting your location. Please allow us to use your location and refresh the page."
);
console.log(error);
});