-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
43 lines (41 loc) · 1.68 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
const getActivity = async e => {
e.preventDefault();
// Get activity type value from select field
const activityType = document.querySelector('#activity-type').value;
try {
// Calling: https://www.boredapi.com/
const response = await fetch(`https://www.boredapi.com/api/activity?type=${activityType}`);
const data = await response.json();
const { activity, participants, type} = data;
// Show activity info
const output = `<div class="card border border-primary my-4 lead">
<div class="card-header bg-dark text-white">
<strong>⚽ Activity</strong>
<button type="button" class="close" aria-label="Close" onclick="removeActivity()">
<span class="text-white" aria-hidden="true">×</span>
</button>
</div>
<div class="card-body">
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Activity: </strong>${activity}</li>
<li class="list-group-item"><strong>Number of participants: </strong>${participants}</li>
<li class="list-group-item"><strong>Type: </strong>${type}</li>
</ul>
</div>
</div>`;
// Insert into output div
document.querySelector('#output').innerHTML = output || '';
} catch (err) {
document.querySelector('#output').innerHTML =
`<div class="alert alert-danger">
Error, please try again.
</div>`;
throw Error(err.statusText);
}
};
// Listen for form submit
document.getElementById('activityForm').addEventListener('submit', getActivity);
// Delete activity box
const removeActivity = () => {
document.querySelector('.card').remove();
};