Skip to content

Commit eede6d7

Browse files
committed
feat(blog-redesign): Refactor JS. Add blog preview image to filtered posts.
1 parent 88d80b9 commit eede6d7

File tree

3 files changed

+79
-63
lines changed

3 files changed

+79
-63
lines changed

assets/javascripts/new-javascripts/blog.js

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@ const checkboxes = [...wrapper.querySelectorAll('.category-filter')]
55
const selectAllBox = document.querySelector('.select-all')
66
const dropdown = document.querySelector('.dropdown')
77
const allCheckboxes = [selectAllBox, ...checkboxes]
8-
const dropdownOpenButton = document.querySelector('.dropdown-toggle')
8+
const filterMenuToggle = document.querySelector('.dropdown-toggle')
99
const dropdownCloseButton = document.querySelector('.dropdown-close')
10-
const elementsCache = []
11-
1210
// create post links
1311
const createAnchor = (postData) => {
1412
const anchor = document.createElement('a')
13+
const imgEl = postData['image-url']
14+
? `<img src="${postData['image-url']}" alt="${postData['image-alt']}" />`
15+
: ''
16+
console.log(imgEl, postData.title)
1517
anchor.href = postData.url
16-
anchor.classList.add('post-link')
18+
anchor.classList = imgEl ? 'post-link post-link-with-image' : 'post-link'
1719
anchor.innerHTML = `
18-
<h3 class="title">${postData.title}</h3>
19-
<time pubdate datetime="${postData.date}" class="blog-date">${postData.date}</time>
20-
<p class="body-copy">${postData.excerpt}</p>
21-
${postData.categories.reduce((markup, category) => {
22-
return markup + ` <span class="category body-copy">${category}</span>`
23-
}, '')}
24-
`
20+
${imgEl}
21+
${imgEl ? '<div>' : ''}
22+
<h3 class="title">${postData.title}</h3>
23+
<time pubdate datetime="${postData.date}" class="blog-date">${postData.date}</time>
24+
<p class="body-copy">${postData.excerpt}</p>
25+
${postData.categories.reduce((markup, category) => {
26+
return markup + ` <span class="category body-copy">${category}</span>`
27+
}, '')}
28+
${imgEl ? '</div>' : ''}
29+
`
2530
return anchor
2631
}
2732

@@ -36,26 +41,57 @@ const selectAllCategories = () => {
3641
}
3742
}
3843

39-
dropdownOpenButton.addEventListener('click', function () {
44+
const filterPosts = (elementsCache, checkboxes) => {
45+
// If all categories are selected, bypass filter check
46+
if (selectAllBox.checked) return elementsCache
47+
48+
const activeFilters = checkboxes
49+
.filter((checkbox) => checkbox.checked)
50+
.map((checkbox) => checkbox.value)
51+
const filteredPosts = elementsCache.filter((post) => {
52+
return post.data.categories.some((category) => {
53+
return activeFilters.includes(category)
54+
})
55+
})
56+
57+
return filteredPosts
58+
}
59+
60+
const updatePosts = (posts, postsWrapper) => {
61+
postsWrapper.innerHTML = ''
62+
for (let post of posts) {
63+
postsWrapper.appendChild(post.elemenet)
64+
}
65+
}
66+
67+
const elementsCache = postData.map((post) => {
68+
return {
69+
data: post,
70+
elemenet: createAnchor(post),
71+
}
72+
})
73+
74+
updatePosts(filterPosts(elementsCache, checkboxes), postsWrapper, postsWrapper)
75+
76+
filterMenuToggle.addEventListener('click', () => {
4077
dropdown.classList.toggle('active')
4178

42-
// Toggle aria-expanded attribute
4379
const isExpanded = this.getAttribute('aria-expanded') === 'true'
44-
this.setAttribute('aria-expanded', !isExpanded)
80+
filterMenuToggle.toggleAttribute('aria-expanded', !isExpanded)
4581
})
4682

4783
// Close the dropdown if clicked outside
48-
window.addEventListener('click', function (evt) {
84+
window.addEventListener('click', (evt) => {
4985
if (!dropdown.contains(evt.target)) {
5086
dropdown.classList.remove('active')
51-
dropdownOpenButton.setAttribute('aria-expanded', 'false')
87+
filterMenuToggle.setAttribute('aria-expanded', 'false')
5288
}
5389
})
5490

55-
// Handle the "All" button functionality
91+
// Select all category filters
5692
selectAllBox.addEventListener('click', selectAllCategories)
5793

58-
dropdownCloseButton.addEventListener('click', function () {
94+
dropdownCloseButton.addEventListener('click', () => {
5995
const dropdown = this.closest('.dropdown')
6096
dropdown.classList.remove('active')
6197
document
@@ -64,9 +100,8 @@ dropdownCloseButton.addEventListener('click', function () {
64100
})
65101

66102
checkboxes.forEach((checkbox) => {
67-
checkbox.addEventListener('change', function () {
68-
// debugger
69-
// If every box is either checked or none are, set all categories on
103+
checkbox.addEventListener('change', () => {
104+
// If every box is either checked or none are, set all filter to checked
70105
if (
71106
checkboxes.every((checkbox) => checkbox.checked === checkboxes[0].checked)
72107
) {
@@ -79,40 +114,3 @@ checkboxes.forEach((checkbox) => {
79114
updatePosts(filterPosts(elementsCache, checkboxes), postsWrapper)
80115
})
81116
})
82-
83-
const filterPosts = (elementsCache, checkboxes) => {
84-
// if all categories is selected, bypass filter check
85-
if (selectAllBox.checked) return elementsCache
86-
87-
const activeFilters = checkboxes
88-
.filter((checkbox) => checkbox.checked)
89-
.map((checkbox) => checkbox.value)
90-
const filteredPosts = elementsCache.filter((post) => {
91-
return post.data.categories.some((category) => {
92-
return activeFilters.includes(category)
93-
})
94-
})
95-
96-
return filteredPosts
97-
}
98-
99-
const makeElements = (elementsCache, postData) => {
100-
for (let i = 0; i < postData.length; i++) {
101-
elementsCache.push({
102-
data: postData[i],
103-
elemenet: createAnchor(postData[i]),
104-
})
105-
}
106-
107-
return elementsCache
108-
}
109-
110-
const updatePosts = (posts, postsWrapper) => {
111-
postsWrapper.innerHTML = ''
112-
for (let post of posts) {
113-
postsWrapper.appendChild(post.elemenet)
114-
}
115-
}
116-
117-
makeElements(elementsCache, postData)
118-
updatePosts(filterPosts(elementsCache, checkboxes), postsWrapper, postsWrapper)

assets/stylesheets/new-stylesheets/pages/_blog.scss

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
}
6363

6464
// page global blog card styles
65-
6665
.blog-date {
6766
display: block;
6867
color: var(--blog-muted-color);
@@ -425,6 +424,26 @@
425424
text-decoration: none;
426425
display: block;
427426

427+
&-with-image {
428+
display: flex;
429+
gap: 36px;
430+
431+
img {
432+
width: 120px;
433+
height: 120px;
434+
background-size: cover;
435+
}
436+
437+
@media only screen and (max-width: 1024px) {
438+
display: block;
439+
440+
img {
441+
width: 100%;
442+
height: auto;
443+
}
444+
}
445+
}
446+
428447
h3 {
429448
font-size: 22px;
430449
font-weight: 400;
@@ -450,5 +469,3 @@
450469
}
451470
}
452471
}
453-
454-
// bg?

blog/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ title: Blog
3232
<span class="blog-title">{{ latest_post.title }}</span>
3333
<time class="blog-date body-copy" pubdate datetime="{{ latest_post.date | date_to_xmlschema }}">{{ latest_post.date | date: "%B %-d, %Y" }}</time>
3434
<p class="blog-excerpt body-copy">{{ latest_post.excerpt | strip_html}}</p>
35-
<a class="blog-post-cta" href="{{ post.url }}">{{site.data.new-data.blog.page-data.read-more}}</a>
35+
<a class="blog-post-cta" href="{{ latest_post.url }}">{{site.data.new-data.blog.page-data.read-more}}</a>
3636
</div>
3737
<!-- </a> -->
3838
</li>
@@ -76,7 +76,8 @@ title: Blog
7676
"url": "{{ post.url | escape }}",
7777
"date": "{{ post.date | date: "%B %-d, %Y" }}",
7878
"excerpt": "{{ post.excerpt | strip_html | strip_newlines | escape }}"{% if post.featured-image %},
79-
"image": "{{ post.featured-image.url }}"
79+
"image-url": "{{ post.featured-image.url | escape }}",
80+
"image-alt": "{{ post.featured-image.alt }}"
8081
{% endif %}
8182
}{% if forloop.last == false %},{% endif %}{% endfor %}]
8283
</script>

0 commit comments

Comments
 (0)