-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwelcome.js
More file actions
151 lines (129 loc) · 3.92 KB
/
welcome.js
File metadata and controls
151 lines (129 loc) · 3.92 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
// Here I Creat The Variable Of Api
const weatherApi = {
key: "fc224f111456a84b1a97e566bf1abc40",
baseUrl: "https://api.openweathermap.org/data/2.5/weather",
};
firebase.auth().onAuthStateChanged((user) => {
if (user) {
var ref = firebase.database().ref("Users");
// ref.once('value', function (snapshot) {
// console.log(snapshot.val());
// });
ref
.orderByChild("email")
.equalTo(user.email)
.on("child_added", function (snapshot) {
var city_name = snapshot.val().city_name;
console.log(city_name);
var country_name = snapshot.val().country_name;
console.log(country_name);
getWeatherReport(city_name);
// Get Weather Report
function getWeatherReport(city) {
fetch(
`${weatherApi.baseUrl}?q=${city}&appid=${weatherApi.key}&units=metric`
)
.then((weather) => {
return weather.json();
})
.then(showWeatherReport)
.catch((error) => {
document.querySelector(".weather-body").style.display = "none";
Swal.fire({
position: "center",
icon: "error",
title: "Please Enter Correct City And Country",
showConfirmButton: true,
timer: 8000,
});
console.log("nooo");
});
}
// Show Weather Report
function showWeatherReport(weather) {
console.log(weather);
let city = document.getElementById("city");
city.innerText = `${weather.name}, ${weather.sys.country}`;
let temperature = document.getElementById("temp");
temperature.innerHTML = `${Math.round(weather.main.temp)}°C`;
let minMaxTemp = document.getElementById("min-max");
minMaxTemp.innerHTML = `${Math.floor(
weather.main.temp_min
)}°C (min)/ ${Math.ceil(weather.main.temp_max)}°C (max) `;
let weatherType = document.getElementById("weather");
weatherType.innerText = `${weather.weather[0].main}`;
let date = document.getElementById("date");
let todayDate = new Date();
date.innerText = dateManage(todayDate);
if (weatherType.textContent == "Clear") {
document.body.style.backgroundImage = "url('images/clear.jpg')";
} else if (weatherType.textContent == "Clouds") {
document.body.style.backgroundImage = "url('images/cloud.jpg')";
} else if (weatherType.textContent == "Haze") {
document.body.style.backgroundImage = "url('images/cloud.jpg')";
} else if (weatherType.textContent == "Rain") {
document.body.style.backgroundImage = "url('images/rain.jpg')";
} else if (weatherType.textContent == "Snow") {
document.body.style.backgroundImage = "url('images/snow.jpg')";
} else if (weatherType.textContent == "Thunderstorm") {
document.body.style.backgroundImage = "url('images/thunderstorm.jpg')";
}
}
// Date manage
function dateManage(dateArg) {
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let year = dateArg.getFullYear();
let month = months[dateArg.getMonth()];
let date = dateArg.getDate();
let day = days[dateArg.getDay()];
return `${date} ${month} (${day}), ${year}`;
}
});
}
});
var logout = document.getElementById("logout");
logout.addEventListener("click", (e) => {
e.preventDefault();
firebase
.auth()
.signOut()
.then(() => {
location.reload("index.html");
});
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log(user.email + " is logged in!");
} else {
window.location.href = "index.html";
}
});
// Here The get full object value of database.
// ref
// .orderByChild('email')
// .equalTo(user.email)
// .once('value', function (snapshot) {
// console.log(snapshot.val());
// });