Skip to content

Commit c20a472

Browse files
committed
Adding tags filtera to blog section
Changes: - New component added to tags menu - Getting tags array from posts - Adding filter method on Blog component - Changing color of blog title
1 parent b98be79 commit c20a472

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/components/MenuTagsBlog.svelte

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script>
2+
export let activeTag;
3+
export let handleTags;
4+
export let tags;
5+
</script>
6+
7+
{#each tags as tag}
8+
<a
9+
href={null}
10+
class={activeTag === tag ? 'text-cmxgreen' : null}
11+
on:click={() => handleTags(tag)}
12+
>
13+
{tag.toUpperCase()}
14+
</a>
15+
{/each}
16+
17+
<style>
18+
a {
19+
display: inline-block;
20+
text-transform: uppercase;
21+
font-weight: 700;
22+
line-height: 2.5;
23+
}
24+
a:not(:last-child) {
25+
padding-right: 15px;
26+
}
27+
a:hover {
28+
cursor: pointer;
29+
text-decoration: underline;
30+
}
31+
</style>

src/lib/utilities.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const interfaceElement = [
2+
{ tags: [''] }
3+
];
4+
5+
export function getFlatArrayUnrepeated(elements = interfaceElement) {
6+
let unrepeated = [];
7+
const setToLowerCase = (item = '') => item.toLowerCase();
8+
9+
elements.forEach(item => {
10+
unrepeated = [...new Set([...unrepeated, ...new Set(item?.tags?.map(setToLowerCase))])]
11+
})
12+
13+
return unrepeated;
14+
}

src/routes/blog/+page.server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import client from '$lib/apiClient'
2+
import { getFlatArrayUnrepeated } from '$lib/utilities';
23
import { readSingleton, readItems } from '@directus/sdk'
34

45
export async function load() {
@@ -20,9 +21,13 @@ export async function load() {
2021
sort: ['-date_published'],
2122
}))
2223

24+
const tags = getFlatArrayUnrepeated(posts);
25+
tags.unshift('todos');
26+
2327
return {
2428
blog,
25-
posts
29+
posts,
30+
tags,
2631
}
2732
}
2833

src/routes/blog/+page.svelte

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<script>
22
import ArticleCard from "@/components/Cards/ArticleCard.svelte";
33
import BlogHero from "@/components/BlogHero.svelte";
4+
import MenuTagsBlog from "@/components/MenuTagsBlog.svelte";
45
export let data
5-
const { blog, posts } = data
6+
const { blog, posts, tags } = data
7+
let filteredPosts = [...posts]
8+
let activeTag = "todos"
9+
10+
const handleTags = (tag = "") => {
11+
tag = tag.toLocaleLowerCase()
12+
activeTag = tag
13+
filteredPosts = tag === "todos"
14+
? [...posts]
15+
: posts.filter(post => post.tags.map((tagItem = '') => tagItem.toLowerCase()).includes(tag))
16+
}
617
</script>
718
<div class="container my-20 mx-auto">
819

920
<div class="container m-auto px-3">
1021
<div class="my-7">
11-
<h1 class="text-5xl text-cmxblack font-bold">Blog</h1>
22+
<h1 class="text-5xl text-cmxgreen font-bold">Blog</h1>
1223
</div>
1324
<BlogHero
1425
slug={blog.highlight_post.slug}
@@ -20,9 +31,16 @@
2031
content={blog.highlight_post.content}
2132
/>
2233
</div>
34+
<div class="container mx-auto px-3 pt-16 pb-12 text-center">
35+
<MenuTagsBlog
36+
activeTag={activeTag}
37+
handleTags={handleTags}
38+
tags={tags}
39+
/>
40+
</div>
2341
<div class="container m-auto p-3">
2442
<div class="md:grid grid-cols-3 gap-5">
25-
{#each posts as post}
43+
{#each filteredPosts as post}
2644
<ArticleCard
2745
slug={post.slug}
2846
title={post.title}

0 commit comments

Comments
 (0)