11import { Children , Fragment , useEffect } from "react" ;
2-
32import Head from "next/head" ;
43import ArticlePreview from "../../components/ArticlePreview/ArticlePreview" ;
54import ArticleLoading from "../../components/ArticlePreview/ArticleLoading" ;
65import Layout from "../../components/Layout/Layout" ;
7- import PageHeading from "../../components/PageHeading/PageHeading" ;
86import { trpc } from "../../utils/trpc" ;
97import { useInView } from "react-intersection-observer" ;
8+ import { useRouter } from "next/router" ;
109
1110const ArticlesPage = ( ) => {
11+ const router = useRouter ( ) ;
12+
13+ const { filter } = router . query ;
14+
15+ type Filter = "newest" | "oldest" | "top" ;
16+ const filters : Filter [ ] = [ "newest" , "oldest" , "top" ] ;
17+
18+ const getSortBy = ( ) => {
19+ if ( typeof filter === "string" ) {
20+ const hasFilter = filters . some ( ( f ) => f === filter ) ;
21+ if ( hasFilter ) return filter as Filter ;
22+ }
23+ return "newest" ;
24+ } ;
25+
26+ const selectedSortFilter = getSortBy ( ) ;
27+
1228 const { status, data, isFetchingNextPage, fetchNextPage, hasNextPage } =
1329 trpc . post . all . useInfiniteQuery (
14- { limit : 15 } ,
30+ { limit : 15 , sort : selectedSortFilter } ,
1531 {
1632 getNextPageParam : ( lastPage ) => lastPage . nextCursor ,
1733 }
@@ -28,7 +44,7 @@ const ArticlesPage = () => {
2844 return (
2945 < >
3046 < Head >
31- < title > Codú | Articles - View our latest web developer articles</ title >
47+ < title > { ` Codú - View our ${ selectedSortFilter } web developer articles` } </ title >
3248 < meta name = "description" content = "Codú | Web Developer Community" />
3349 < link rel = "icon" href = "/favicon.ico" />
3450 < link rel = "manifest" href = "site.webmanifest" />
@@ -40,7 +56,7 @@ const ArticlesPage = () => {
4056 < link rel = "alternate" type = "application/rss+xml" href = "/feed.xml" />
4157 < meta
4258 property = "og:image"
43- content = "/images/og/home-og.png"
59+ content = "https://codu.co /images/og/home-og.png"
4460 key = "og:image"
4561 />
4662 < meta property = "og:type" content = "website" />
@@ -49,7 +65,29 @@ const ArticlesPage = () => {
4965 < Layout >
5066 < div className = "border-t border-white" >
5167 < div className = "relative sm:mx-auto max-w-2xl mx-4" >
52- < PageHeading > Articles</ PageHeading >
68+ < div className = "my-8 border-b-2 pb-4 flex justify-between items-center" >
69+ < h1 className = "text-3xl tracking-tight font-extrabold text-gray-50 sm:text-4xl " >
70+ Articles
71+ </ h1 >
72+ < div >
73+ < label htmlFor = "filter" className = "sr-only" >
74+ Location
75+ </ label >
76+ < select
77+ id = "filter"
78+ name = "filter"
79+ className = "capitalize mt-2 block w-full rounded-md border-0 py-1.5 pl-3 pr-10 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-pink-600 sm:text-sm sm:leading-6 "
80+ onChange = { ( e ) => {
81+ router . push ( `/articles?filter=${ e . target . value } ` ) ;
82+ } }
83+ value = { selectedSortFilter }
84+ >
85+ < option > newest</ option >
86+ < option > oldest</ option >
87+ < option > top</ option >
88+ </ select >
89+ </ div >
90+ </ div >
5391 < section >
5492 { status === "error" && (
5593 < div > Something went wrong... Please refresh your page.</ div >
0 commit comments