Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leonardo Lodi #108

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./styles/reset.css" />
<link rel="stylesheet" href="./styles/index.css" />
<script defer src="script.js"></script>
<title>Brewery Tours</title>
</head>
<body>
Expand All @@ -19,20 +20,18 @@ <h2>Welcome to Brewery Tours</h2>
</form>
</section>
</header>
<main>
<main id="main-list">
<aside class="filters-section">
<aside class="filters-section">
<h2>Filter By:</h2>
<form id="filter-by-type-form" autocompete="off">
<label for="filter-by-type"><h3>Type of Brewery</h3></label>
<select name="filter-by-type" id="filter-by-type">
<option value="">Select a type...</option>
<option value="micro">Micro</option>
<option value="regional">Regional</option>
<option value="brewpub">Brewpub</option>
</select>
</form>
</aside>
<h2>Filter By:</h2>
<form id="filter-by-type-form" autocompete="off">
<label for="filter-by-type"><h3>Type of Brewery</h3></label>
<select name="filter-by-type" id="filter-by-type">
<option value="">Select a type...</option>
<option value="micro">Micro</option>
<option value="regional">Regional</option>
<option value="brewpub">Brewpub</option>
</select>
</form>
</aside>
<h1>List of Breweries</h1>
<article>
Expand Down
223 changes: 223 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
const url = 'https://api.openbrewerydb.org/v1/breweries'
const breweriesList = document.getElementById('breweries-list')
const breweriesFilterByType = document.getElementById('filter-by-type')
const breweriesFilterByState = document.getElementById('select-state-form')
const breweriesFilterInput = document.getElementById('select-state')
const mainList = document.getElementById('main-list')
const filterSection = document.querySelector('.filters-section')

function createBreweryEl(name, type, street, address, phoneNumber, website_url, state, city) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take in a brewery object

const breweryEl = document.createElement('li')
breweryEl.setAttribute('value', state)
breweryEl.setAttribute('type', type)
breweryEl.setAttribute('name', name)
breweryEl.setAttribute('city', city)
breweriesList.append(breweryEl)

const breweryName = document.createElement('h2')
breweryName.innerText = name
breweryEl.append(breweryName)

const breweryType = document.createElement('div')
breweryType.innerText = type
breweryType.classList.add('type')
breweryEl.append(breweryType)

const breweryAddressSection = document.createElement('section')
breweryAddressSection.classList.add('address')
breweryEl.append(breweryAddressSection)

const breweryAddress = document.createElement('h3')
breweryAddress.innerText = 'Address:'
breweryAddressSection.append(breweryAddress)

const breweryAddressStreet = document.createElement('p')
breweryAddressStreet.innerText = street
breweryAddressSection.append(breweryAddressStreet)

const breweryAddressCity = document.createElement('p')
breweryAddressSection.append(breweryAddressCity)

const breweryStrongAddressCity = document.createElement('strong')
breweryStrongAddressCity.innerText = address
breweryAddressCity.append(breweryStrongAddressCity)

const breweryPhoneSection = document.createElement('section')
breweryPhoneSection.classList.add('phone')
breweryEl.append(breweryPhoneSection)

const breweryPhone = document.createElement('h3')
breweryPhone.innerText = 'Phone:'
breweryPhoneSection.append(breweryPhone)

const breweryPhoneNumber = document.createElement('p')
breweryPhoneNumber.innerText = phoneNumber
breweryPhoneSection.append(breweryPhoneNumber)

const breweryLinkSection = document.createElement('section')
breweryLinkSection.classList.add('link')
breweryEl.append(breweryLinkSection)

const breweryLink = document.createElement('a')
breweryLink.setAttribute('href', website_url)
breweryLink.setAttribute('target', '_blank')
breweryLink.innerText = 'Visit Website'
breweryLinkSection.append(breweryLink)

checkBreweryType(type, breweryEl)

breweriesFilterByState.addEventListener('submit', (e) => {
e.preventDefault()

if (
breweryEl.getAttribute('value').toLowerCase() === breweriesFilterInput.value.toLowerCase() ||
breweriesFilterInput.value === ''
) {
breweryEl.style.display = ''
} else {
breweryEl.style.display = 'none'
}
})
}

