-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (72 loc) · 2.86 KB
/
script.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
const breweryList = document.getElementById('breweryList');
const searchInput = document.getElementById('searchInput');
async function getBreweries() {
const response = await fetch('https://api.openbrewerydb.org/breweries');
const data = await response.json();
return data;
}
async function searchBreweries(query) {
const response = await fetch(`https://api.openbrewerydb.org/breweries/search?query=${query}`);
const data = await response.json();
return data;
}
async function displayBreweries() {
const breweries = await getBreweries();
breweryList.innerHTML = '';
breweries.forEach((brewery) => {
const breweryDiv = document.createElement('div');
breweryDiv.classList.add('brewery');
const name = document.createElement('h2');
name.innerText = brewery.name;
const type = document.createElement('p');
type.innerText = `Type: ${brewery.brewery_type}`;
const address = document.createElement('p');
address.innerText = `Address: ${brewery.street}, ${brewery.city}, ${brewery.state}, ${brewery.postal_code}`;
const website = document.createElement('a');
website.href = brewery.website_url;
website.innerText = 'Website';
const phone = document.createElement('p');
phone.innerText = `Phone: ${brewery.phone}`;
breweryDiv.appendChild(name);
breweryDiv.appendChild(type);
breweryDiv.appendChild(address);
breweryDiv.appendChild(website);
breweryDiv.appendChild(phone);
breweryList.appendChild(breweryDiv);
});
}
displayBreweries();
searchInput.addEventListener('input', async (event) => {
const query = event.target.value.trim();
try {
if (query) {
const results = await searchBreweries(query);
breweryList.innerHTML = '';
results.forEach((result) => {
const breweryDiv = document.createElement('div');
breweryDiv.classList.add('brewery');
const name = document.createElement('h2');
name.innerText = result.name;
const type = document.createElement('p');
type.innerText = `Type: ${result.brewery_type}`;
const address = document.createElement('p');
address.innerText = `Address: ${result.street}, ${result.city}, ${result.state}, ${result.postal_code}`;
const website = document.createElement('a');
website.href = result.website_url;
website.innerText = 'Website';
const phone = document.createElement('p');
phone.innerText = `Phone: ${result.phone}`;
breweryDiv.appendChild(name);
breweryDiv.appendChild(type);
breweryDiv.appendChild(address);
breweryDiv.appendChild(website);
breweryDiv.appendChild(phone);
breweryList.appendChild(breweryDiv);
});
} else {
displayBreweries();
}
} catch (error) {
console.error(error);
}
});