-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
201 lines (179 loc) · 7.24 KB
/
main.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const config = {
cUrl: "https://api.countrystatecity.in/v1/countries",
cKey: "QUlGamNYN1dUblVvSENNZlpTdjRXOTBKWUxlNmk5WTRPM29ZZDlJbA==",
wUrl: "https://api.openweathermap.org/data/2.5/",
wKey: "4bf478b2a273f25aa04e3e46c41a039e",
};
// get countries
const getCountries = async (fieldName, ...args) => {
let apiEndPoint;
switch (fieldName) {
case "countries":
apiEndPoint = config.cUrl;
break;
case "states":
apiEndPoint = `${config.cUrl}/${args[0]}/states`;
break;
case "cities":
apiEndPoint = `${config.cUrl}/${args[0]}/states/${args[1]}/cities`;
//${config.url}/IN/states/UP/cities
default:
}
const response = await fetch(apiEndPoint, {
headers: {
"X-CSCAPI-KEY": config.cKey
},
});
if (response.status != 200) {
throw new Error(`Something went wrong, status code: ${response.status}`);
}
const countries = await response.json();
return countries;
};
const getWeather = async (cityName, ccode, units = "metric") => {
const apiEndPoint = `${config.wUrl}weather?q=${cityName},${ccode.toLowerCase()}&APPID=${config.wKey}&units=${units}`;
console.log(apiEndPoint);
try {
const response = await fetch(apiEndPoint);
if (response.status != 200) {
if (response.status == 404) {
weatherDiv.innerHTML = `<div class="alert-danger">
<h3>Oops! No data available.</h3>
</div>`;
} else {
throw new Error(`Something went wrong, status code: ${response.status}`);
}
}
const weather = await response.json();
return weather;
} catch (error) {
console.log(error);
}
};
const getDateTime = (unixTimeStamp) => {
const millisecond = unixTimeStamp * 1000;
const dateObject = new Date(millisecond);
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const humanDateFormate = dateObject.toLocaleDateString("en-US", options);
return humanDateFormate;
};
const tempCard = (val, unit = "cel") => {
const flag = unit == "far" ? "°F" : "°C";
return `<div id="temp-card">
<h6 class="card-subtitle mb2 ${unit}">${val.temp}</h6>
<p class="card-text">Feels Like:${val.temp} ${flag}</p>
<p class="card-text">Max:${val.temp_max} ${flag}, Min:${val.temp_min} ${flag}</p>
</div>`;
}
const displayWeather = (data) => {
const weatherWidget = `<div class="card">
<div class ="card-body">
<!-- A card is a flexible and extensible content container. It includes options for headers and footers, a wide variety of content, contextual background colors, and powerful display options -->
<div class="card-body">
<!-- The building block of a card is the .card-body. Use it whenever you need a padded section within a card. -->
<h5 class="card-title">
${data.name},${data.sys.country} <span class="float-end units"><a href="#" class="unitlink active" data-unit="cel">°C</a> | <a href="#" data-unit="far" class="unitlink">°F</a></span>
</h5>
<p>${getDateTime(data.dt)}</p>
<div id="tempcard">${tempCard(data.main)}</div>
${data.weather.map(
(w)=>` <div id="img-container">${w.main}<img src="https://openweathermap.org/img/wn/${w.icon}.png"/></div>
<p>${w.description}</p>`).join("\n")}
</div>
</div>`;
weatherDiv.innerHTML = weatherWidget;
}
const getLoader = () => {
return `<div class="spinner-grow text-info" role="status">
<span class="visually-hidden">Loading...</span>
</div>`;
};
const countriesListDropDown = document.querySelector("#countrylist");
const statesListDropDown = document.querySelector("#statelist");
const citiesListDropDown = document.querySelector("#citylist");
const weatherDiv = document.querySelector("#weatherwidget")
// on content load
document.addEventListener("DOMContentLoaded", async () => {
const countries = await getCountries("countries");
// console.log(countries);
let countriesOptions = "";
if (countries) {
countriesOptions += `<option value="">Country</option>`;
countries.forEach((country) => {
countriesOptions += `<option value="${country.iso2}">${country.name}</option>`;
});
countriesListDropDown.innerHTML = countriesOptions;
}
// List states
countriesListDropDown.addEventListener("change", async function() {
const selectedCountryCode = this.value;
//console.log('selectedCountryCode',selectedCountryCode);
const states = await getCountries("states", selectedCountryCode);
//console.log(states);
let statesOptions = "";
if (states) {
statesOptions += `<option value="">State</option>`;
states.forEach((state) => {
statesOptions += `<option value="${state.iso2}">${state.name}</option>`;
});
statesListDropDown.innerHTML = statesOptions;
statesListDropDown.disabled = false;
citiesListDropDown.innerHTML = "";
//as we have disabled in htm so for enabling we will use this line
}
});
//list cities
statesListDropDown.addEventListener("change", async function() {
const selectedCountryCode = countriesListDropDown.value;
const selectedStateCode = this.value;
//console.log('selectedCountryCode',selectedCountryCode);
const cities = await getCountries("cities", selectedCountryCode, selectedStateCode);
console.log(cities);
let citiesOptions = "";
if (cities) {
citiesOptions += `<option value="">City</option>`;
cities.forEach((city) => {
citiesOptions += `<option value="${city.name}">${city.name}</option>`;
});
citiesListDropDown.innerHTML = citiesOptions;
citiesListDropDown.disabled = false;
}
});
citiesListDropDown.addEventListener("change", async function() {
const selectedCountryCode = countriesListDropDown.value;
//console.log(selectedCountryCode);
const selectedCity = this.value;
// console.log(selectedCity);
weatherDiv.innerHTML = getLoader();
const weatherInfo = await getWeather(selectedCity, selectedCountryCode);
// console.log(weatherInfo);
displayWeather(weatherInfo);
});
//change unit by clicking on screen
document.addEventListener("click", async (e) => {
if (e.target.classList.contains("unitlink")) {
e.preventDefault();
const unitValue = e.target.getAttribute("data-unit");
const selectedCountryCode = countriesListDropDown.value;
const selectedCity = citiesListDropDown.value;
const unitFlag = unitValue == "far" ? "imperial" : "metric";
const weatherInfo = await getWeather(
selectedCity,
selectedCountryCode,
unitFlag
);
const weatherTemp = tempCard(weatherInfo.main, unitValue);
document.querySelector("#tempcard").innerHTML = weatherTemp;
// active unit
document.querySelectorAll(".unitlink").forEach((link) => {
link.classList.remove("active");
});
e.target.classList.add("active");
}
});
});