-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
161 lines (105 loc) · 4.09 KB
/
app.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
The initial idea of the coding style came from @tyler_potts_
(https://www.youtube.com/watch?v=n4dtwWgRueI)
Later I tried on my using async function own and added a few features to it.
*/
let weather = (function () {
// I don't like writing document.querySelector again & again
let $ = selector => document.querySelector(selector);
const searchbox = $(`.search-box`);
// later we'll manipulate these values
const api = {
key: "75dd19f75c352c261a871ec6545ebdba",
base: "https://cors-anywhere.herokuapp.com/https://api.openweathermap.org/data/2.5/"
}
async function getWeather (query) {
try {
const result = await fetch(`${api.base}weather?q=${query}&units=metric&appid=${api.key}`)
const data = await result.json();
return displayResults(data);
}catch (error) {
console.log(error);
$(`.alert`).textContent = `🛑 Couldn't find "${searchbox.value}"`;
}
};
function displayResults(weather) {
//console.log(weather);
$(`.city`).textContent = `${weather.name}, ${weather.sys.country}`;
$(`.date`).textContent = datetime();
$(`.temp`).innerHTML = `${parseInt(weather.main.temp)}<span>°c</span>`;
$(`.weather`).textContent = weather.weather[0].main;
$(`.hi-low`).textContent = `${parseInt(weather.main.temp_min)}°c / ${parseInt(weather.main.temp_max)}°c`;
};
function datetime () {
let now, year, months, days, date;
now = new Date();
year = now.getFullYear();
date = now.getDate();
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
month = now.getMonth();
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
day = now.getDay();
return `${days[day]} ${date} ${months[month]} ${year}`;
};
const event = function () {
searchbox.addEventListener('keypress', setQuery);
};
function setQuery(evt) {
if (evt.keyCode == 13 || event.which === 13) {
getWeather(searchbox.value);
// alert is blank after the event
$(`.alert`).textContent = ``;
}
}
return {
init: function () {
console.log('App started...');
// focus on the search box
searchbox.focus();
// fire the eventListener
event();
}
};
}) ();
weather.init();
/*
// Tyler's Code
const api = {
key: "75dd19f75c352c261a871ec6545ebdba",
base: "https://cors-anywhere.herokuapp.com/https://api.openweathermap.org/data/2.5/"
}
const searchbox = $(`.search-box`);
searchbox.addEventListener('keypress', setQuery);
function setQuery(evt) {
if (evt.keyCode == 13) {
getResults(searchbox.value);
}
}
function getResults (query) {
fetch(`${api.base}weather?q=${query}&units=metric&appid=${api.key}`)
.then(weather => {
return weather.json();
})
.then(displayResults);
}
function displayResults(weather) {
//console.log(weather);
$(`.city`).textContent = `${weather.name}, ${weather.sys.country}`;
//let now = new Date();
$(`.date`).textContent = datetime();
$(`.temp`).innerHTML = `${parseInt(weather.main.temp)}<span>°c</span>`;
$(`.weather`).textContent = weather.weather[0].main; // remove the backtick
$(`.hi-low`).textContent = `${parseInt(weather.main.temp_min)}°c / ${parseInt(weather.main.temp_max)}°c`;
};
function datetime () {
let now, year, months, days, date;
now = new Date();
year = now.getFullYear();
date = now.getDate();
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
month = now.getMonth();
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
day = now.getDay();
return `${days[day]} ${date} ${months[month]} ${year}`;
};
*/