@@ -6,8 +6,13 @@ import CardEventAlt from "./ui/card-event-alt"
66import Button from "./ui/button"
77import Map from "../svg/map.svg"
88
9+ const pageSize = 9
10+
911const Events = ( { upcomingLimit, pastLimit } ) => {
1012 const date = new Date ( )
13+ const [ expanded , setExpanded ] = React . useState ( false )
14+ const [ search , setSearch ] = React . useState ( "" )
15+ const [ page , setPage ] = React . useState ( 0 )
1116 const query = useStaticQuery (
1217 graphql `
1318 query {
@@ -37,14 +42,28 @@ const Events = ({ upcomingLimit, pastLimit }) => {
3742 const startDate = new Date ( eventDate )
3843 return eventDate ? startDate > date : false
3944 } )
40- . sort ( ( a , b ) => a . node . frontmatter ?. date < b . node . frontmatter ?. date )
45+ . reverse ( )
4146
4247 const pastEvents = query . allMdx . edges . filter ( event => {
4348 const eventDate = event . node . frontmatter ?. date
4449 const startDate = new Date ( eventDate )
4550 return eventDate ? startDate < date : false
4651 } )
4752
53+ const showAll = expanded || pastEvents . length <= pastLimit
54+ const term = search . trim ( ) . toLowerCase ( )
55+ // Search covers every past event; pagination applies to the matches.
56+ const matchedPastEvents = showAll
57+ ? pastEvents . filter ( ( { node : { frontmatter : f } } ) =>
58+ `${ f ?. title } ${ f ?. location } ${ f ?. date } ` . toLowerCase ( ) . includes ( term )
59+ )
60+ : pastEvents . slice ( 0 , pastLimit )
61+ const pageCount = Math . max ( 1 , Math . ceil ( matchedPastEvents . length / pageSize ) )
62+ const currentPage = Math . min ( page , pageCount - 1 )
63+ const shownPastEvents = showAll
64+ ? matchedPastEvents . slice ( currentPage * pageSize , ( currentPage + 1 ) * pageSize )
65+ : matchedPastEvents
66+
4867 return (
4968 < div className = "relative pt-32 overflow-hidden" >
5069 < Map className = "absolute -z-1 top-12 left-[40%] h-auto w-[140%] sm:w-[1700px] text-[#4d8dff]" />
@@ -86,20 +105,68 @@ const Events = ({ upcomingLimit, pastLimit }) => {
86105 Past < span className = "text-accent" > Events</ span >
87106 </ h2 >
88107
108+ { showAll && (
109+ < input
110+ type = "search"
111+ value = { search }
112+ onChange = { e => {
113+ setSearch ( e . target . value )
114+ setPage ( 0 )
115+ } }
116+ placeholder = "Search past events"
117+ aria-label = "Search past events"
118+ className = "mt-6 w-full max-w-md rounded-xl bg-dark/20 px-4 py-3 text-white placeholder-light/70 focus:outline-none focus:ring-2 focus:ring-accent"
119+ />
120+ ) }
121+
89122 < Grid md = { 2 } xl = { 3 } className = "pr-6 pt-4 pb-6 lg:gap-12" >
90- { pastEvents . map (
91- ( item , index ) =>
92- index < pastLimit && (
93- < CardEventAlt
94- key = { index }
95- date = { item . node . frontmatter ?. date }
96- title = { item . node . frontmatter ?. title }
97- location = { item . node . frontmatter ?. location }
98- url = { item . node . frontmatter ?. recordings }
99- />
100- )
101- ) }
123+ { shownPastEvents . map ( ( item , index ) => (
124+ < CardEventAlt
125+ key = { index }
126+ date = { item . node . frontmatter ?. date }
127+ title = { item . node . frontmatter ?. title }
128+ location = { item . node . frontmatter ?. location }
129+ url = { item . node . frontmatter ?. recordings }
130+ />
131+ ) ) }
102132 </ Grid >
133+
134+ { showAll && shownPastEvents . length === 0 && (
135+ < p className = "mt-4" > No events match “{ search } ”.</ p >
136+ ) }
137+
138+ { showAll && pageCount > 1 && (
139+ < nav
140+ aria-label = "Past events pages"
141+ className = "flex items-center justify-center gap-6 mt-4 lg:justify-start" >
142+ < button
143+ type = "button"
144+ disabled = { currentPage === 0 }
145+ onClick = { ( ) => setPage ( currentPage - 1 ) }
146+ className = "font-bold text-accent hover:underline disabled:opacity-40 disabled:no-underline" >
147+ ← Newer
148+ </ button >
149+ < span >
150+ Page { currentPage + 1 } of { pageCount }
151+ </ span >
152+ < button
153+ type = "button"
154+ disabled = { currentPage === pageCount - 1 }
155+ onClick = { ( ) => setPage ( currentPage + 1 ) }
156+ className = "font-bold text-accent hover:underline disabled:opacity-40 disabled:no-underline" >
157+ Older →
158+ </ button >
159+ </ nav >
160+ ) }
161+
162+ { ! showAll && (
163+ < div className = "text-center mt-6 lg:text-left" >
164+ < Button
165+ onClick = { ( ) => setExpanded ( true ) }
166+ label = { `Show all ${ pastEvents . length } past events` }
167+ />
168+ </ div >
169+ ) }
103170 </ React . Fragment >
104171 ) }
105172 </ Container >
0 commit comments