-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard-game-explorer.js
64 lines (56 loc) · 1.84 KB
/
board-game-explorer.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
document.addEventListener("alpine:init", () => {
Alpine.data("boardGameExplorer", () => ({
games: [],
search: Alpine.$persist(""),
sortByRating: Alpine.$persist("none"),
sortByComplexity: Alpine.$persist("none"),
init() {
fetch("board-games.json")
.then((response) => response.json())
.then((data) => {
this.games = data.items.map((item) => ({
id: item.id,
name: item.product.title,
year: item.product.year_published,
minPlayers: item.product.detail.min_players,
maxPlayers: item.product.detail.max_players,
minPlayTime: item.product.detail.min_play_time,
maxPlayTime: item.product.detail.max_play_time,
complexity: item.product.detail.bgg_complexity,
rating: item.product.detail.bgg_geek_rating,
image: item.product.media.source.href,
}));
});
},
get filteredGames() {
if (this.search !== "") {
return this.games.filter((game) =>
game.name.toLowerCase().includes(this.search.toLowerCase())
);
}
return this.games;
},
get sortedGames() {
let sorted = [...this.filteredGames];
if (this.sortByRating === "asc") {
sorted.sort((a, b) => a.rating - b.rating);
} else if (this.sortByRating === "desc") {
sorted.sort((a, b) => b.rating - a.rating);
}
if (this.sortByComplexity === "asc") {
sorted.sort((a, b) => a.complexity - b.complexity);
} else if (this.sortByComplexity === "desc") {
sorted.sort((a, b) => b.complexity - a.complexity);
}
return sorted;
},
searchGames() {
this.sortByRating = "none";
this.sortByComplexity = "none";
},
clearFilter() {
this.search = "";
this.searchGames();
},
}));
});