-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (75 loc) · 2.66 KB
/
Copy pathscript.js
File metadata and controls
95 lines (75 loc) · 2.66 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
const loca = document.getElementById('location');
const img = document.getElementById('img');
const desc = document.getElementById('desc');
const temperature = document.getElementById('temp')
const form = document.getElementById('search');
const btn = document.getElementById('btn');
const day = document.getElementById('date');
const wi = document.getElementById('wind');
const hum = document.getElementById('hum');
const cld = document.getElementById('cld');
const we = document.getElementById('we');
const api = {
baseQueryUrl: 'http://api.openweathermap.org/data/2.5/weather?q=',
id: id,
metricUnit: 'units=metric'
}
function apiCall(queryValue) {
fetch(`${api.baseQueryUrl}${queryValue}&appid=${api.id}&${api.metricUnit}`)
.then(res => res.json())
.then(data => {
localStorage.setItem('weather', encodeURIComponent(JSON.stringify(data)));
displayWeatherContent(data);
})
.catch(err => console.log(err))
};
window.addEventListener('load', apiCall('lagos'));
function search() {
apiCall(form.value);
}
function keyPress(e) {
if(e.keyCode == 13) {
e.preventDefault();
apiCall(form.value);
}
}
btn.addEventListener('click', search);
form.addEventListener('keypress', keyPress);
function displayWeatherContent(weatherResult) {
const country = weatherResult.sys.country;
const city = weatherResult.name;
const icon = weatherResult.weather[0].icon
const description = weatherResult.weather[0].description;
const temp = weatherResult.main.temp;
const wind = weatherResult.wind.speed;
const humidity = weatherResult.main.humidity;
const cloud = weatherResult.clouds.all;
const weather = weatherResult.weather[0].main;
loca.innerText = `${city}, ${country}`;
img.src = `https://openweathermap.org/img/w/${icon}.png`;
desc.innerText = description;
temperature.innerText = `${Math.round(temp)}°c`;
day.innerText = date();
wi.innerText = `${wind}/sec `;
hum.innerText = `${humidity}%`;
cld.innerText = `${cloud}%`;
we.innerText = `${weather}`;
function date(D, d, m, y){
const date = new Date();
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const months = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
D = days[date.getDay()];
d = date.getDate();
m = months[date.getMonth()];
y = date.getFullYear();
return `${D}, ${d}th ${m} ${y}`;
}
};
if('serviceWorker' in navigator){
window.addEventListener('load', () =>{
navigator.serviceWorker
.register('./sw.js')
.then(reg => console.log())
.catch(err => console.log(`Service worker: Error: ${err}`))
})
};