Skip to content

Commit 90898ac

Browse files
committed
Add button to get forecast
1 parent ab05791 commit 90898ac

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

dist/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ <h5>Write the location to get the weather</h5>
3333
<form id="searching" action="javascript:void(0);">
3434
<input class="form-class w-50" type="text" id="search" placeholder="Input your search keyword" name="search">
3535
<br><br>
36-
<input type="submit" class="btn btn-warning" id="submit" value="Search!">
36+
<input type="submit" class="btn btn-warning" id="submit" value="Weather">
37+
<input type="submit" class="btn btn-warning" id="submit2" value="Forecast!">
3738
</form>
3839
</div>
3940
</main>

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ let help = helpers();
1010
const submit = document.getElementById('submit');
1111
submit.addEventListener('click', event.getSearch.bind(this,'search'));
1212

13+
const submit2 = document.getElementById('submit2');
14+
submit2.addEventListener('click', event.getForecast);
15+
1316
const search2 = document.getElementById('search2');
1417
search2.addEventListener('click', event.getSearch.bind(this, 'searchBar'));
1518

src/js/events.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ const events = function () {
1818
}
1919
};
2020

21+
async function getForecast(searchBar) {
22+
try {
23+
const value = (document.getElementById('search').value).toLowerCase();
24+
const url = `https://api.openweathermap.org/data/2.5/forecast?q=${value}&units=metric&appid=903507f17d707fecd352d38301efba77`;
25+
const response = await fetch(url, { mode: 'cors' });
26+
const cityData = await response.json();
27+
showFlow(cityData);
28+
} catch (error) {
29+
console.error('Error:', error);
30+
alert('Could not find the location');
31+
}
32+
};
33+
2134
function showFlow(data) {
2235
dom().fillCard(data);
2336
dom().imageSwitch(data);
2437
dom().show();
2538
};
2639

27-
return { getSearch, showFlow, }
40+
return { getSearch, showFlow, getForecast, }
2841
};
2942

3043
export { events }

0 commit comments

Comments
 (0)