-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (94 loc) · 4.28 KB
/
script.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
//when user clicks the search button, append city to list group
$('.searchButton').on('click', function(){
let location = $('.search-city').val();
getCurrentWeather(location);
appendWeather(location);
localStorage.setItem('city', location);
})
//append the city to the left side of the page as a previous search
const appendWeather = location => {
//create new list item for city
let li = document.createElement('BUTTON');
li.setAttribute('class', 'list-group-item past-city');
li.innerHTML = location;
//when user clicks the city, they will revisit previous search history
li.addEventListener('click', function(){
getCurrentWeather(location);
})
//adds most recently searched location above older searches
$('.city-list').prepend(li);
}
//when user searches for city, ajax calls for current and future conditions of the city
const getCurrentWeather = location => {
let currentDate = moment().format('L');
$('#city-name').text(`${location} ${currentDate}`);
//set apiKey
const apiKey = 'eb064f8519bae71185dae0cf9c297178'
//create url with location and apiKey
const queryURL = `https://api.openweathermap.org/data/2.5/weather?q=${location}&units=imperial&appid=${apiKey}`;
//ajax call
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
let lat = response.coord.lat;
let lon = response.coord.lon;
getWeekForecast(lat, lon);
//sets text in card body to display temp, humidity, and wind speed for main card
$('#temperature').text(`Temperature: ${Math.floor(response.main.temp)} F`);
$('#humidity').text(`Humidity: ${response.main.humidity} %`);
$('#wind').text(`Wind Speed: ${response.wind.speed} MPH`);
});
}
//user presented with city name, date, icone representation of weather conditions
//uv inex displays if weather is favorable, m,oderate or sever
//five day forecast displays the date, icon of weather condition
const getWeekForecast = (lat, lon, location) => {
console.log(location);
//set apiKey
const apiKey = 'eb064f8519bae71185dae0cf9c297178'
//create url with location and apiKey using onecall API
const queryURL = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&units=imperial&appid=${apiKey}&cnt=5`;
//ajax call
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
console.log(response);
let forecasts = response.daily;
//clears forecast row
$('.forecast-row').empty();
forecasts.forEach(forecast => {
let card = document.createElement('div');
card.setAttribute('class', 'card text-white bg-dark mb-3');
let header = document.createElement('h5');
header.setAttribute('class', 'card-header');
header.textContent = forecast.weather[0].main;
let icon = document.createElement('img');
icon.setAttribute('src', `http://openweathermap.org/img/wn/${forecast.weather[0].icon}@2x.png`)
header.append(icon);
let cardBody = document.createElement('div');
cardBody.setAttribute('class', 'card-body');
let temperature = document.createElement('h5');
temperature.setAttribute('class', 'card-title');
temperature.textContent = `Temp: ${Math.floor(forecast.temp.day)} F`;
let humidity = document.createElement('h5');
humidity.setAttribute('class', 'card-title')
humidity.textContent = `Humidity: ${forecast.humidity} %`;
cardBody.append(temperature, humidity);
card.append(header, cardBody);
$('.forecast-row').append(card);
});
});
}
//when open weather dashboard, last searched city forecast is displayed
const reloadForecast = () => {
let lastCity = localStorage.getItem('city');
if (lastCity === null){
return;
} else {
getCurrentWeather(lastCity);
appendWeather(lastCity);
}
}
reloadForecast();