function createSearchBreweriesByName() {
const searchBar = document.createElement('div')
searchBar.classList.add('search-bar')
searchBar.setAttribute('id', 'search-breweries-form')
searchBar.style.margin = '0 1rem 0 1rem'
mainList.append(searchBar)

const searchBarLabel = document.createElement('label')
searchBarLabel.innerText = 'Search breweries:'
searchBarLabel.style.fontWeight = 'bold'
searchBarLabel.setAttribute('name', 'search-bar-label')
searchBar.append(searchBarLabel)

const searchBarInput = document.createElement('input')
searchBarInput.setAttribute('name', 'search-bar-input')
searchBar.append(searchBarInput)

searchBarInput.addEventListener('input', () => {
for (let i = 0; i < breweriesList.children.length; i++) {
if (breweriesList.children[i].getAttribute('name').toLowerCase().includes(searchBarInput.value.toLowerCase()) || searchBarInput.value.toLowerCase() === '') {
breweriesList.children[i].style.display = ''
} else {
breweriesList.children[i].style.display = 'none'
}
}
})
}

function checkBreweryType(brewery_type, brewery_el) {
if (
brewery_type !== 'micro' &&
brewery_type !== 'regional' &&
brewery_type !== 'brewpub'
) brewery_el.remove()
Comment on lines +112 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!brewery_type.includes(['micro', 'regional', ''])

}

function createCheckboxByCity() {
const filterSectionForm = document.createElement('form')
filterSectionForm.setAttribute('id', 'filter-by-city-form')
filterSection.append(filterSectionForm)

const filterSectionH3 = document.createElement('h3')
filterSectionH3.innerText = 'Cities'
filterSectionForm.append(filterSectionH3)

const clearAllBtn = document.createElement('button')
clearAllBtn.classList.add('clear-all-btn')
clearAllBtn.innerText = 'clear all'
filterSectionForm.append(clearAllBtn)

const citiesArray = []

for (let i = 0; i < breweriesList.children.length; i++) {
const city = breweriesList.children[i].getAttribute('city')
if (!citiesArray.includes(city)) {
citiesArray.push(city)
}
}

citiesArray.forEach(city => {
const breweriesCheckbox = document.createElement('input')
breweriesCheckbox.setAttribute('type', 'checkbox')
breweriesCheckbox.setAttribute('name', 'city')
breweriesCheckbox.setAttribute('value', city)
filterSectionForm.append(breweriesCheckbox)

const breweriesCheckboxLabel = document.createElement('label')
breweriesCheckboxLabel.innerText = city
filterSectionForm.append(breweriesCheckboxLabel)
})

const checkboxes = filterSectionForm.querySelectorAll('input[type="checkbox"]');

filterSectionForm.addEventListener('change', (e) => {
e.preventDefault()

checkboxes.forEach(checkbox => {
if (checkbox.checked) {
breweriesList.querySelectorAll('li').forEach(brewery => {
if (checkbox.value === '' || brewery.getAttribute('city') === checkbox.value) {
brewery.style.display = ''
} else {
brewery.style.display = 'none'
}
})
}
})

let noCheckboxChecked = true
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
noCheckboxChecked = false
}
})

if (noCheckboxChecked) {
breweriesList.querySelectorAll('li').forEach(brewery => {
brewery.style.display = ''
})
}
})

clearAllBtn.addEventListener('click', (e) => {
e.preventDefault()

checkboxes.forEach(checkbox => {
checkbox.checked = false
})

breweriesList.querySelectorAll('li').forEach(brewery => {
brewery.style.display = ''
})
})
}

async function getAllBreweries() {
const res = await fetch(url)
const json = await res.json()

json.forEach(brewery => createBreweryEl(brewery.name, brewery.brewery_type, brewery.street, `${brewery.city}, ${brewery.postal_code}`, brewery.phone, brewery.website_url, brewery.state, brewery.city))
}

function onChange() {
for (let i = 0; i < breweriesList.children.length; i++) {
if (
breweriesFilterByType.value === breweriesList.children[i].getAttribute('type') ||
breweriesFilterByType.value === ''
) {
breweriesList.children[i].style.display = ''
} else {
breweriesList.children[i].style.display = 'none'
}
}
}

breweriesFilterByType.addEventListener('change', onChange)

getAllBreweries().then(() => {
createCheckboxByCity()
createSearchBreweriesByName()
})
4 changes: 3 additions & 1 deletion styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ main {
}

.clear-all-btn {
display: flex;
justify-content: end;
border: none;
background-color: transparent;
color: #1565c0;
Expand All @@ -93,7 +95,6 @@ main {
grid-gap: 1rem;

border: 1px solid #000;
border-radius: 0 0 0.25rem 0.25rem;
}

#filter-by-type-form label {
Expand All @@ -110,6 +111,7 @@ main {
border-radius: 0 0 0.25rem 0.25rem;
border: 1px solid #000;
border-top: none;
row-gap: 1rem;
}

h1 {
Expand Down