Skip to content

Commit

Permalink
Filter Conferences by Date Range
Browse files Browse the repository at this point in the history
Work in progress

tech-conferences#749
  • Loading branch information
JuanPabloDiaz committed Sep 11, 2024
1 parent 20babec commit b7f7af3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/scenes/ConferenceList/ConferenceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import algoliasearch from 'algoliasearch/lite'
import { subMonths } from 'date-fns'
import qs from 'qs'
import React, { useMemo, useState } from 'react'
import DatePicker from 'react-datepicker'
import {
Configure,
InstantSearch,
Expand Down Expand Up @@ -41,6 +42,8 @@ import {
QUERY_SEPARATOR,
} from './utils'

import 'react-datepicker/dist/react-datepicker.css'

const CURRENT_YEAR = new Date().getFullYear()
const TODAY = new Date()

Expand Down Expand Up @@ -84,6 +87,16 @@ const ConferenceListPage: React.FC<Props> = ({
const [hitsPerPage, setHitsPerPage] = useState(600)
const [sortBy, setSortBy] = useState<SortBy>('startDate')
const [online, setOnline] = useState<OnlineOptions>('hybrid')
const [startDate, setStartDate] = useState<Date | null>(null)
const [endDate, setEndDate] = useState<Date | null>(null)

const handleStartDateSelect = (date: Date) => {
setStartDate(date)
}

const handleEndDateSelect = (date: Date) => {
setEndDate(date)
}

const [sortDirection] = useState<SortDirection>(showPast ? 'desc' : 'asc')
const [pastConferencePage, setPastConferencePage] = useState(
Expand Down Expand Up @@ -196,6 +209,29 @@ const ConferenceListPage: React.FC<Props> = ({
</Link>
</p>
<Search />
<div className={styles.dateRangePicker}>
<DatePicker
className={styles.customDatePicker}
selected={startDate}
onSelect={handleStartDateSelect}
onChange={() => {}}
selectsStart
startDate={startDate}
endDate={endDate}
placeholderText='Start Date'
/>
<DatePicker
className={styles.customDatePicker}
selected={endDate}
onSelect={handleEndDateSelect}
onChange={() => {}}
selectsEnd
startDate={startDate}
endDate={endDate}
placeholderText='End Date'
/>
</div>

<RefinementList
limit={40}
attribute='topics'
Expand Down Expand Up @@ -259,6 +295,8 @@ const ConferenceListPage: React.FC<Props> = ({
sortBy={sortBy}
sortDirection={sortDirection}
showCFP={showCFP}
startDate={startDate}
endDate={endDate}
/>
</InstantSearch>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@
font-size: 0.9em;
margin: spacing(extra-loose);
}

.dateRangePicker {
display: flex !important;
flex-direction: row !important; // Force it to be in a row
gap: 0.5rem !important;
align-items: center !important;
padding: 0.5rem !important;

.customDatePicker {
border: 1px solid #d1d5db !important;
padding: 0.25rem !important;
width: auto !important;
max-width: 12rem !important;
height: 35px !important;
color: black !important;
border-radius: 0.375rem !important;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPast, parseISO } from 'date-fns'
import { isPast, parseISO, isWithinInterval } from 'date-fns'
import { filter } from 'lodash'
import React from 'react'
import { connectInfiniteHits } from 'react-instantsearch-dom'
Expand All @@ -22,6 +22,8 @@ interface Props {
hasMore?: boolean
hits: Conference[]
onLoadMore(): void
startDate: Date | null
endDate: Date | null
}

const ConferenceList: React.FC<Props> = ({
Expand All @@ -31,6 +33,8 @@ const ConferenceList: React.FC<Props> = ({
sortDirection,
hasMore,
onLoadMore,
startDate,
endDate,
}) => {
let filteredConferences = hits as Conference[]

Expand All @@ -39,6 +43,20 @@ const ConferenceList: React.FC<Props> = ({
return conf.cfpEndDate && !isPast(parseISO(conf.cfpEndDate))
}) as Conference[]
}
if (startDate && endDate) {
filteredConferences = filteredConferences.filter((conf) => {
const conferenceStartDate = parseISO(conf.startDate)
const conferenceEndDate = parseISO(conf.endDate)
return (
isWithinInterval(conferenceStartDate, {
start: startDate,
end: endDate,
}) ||
isWithinInterval(conferenceEndDate, { start: startDate, end: endDate })
)
})
}

const confs = groupAndSortConferences(
filteredConferences,
sortBy,
Expand Down

0 comments on commit b7f7af3

Please sign in to comment.