forked from boolean-uk/js-dom-brewery-tours-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
170 lines (133 loc) · 5 KB
/
events.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//FILTER BY STATE (core)
selectStateForm.addEventListener('submit', event => {
event.preventDefault()
const stateName = spacesToUnderscores(event.target[0].value)
state.selectedState = stateName.toLowerCase()
getBreweriesByState(state.selectedState )
})
const getBreweriesByState = (usState) => {
fetch(`https://api.openbrewerydb.org/v1/breweries?by_state=${usState}`)
.then(r => r.json())
.then(d => {
state.filteredByState = d
filterByPageChosen(state.filteredByState)
displayPage(1)
getAndRenderCities(state.filteredByState)
})
}
//FILTER BY TYPE OR BY STATE AND TYPE (core)
const getBreweriesByStateAndType = (t) => {
if(state.selectedState) {
fetch(`https://api.openbrewerydb.org/v1/breweries?by_state=${state.selectedState}&&by_type=${t}`)
.then(r => r.json())
.then(d => {
state.filteredByStateAndType = d
filterByPageChosen(state.filteredByStateAndType)
displayPage(1)
})
} else {
fetch(`https://api.openbrewerydb.org/v1/breweries?by_type=${t}`)
.then(r => r.json())
.then(d => {
state.filteredByType = d
filterByPageChosen(state.filteredByType)
displayPage(1)
})
}
}
chooseFilter.addEventListener('change', change => {
if (change.target.value === "micro") {
getBreweriesByStateAndType("micro")
}
if (change.target.value === "regional") {
getBreweriesByStateAndType("regional")
}
if (change.target.value === "brewpub") {
getBreweriesByStateAndType("brewpub")
}
if (change.target.value === "") {
filterByPageChosen(state.filteredByState)
displayPage(1)
}
})
//EXTENSION 1
//SEARCH BY NAME - THE LIST UPDATES AS THE USER TYPES
const searchBreweries = document.querySelector('#search-breweries')
searchBreweries.addEventListener('input', event => {
event.preventDefault()
const searchParameter = event.target.value
fetch(`https://api.openbrewerydb.org/v1/breweries/search?query={${searchParameter}}`)
.then(r => r.json())
.then(d => {
state.filteredByName = d
filterByPageChosen(state.filteredByName)
displayPage(1)
})
})
//EXTENSION 2
//FILTER BY CITY
const filterByCity = (selectedCity) => {
if (state.filteredByStateAndType) {
return state.filteredByStateAndType.filter(brewery => brewery.city === selectedCity)
} else {
return state.filteredByState.filter(brewery => brewery.city === selectedCity)
}
}
state.filteredByCities = []
const addEventToCheckbox = (cityCheckbox) => {
cityCheckbox.addEventListener('change', event => {
event.preventDefault()
if (cityCheckbox.checked === true) {
//when a city is first selected, add its breweris to state.filteredByCities, then render all the breweries stored there
state.filteredByCities = state.filteredByCities.concat(filterByCity(event.target.value))
filterByPageChosen(state.filteredByCities)
displayPage(currentPage)
} else {
//if a city is then unselected, filter through state.filterdByCities and only keep the breweries that are NOT located in the un-selected city
state.filteredByCities = state.filteredByCities.filter(brewery => brewery.city !== cityCheckbox.value)
//handles cases where unselecting that one city results in no cities at all being selected
if (state.filteredByCities.length === 0) {
renderBreweriesByStateOrByStateAndType()
} else {
filterByPageChosen(state.filteredByCities)
displayPage(currentPage)
}
}
})
}
const renderBreweriesByStateOrByStateAndType = () => {
if (state.filteredByStateAndType){
filterByPageChosen(state.filteredByStateAndType)
} else {
filterByPageChosen(state.filteredByState)
}
displayPage(currentPage)
}
const addClearAllEvent = (cityCheckbox) => {
const clearAllButton = document.querySelector(".clear-all-btn")
clearAllButton.addEventListener('click', () => {
cityCheckbox.checked = false
removeCurrentList()
renderBreweriesByStateOrByStateAndType()
})
}
//EXTENSION 3
//ensures that only the search results belonging to one page appear as said page number is selected
const filterByPageChosen = (breweriesArray) => {
state.byPage = []
for (let i = 0; i < breweriesArray.length; i+=10) {
const filteredArray = breweriesArray.slice(i, i + 10)
state.byPage.push(filteredArray)
}
}
//EXTENSION 4
const addToVisitListButtonEvent = (addToVisitListButton, brewery) => {
addToVisitListButton.addEventListener('click', () => {
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(brewery)
}
fetch("http://localhost:3000/myList", options)
})
}