-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filters to blog page (nrwl#26997)
Co-authored-by: Juri <[email protected]>
- Loading branch information
1 parent
157aca4
commit 3b1deb1
Showing
7 changed files
with
324 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,161 @@ | ||
'use client'; | ||
import { BlogPostDataEntry } from '@nx/nx-dev/data-access-documents/node-only'; | ||
import { MoreBlogs } from './more-blogs'; | ||
import { FeaturedBlogs } from './featured-blogs'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { Filters } from './filters'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import { | ||
ComputerDesktopIcon, | ||
BookOpenIcon, | ||
MicrophoneIcon, | ||
CubeIcon, | ||
AcademicCapIcon, | ||
ChatBubbleOvalLeftEllipsisIcon, | ||
ListBulletIcon, | ||
} from '@heroicons/react/24/outline'; | ||
|
||
export interface BlogContainerProps { | ||
blogPosts: BlogPostDataEntry[]; | ||
tags: string[]; | ||
} | ||
let ALL_TOPICS = [ | ||
{ | ||
label: 'All', | ||
icon: ListBulletIcon, | ||
value: 'All', | ||
heading: 'All Blogs', | ||
}, | ||
{ | ||
label: 'Stories', | ||
icon: BookOpenIcon, | ||
value: 'customer story', | ||
heading: 'Customer Stories', | ||
}, | ||
{ | ||
label: 'Webinars', | ||
icon: ComputerDesktopIcon, | ||
value: 'webinar', | ||
heading: 'Webinars', | ||
}, | ||
{ | ||
label: 'Podcasts', | ||
icon: MicrophoneIcon, | ||
value: 'podcast', | ||
heading: 'Podcasts', | ||
}, | ||
{ | ||
label: 'Releases', | ||
icon: CubeIcon, | ||
value: 'release', | ||
heading: 'Release Blogs', | ||
}, | ||
{ | ||
label: 'Talks', | ||
icon: ChatBubbleOvalLeftEllipsisIcon, | ||
value: 'talk', | ||
heading: 'Talks', | ||
}, | ||
{ | ||
label: 'Tutorials', | ||
icon: AcademicCapIcon, | ||
value: 'tutorial', | ||
heading: 'Tutorials', | ||
}, | ||
]; | ||
export function BlogContainer({ blogPosts, tags }: BlogContainerProps) { | ||
const searchParams = useSearchParams(); | ||
const [filteredList, setFilteredList] = useState(blogPosts); | ||
|
||
// Only show filters that have blog posts | ||
const filters = useMemo(() => { | ||
return [ | ||
ALL_TOPICS[0], | ||
...ALL_TOPICS.filter((filter) => tags.includes(filter.value)), | ||
]; | ||
}, [tags]); | ||
|
||
const { | ||
initialFirstFive, | ||
initialRest, | ||
initialSelectedFilterHeading, | ||
initialSelectedFilter, | ||
} = useMemo( | ||
() => initializeFilters(blogPosts, searchParams), | ||
[blogPosts, searchParams] | ||
); | ||
|
||
const [firstFiveBlogs, setFirstFiveBlogs] = | ||
useState<BlogPostDataEntry[]>(initialFirstFive); | ||
const [remainingBlogs, setRemainingBlogs] = | ||
useState<BlogPostDataEntry[]>(initialRest); | ||
const [selectedFilterHeading, setSelectedFilterHeading] = useState( | ||
initialSelectedFilterHeading | ||
); | ||
|
||
function updateBlogPosts() { | ||
setFirstFiveBlogs( | ||
filteredList.slice(0, filteredList.length > 5 ? 5 : filteredList.length) | ||
); | ||
setRemainingBlogs(filteredList.length > 5 ? filteredList.slice(5) : []); | ||
} | ||
|
||
useEffect(() => updateBlogPosts(), [filteredList]); | ||
|
||
export function BlogContainer({ blogPosts }: BlogContainerProps) { | ||
const [blog1, blog2, blog3, blog4, blog5, ...restOfPosts] = blogPosts; | ||
return ( | ||
<main id="main" role="main" className="w-full py-8"> | ||
<div className="mx-auto mb-8 w-full max-w-[1088px] px-8"> | ||
<header className="mx-auto mb-16"> | ||
<h1 | ||
id="blog-title" | ||
className="text-xl font-semibold tracking-tight text-slate-900 md:text-2xl dark:text-slate-100" | ||
> | ||
Blog | ||
</h1> | ||
</header> | ||
<FeaturedBlogs blogs={[blog1, blog2, blog3, blog4, blog5]} /> | ||
{restOfPosts.length > 0 ? <MoreBlogs blogs={restOfPosts} /> : null} | ||
<div className="mb-12 mt-20 flex items-center justify-between"> | ||
<header> | ||
<h1 | ||
id="blog-title" | ||
className="text-xl font-semibold tracking-tight text-slate-900 md:text-2xl dark:text-slate-100" | ||
> | ||
{selectedFilterHeading} | ||
</h1> | ||
</header> | ||
<div className="flex items-center justify-end md:justify-start"> | ||
<Filters | ||
blogs={blogPosts} | ||
filters={filters} | ||
initialSelectedFilter={initialSelectedFilter} | ||
setFilteredList={setFilteredList} | ||
setSelectedFilterHeading={setSelectedFilterHeading} | ||
/> | ||
</div> | ||
</div> | ||
<FeaturedBlogs blogs={firstFiveBlogs} /> | ||
{!!remainingBlogs.length && <MoreBlogs blogs={remainingBlogs} />} | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
function initializeFilters( | ||
blogPosts: BlogPostDataEntry[], | ||
searchParams: URLSearchParams | ||
) { | ||
const filterBy = searchParams.get('filterBy'); | ||
|
||
const defaultState = { | ||
initialFirstFive: blogPosts.slice(0, 5), | ||
initialRest: blogPosts.slice(5), | ||
initialSelectedFilterHeading: 'All Blogs', | ||
initialSelectedFilter: 'All', | ||
}; | ||
|
||
if (!filterBy) { | ||
return defaultState; | ||
} | ||
|
||
const result = blogPosts.filter((post) => post.tags.includes(filterBy)); | ||
|
||
const initialFilter = ALL_TOPICS.find((filter) => filter.value === filterBy); | ||
|
||
return { | ||
initialFirstFive: result.slice(0, 5), | ||
initialRest: result.length > 5 ? result.slice(5) : [], | ||
initialSelectedFilterHeading: initialFilter?.heading || 'All Blogs', | ||
initialSelectedFilter: initialFilter?.value || 'All', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.