Skip to content

Commit 34dfd67

Browse files
authored
Merge pull request #49 from Uvesh99/ui-error-display
added ui element to show the error
2 parents 53fd993 + 2c99abf commit 34dfd67

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

css/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,9 @@ html {
311311
body {
312312
overflow: auto;
313313
}
314+
.error-message {
315+
color: red;
316+
margin-top: 10px;
317+
font-size: 14px;
318+
display: none;
319+
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h2><i class="fas fa-search-location"></i> Enter a City</h2>
2424
<div id="loading" class="loading" style="display: none;">
2525
<i class="fas fa-spinner fa-spin"></i> Loading...
2626
</div>
27+
<div id="error-message" class="error-message" style="display: none;"></div>
2728
<ul id="suggestions"></ul>
2829
</div>
2930

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const dateTime = document.getElementById("current-date");
66
const suggestionsBox = document.getElementById('suggestions');
77
const suggestions = document.getElementsByClassName('suggestion');
88
const loadingIndicator = document.getElementById("loading");
9+
const errorMessage = document.getElementById("error-message");
910
let forecastInfo = [];
1011
let debounceTimeout;
1112

@@ -17,6 +18,15 @@ function hideLoading() {
1718
loadingIndicator.style.display = "none";
1819
}
1920

21+
function displayErrorMessage(message) {
22+
errorMessage.textContent = message;
23+
errorMessage.style.display = "block"; // Show the error message
24+
}
25+
26+
function hideErrorMessage() {
27+
errorMessage.style.display = "none"; // Hide the error message
28+
}
29+
2030
if (cityInput) {
2131
cityInput.addEventListener('input', async function() {
2232
const query = cityInput.value.trim();
@@ -72,7 +82,7 @@ const fetchData = () => {
7282
hideLoading();
7383
});
7484
} else {
75-
alert("Please enter a city name.");
85+
displayErrorMessage("Please enter a city name.");
7686
}
7787
}
7888

@@ -100,9 +110,10 @@ function getWeatherByCity(city) {
100110
})
101111
.then((data) => {
102112
sessionStorage.setItem("weatherData", JSON.stringify(data));
113+
hideErrorMessage();
103114
})
104115
.catch((error) => {
105-
alert(`${city} not found. Please try again...`);
116+
displayErrorMessage(`${city} not found. Please try again...`);
106117
cityInput.value = "";
107118
hideLoading();
108119
throw error;
@@ -121,11 +132,12 @@ function getForecastByCity(city) {
121132
})
122133
.then((data) => {
123134
sessionStorage.setItem("forecastData", JSON.stringify(data));
135+
hideErrorMessage();
124136
hideLoading();
125137
window.location = "weather_info.html";
126138
})
127139
.catch((error) => {
128-
alert(`${city} not found. Please try again...`);
140+
displayErrorMessage(`${city} not found. Please try again...`);
129141
cityInput.value = "";
130142
hideLoading();
131143
throw error;

0 commit comments

Comments
 (0)