-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (54 loc) · 1.95 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
// Init Weather Object
const ui = new UI();
// Init LocalStorage Object
const storage = new LS();
// Get Stored Location Data
const weatherLocation = storage.getLocation();
// Init Weather Object
const weather = new Weather(weatherLocation.city, weatherLocation.country, weatherLocation.unit);
// Get Weather on DOM Load
document.addEventListener('DOMContentLoaded', getWeather(weatherLocation.city, weatherLocation.country, weatherLocation.unit));
// Change Location Event
document.getElementById('w-change-btn').addEventListener('click', (e) => {
const city = document.getElementById('city').value;
const country = document.getElementById('country').value;
let unit;
if (document.getElementById('unit').checked) {
unit = document.getElementById('unit').value;
}
if (document.getElementById('unit1').checked) {
unit = document.getElementById('unit1').value;
}
if(city !== '' && country !== ''){
// Change Location
weather.changeLocation(city, country, unit);
// Get Weather
getWeather(city, country, unit);
// Clear Inputs
document.getElementById('city').value = '';
document.getElementById('country').value = '';
document.getElementById('unit').checked = true;
document.getElementById('unit1').checked = false;
// Close Model
$('#locModal').modal('hide');
}else{
// Show erroe message
ui.showAlert('Missing Inputs', 'alert alert-danger');
// Close Model
$('#locModal').modal('hide');
}
});
// Get Weather
function getWeather(city, country, unit){
weather.getWeather()
.then(results => {
// Show UI
ui.paint(results, unit);
// Save Location Data in LocalStorage
storage.setLocation(city, country, unit);
})
.catch(err => {
// Show Error Message
ui.showAlert(err.message, 'alert alert-danger');
});
}