-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (105 loc) · 3.85 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
// Select Elements
const iconElement = document.querySelector(".weather-icon");
const tempElement = document.querySelector(".temperature-value p");
const descElement = document.querySelector(".temperature-description p");
const locationElement = document.querySelector(".location p");
const notificationElement = document.querySelector(".notification");
//App data
const weather = {};
weather.temperature = {
unit: 'celsius'
}
//APP CONSTS AND VARIABLES
const kelvin = 273;
//API Key
const key = "82005d27a116c2880c8f0fcb866998a0";
//CHECK GEOLOCATIION SUPPORT IN BROWSER
if('geolocation' in navigator){
navigator.geolocation.getCurrentPosition(setPosition, showError);
}else{
notificationElement.style.display = 'block';
notificationElement.innerHTML = '<p>This browser does not support geolocation</p>';
}
//Change Background
function changeBackground(description){
const pexelKey = '563492ad6f91700001000001f02a163ae3b4454bb48b70c60313943d';
let url = `https://api.pexels.com/v1/search?query=${description}`;
fetch(url, {
headers: {
'Authorization': pexelKey
}
})
.then(res => res.json().then(data => {
console.log(data);
const i = Math.floor(Math.random() * 15);
console.log(i);
const source = data.photos[i].src.large;
document.body.style.background = `url(${source})`;
})
.catch(error => console.log(error)))
.catch(error => console.log(error))
}
//Set User Position
function setPosition(position){
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
getWeather(latitude, longitude);
}
//Show ERROR if there is issue
function showError(error){
notificationElement.style.display = 'block';
notificationElement.innerHTML = `<p>${error.message}</p>`;
}
//GET Weather from API provider
function getWeather(latitude, longitude){
let api = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${key}`;
fetch(api)
.then(function(response){
let data = response.json();
return data;
})
.then(function(data){
weather.temperature.value = Math.floor(data.main.temp - kelvin);
weather.description = data.weather[0].description;
weather.iconId = data.weather[0].icon;
weather.city = data.name;
weather.country = data.sys.country;
})
.then(function(){
displayWeather();
})
.then(function() {
changeBackground(weather.description);
})
}
//Displaying weather in UI
function displayWeather(){
iconElement.innerHTML = `<img src = 'icons/${weather.iconId}.png'>`;
tempElement.innerHTML = `${weather.temperature.value}º <span>C</span>`;
descElement.innerHTML = weather.description;
locationElement.innerHTML = `${weather.city}, ${weather.country}`;
}
//Conversion to Farenheit
function celsiusToFahrenheit(temperature){
return (temperature * 9/5) + 32;
}
// When User clicks on Temperature Element
tempElement.addEventListener("click", function(){
if(weather.temperature.value == undefined) return;
if(weather.temperature.unit === "celsius"){
let fahrenheit = celsiusToFahrenheit(weather.temperature.value);
fahrenheit = Math.floor(fahrenheit);
tempElement.innerHTML = `${fahrenheit}º<span>F</span>`;
weather.temperature.unit = "fahrenheit";
}
else{
tempElement.innerHTML = `${weather.temperature.value}º<span>C</span>`;
weather.temperature.unit = "celsius";
}
});
//background image should change based on the
//weather state. Images for winter, summer,
//clear sky and windy should be toggled. ==== Done
//Let users search for cities and get weather results of various cities. === Not Done
// Allow dark mode toggle === Not Done
//Add glassmorphism === Done