-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (116 loc) · 4.46 KB
/
script.js
File metadata and controls
164 lines (116 loc) · 4.46 KB
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
159
160
161
162
163
164
const searchValue = document.querySelector('.zipSearch');
const skies = document.querySelector('.skies');
document.getElementById('searchForm').addEventListener('submit', function(e) {
e.preventDefault();
let testData = getPrimaryCardData();
console.log(testData)
}, false);
async function getPrimaryCardData(){
let searchZip = searchValue.value;
try{
const firstSearch = await fetch('http://api.openweathermap.org/data/2.5/weather?zip=' + searchZip +',us&APPID=5512e7a00e47ffc14cacc43c7b9a21b8', {mode: 'cors'})
const weatherData = await firstSearch.json();
let longitude= weatherData.coord.lon
let latitude = weatherData.coord.lat
const weeklyData = getWeeklyData(latitude,longitude)
let temp = Math.floor(((weatherData.main.temp)-273.15)*(9/5)+32);
let city = weatherData.name;
updateWeatherStatus(weatherData.weather[0].main, temp, city)
return weeklyData;
} catch (error){
//viewPort.innerHTML = 'Please enter a valid zipcode to search for a locations current weather'
searchValue.value = ''
skies.innerHTML = ''
return error;
}
}
async function getWeeklyData(lat,lon){
try{
const weekSearch = await fetch('https://api.openweathermap.org/data/2.5/onecall?lat='+lat+'&lon='+lon+'&exclude=current,minutely,hourly,alerts&appid=5512e7a00e47ffc14cacc43c7b9a21b8', {mode: 'cors'})
const weatherData = await weekSearch.json();
let weekDisplay = document.querySelector('.weekDisplay');
weekDisplay.innerHTML = ''
for(let i = 1; i < 7; i++){
let temp2 = Math.floor(((weatherData.daily[i].temp.day)-273.15)*(9/5)+32);
updateWeekWeatherStatus(weatherData.daily[i].weather[0].main, temp2)
}
return weatherData;
} catch (error){
searchValue.value = ''
skies.innerHTML = ''
return error;
}
}
function updateWeatherStatus(sky, degree, location){
if(sky == "Clouds"){
let newSpan = document.createElement('span');
createMainWeatherCard(newSpan,degree,location);
newSpan.innerHTML = 'wb_cloudy'
}
if(sky == "Rain"){
let newSpan = document.createElement('span');
createMainWeatherCard(newSpan,degree,location);
newSpan.innerHTML = 'bolt'
}
if(sky == "Clear"){
let newSpan = document.createElement('span');
createMainWeatherCard(newSpan,degree,location);
newSpan.innerHTML = 'brightness_high'
}
if(sky == "Snow"){
let newSpan = document.createElement('span');
createMainWeatherCard(newSpan,degree,location);
newSpan.innerHTML = 'ac_unit'
}
}
function createMainWeatherCard(newSpan, degree, location){
let newDiv = document.createElement('DIV');
newDiv.classList.add("card")
skies.innerHTML = ''
newSpan.classList.add("material-icons")
newSpan.classList.add("md-48")
newSpan.classList.add("icon")
let viewPort = document.createElement('h2');
viewPort.classList.add("weatherData")
viewPort.innerHTML = location + ' ' + degree +"°F";
skies.appendChild(newDiv);
newDiv.appendChild(newSpan);
newDiv.appendChild(viewPort)
}
function updateWeekWeatherStatus(sky, degree){
if(sky == "Clouds"){
let newSpan = document.createElement('span');
createWeekWeatherCard(newSpan,degree);
newSpan.innerHTML = 'wb_cloudy'
}
if(sky == "Rain"){
let newSpan = document.createElement('span');
createWeekWeatherCard(newSpan,degree);
newSpan.innerHTML = 'bolt'
}
if(sky == "Clear"){
let newSpan = document.createElement('span');
createWeekWeatherCard(newSpan,degree);
newSpan.innerHTML = 'brightness_high'
}
if(sky == "Snow"){
let newSpan = document.createElement('span');
createWeekWeatherCard(newSpan, degree);
newSpan.innerHTML = 'ac_unit'
}
}
function createWeekWeatherCard(newSpan,degree){
const weekDisplay = document.querySelector('.weekDisplay');
let newDiv = document.createElement('DIV');
newDiv.classList.add("card")
newDiv.style.textAlign = 'center'
newSpan.classList.add("material-icons")
newSpan.classList.add("md-48")
newSpan.classList.add("icon")
let viewPort = document.createElement('h2');
viewPort.classList.add("weatherData")
viewPort.innerHTML = degree +"°F";
weekDisplay.appendChild(newDiv);
newDiv.appendChild(newSpan);
newDiv.appendChild(viewPort)
}