Skip to content

Commit f9c84f3

Browse files
committed
added a total occurance counter to each filter button
1 parent 3a61546 commit f9c84f3

File tree

1 file changed

+68
-18
lines changed

1 file changed

+68
-18
lines changed

src/components/Content.astro

+68-18
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,66 @@ import '../styles/globals.scss';
44
import Card from "./ui/Card.astro";
55
---
66

7+
<script>
8+
const inspire = document.getElementById('inspirationAmount');
9+
const personal = document.getElementById('personalAmount');
10+
const commerce = document.getElementById('commerceAmount');
11+
const resources = document.getElementById('resourcesAmount');
12+
const free = document.getElementById('freeAmount');
13+
const all = document.getElementById('totalAmount');
14+
15+
let items = document.querySelectorAll('.item');
16+
17+
const myList = document.getElementById('contentWrap');
18+
19+
const typeCounts = {};
20+
21+
for (const listItem of myList.children) {
22+
const itemType = listItem.firstElementChild.textContent.toLowerCase().trim();
23+
24+
if (typeCounts[itemType]) {
25+
typeCounts[itemType]++;
26+
} else {
27+
typeCounts[itemType] = 1;
28+
}
29+
}
30+
31+
let inspireType = 'inspiration',
32+
personalType = 'personal',
33+
commerceType = 'commerce',
34+
resourcesType = 'resources',
35+
freeType = 'free';
36+
37+
let total = 0;
38+
for (const type in typeCounts) {
39+
console.log(`Type "${type}": ${typeCounts[type]} occurrences`);
40+
total += typeCounts[type];
41+
42+
43+
inspire.textContent = `${typeCounts[inspireType]}`.toString();
44+
personal.textContent = `${typeCounts[personalType]}`.toString();
45+
commerce.textContent = `${typeCounts[commerceType]}`.toString();
46+
resources.textContent = `${typeCounts[resourcesType]}`.toString();
47+
free.textContent = `${typeCounts[freeType]}`.toString();
48+
all.textContent = `${total}`.toString();
49+
}
50+
51+
</script>
52+
753
<section>
854
<div id="filterList">
955
<div>
1056
<section class="filterMenu">
1157
<h3>Filter</h3>
1258
<div class="filterButtonList">
13-
<button class="active" id="filterAll">All</button>
59+
<button class="active" id="filterAll">All<span id="totalAmount"></span></button>
1460
</div>
1561
<div class="filterButtonList">
16-
<button class='filterButton' aria-label='Inspiration contentt'>Inspiration</button>
17-
<button class='filterButton' aria-label='Personal websites, portfolios, blogs'>Personal</button>
18-
<button class='filterButton' aria-label='Commerce tools'>Commerce</button>
19-
<button class='filterButton' aria-label='Resources'>Resources</button>
20-
<button class='filterButton' aria-label='Free resources'>Free</button>
62+
<button class='filterButton' aria-label='Inspiration contentt'><a>Inspiration</a><span id="inspirationAmount"></span></button>
63+
<button class='filterButton' aria-label='Personal websites, portfolios, blogs'><a>Personal</a><span id="personalAmount"></span></button>
64+
<button class='filterButton' aria-label='Commerce tools'><a>Commerce</a><span id="commerceAmount"></span></button>
65+
<button class='filterButton' aria-label='Resources'><a>Resources</a><span id="resourcesAmount"></span></button>
66+
<button class='filterButton' aria-label='Free resources'><a>Free</a><span id="freeAmount"></span></button>
2167
</div>
2268
</section>
2369
</div>
@@ -141,18 +187,13 @@ import Card from "./ui/Card.astro";
141187
font-size: 1rem !important;
142188
}
143189

144-
@media screen and (max-width: 800px) {
145-
section {
146-
flex-direction: row;
147-
padding: 1rem;
148-
border-radius: 0;
149-
}
150190

151-
}
152191

153192

154193
.filterButton, #filterAll {
155194
display: flex;
195+
align-items: center;
196+
gap: 0.2rem;
156197
}
157198

158199
.filterButton, #filterAll {
@@ -193,9 +234,21 @@ import Card from "./ui/Card.astro";
193234
background-color: rgba(53, 115, 240, 0.25);
194235
}
195236

237+
.filterButton span, #totalAmount {
238+
font-size: 0.7rem !important;
239+
position: relative;
240+
top: 1px;
241+
font-weight: 600;
242+
opacity: 0.5;
243+
@apply p-1;
244+
}
245+
196246
</style>
197247

198248
<script>
249+
250+
251+
199252
let filters = new Set();
200253
let buttons = document.querySelectorAll('.filterButton');
201254
let items = document.querySelectorAll('.item');
@@ -206,10 +259,10 @@ import Card from "./ui/Card.astro";
206259
btn.addEventListener('click', () => {
207260
if (btn.classList.contains('active')) {
208261
btn.classList.remove('active');
209-
filters.delete(btn.innerHTML.toUpperCase());
262+
filters.delete(btn.firstElementChild.textContent.toUpperCase());
210263
} else {
211264
btn.classList.add('active');
212-
filters.add(btn.innerHTML.toUpperCase());
265+
filters.add(btn.firstElementChild.textContent.toUpperCase());
213266
}
214267

215268
if (showAll.classList.contains('active')) {
@@ -256,9 +309,6 @@ import Card from "./ui/Card.astro";
256309
} else {
257310

258311
items.forEach(item => {
259-
console.log(item.querySelector('p') + ' item');
260-
261-
item.firstElementChild.textContent;
262312

263313
let itemText = item.firstElementChild.textContent.toUpperCase().trim();
264314

0 commit comments

Comments
 (0)