-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-beer.js
30 lines (26 loc) · 1.02 KB
/
filter-beer.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
import { renderBeers } from "./renderer.js";
import { beersRepository } from "./beers-repository.js";
import { sortBeers } from "./sort-beers.js";
export function createFilterEvents() {
const input = document.querySelector("#search-beer");
input.addEventListener("input", async event => {
const filteredBeersByName = await filterBeersByName(event.target.value);
const sortedFilteredBeers = sortBeers(filteredBeersByName);
renderBeers(sortedFilteredBeers);
});
const date = document.querySelector("#search-beer-date");
date.addEventListener("input", event => {
const date = new Date(event.target.value);
const filteredBeersByDate = filterBeersByDate(date);
const sortedFilteredBeers = sortBeers(filteredBeersByDate);
renderBeers(sortedFilteredBeers);
});
}
async function filterBeersByName(name) {
return beersRepository.getBeers({ name });
}
function filterBeersByDate(date) {
return beersRepository.beers.filter(
beer => beer.date.getTime() >= date.getTime()
);
}