-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (81 loc) · 2.59 KB
/
index.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
import {
loadingSpinner,
generateLabel,
locationNameLabel,
getLocationName,
postInput,
regex,
} from "./modules/utils.js";
import { getWeatherData } from "./modules/getWeatherData.js";
const submitBtn = document.querySelector("#submit-btn");
function setBackground() {
const time = new Date().getHours();
switch (true) {
case time >= 19 || time < 6:
document.body.background = "../img/night.jpg";
break;
case time >= 6 && time < 11:
document.body.background = "../img/morning.jpg";
break;
case time >= 11 && time < 16:
document.body.background = "../img/afternoon.jpg";
break;
case time >= 16 && time < 19:
document.body.background = "../img/evening.jpg";
break;
}
}
function initGoogleApi() {
const script = document.createElement("script");
script.src = `https://maps.googleapis.com/maps/api/js?key=AIzaSyC-AIwPCYwuJASspWwGkrDqFlVpTAlpMko&libraries=&v=weekly`;
script.defer = true;
document.head.appendChild(script);
}
setBackground();
initGoogleApi();
//Event Listeners to start query
postInput.addEventListener("keyup", (event) =>
event.keyCode === 13 ? setQuery() : null
); //on press enter
submitBtn.addEventListener("click", setQuery); //on submit button click
function setQuery(event) {
//checks if there are warning h2's currently rendered to the DOM & removes them
if (document.querySelector(".warning-label")) {
document.querySelector(".warning-label").remove();
}
if (regex.test(postInput.value)) {
//request lat and lon vals from google api
getLatLngByZipcode(postInput.value);
} else {
generateLabel(
"h2",
document.querySelector("main header"),
"null",
"warning-label"
).innerText = "Sorry, only valid Japan postal codes (Ex. 167-0022).";
}
}
export const getLatLngByZipcode = (zipcode) => {
loadingSpinner.classList.toggle("is-hidden"); // starts loading spinner
const geocoder = new google.maps.Geocoder();
const address = zipcode;
geocoder.geocode({ address: "zipcode " + address }, (results, status) => {
if (status === "OK") {
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();
locationNameLabel.textContent = getLocationName(
results[0].address_components
);
getWeatherData(lat, lng);
} else {
generateLabel(
"h2",
document.querySelector("main header"),
"null",
"warning-label"
).innerText =
"Sorry, either post code does not exist, or there's a connect error.";
loadingSpinner.classList.add("is-hidden");
}
});
};