Skip to content

Commit

Permalink
VAGOV-CMS- 9474: Remove old recurring events from View Other Times fo…
Browse files Browse the repository at this point in the history
…r this event dropdown (#849)

Co-authored-by: Chris Kim <[email protected]>
Co-authored-by: Chris Kim <[email protected]>
Co-authored-by: Tim Cosgrove <[email protected]>
  • Loading branch information
4 people authored Jan 7, 2025
1 parent 343aa10 commit 33aad5a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
9 changes: 5 additions & 4 deletions playwright/tests/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ test.describe('Event Page', () => {
test('Event page renders with correct title and details', async ({
page,
}) => {
await page.goto('/central-iowa-health-care/events/52265/')
await expect(page.locator('h1')).toHaveText('Pickleball Club') // Replace with actual event title
await expect(page.locator('.va-introtext')).toContainText('Pickleball') // Replace with actual event description
await page.goto('/central-iowa-health-care/events/63096/')
await expect(page.locator('h1')).toHaveText(
'Battlefield Acupuncture Walk-in Clinic'
) // Replace with actual event title
})

test('Should render without a11y errors', async ({
page,
makeAxeBuilder,
}) => {
await page.goto('/central-iowa-health-care/events/52265/')
await page.goto('/central-iowa-health-care/events/63096/')

const accessibilityScanResults = await makeAxeBuilder().analyze()

Expand Down
18 changes: 18 additions & 0 deletions src/lib/utils/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
deriveMostRecentDate,
deriveFormattedTimestamp,
isEventInPast,
filterPastEvents,
} from './date'

describe('isISOString', () => {
Expand Down Expand Up @@ -245,6 +246,23 @@ describe('formatDateObject', () => {
})
})

describe('FilterPastEvents', () => {
it('should filter out past events from date time range for events', () => {
const datetimeRange = [
{
value: new Date('2023-09-07T14:00:00Z'),
end_value: new Date('2023-09-07T16:00:00Z'),
},
{
value: new Date('2999-09-07T14:00:00Z'),
end_value: new Date('2999-09-07T16:00:00Z'),
},
]
const result = filterPastEvents(datetimeRange)
expect(result).toHaveLength(1)
})
})

describe('deriveMostRecentDate', () => {
it('should return the closest future or ongoing event', () => {
const mockCurrentTime = new Date('2023-08-01T18:00:00Z').getTime()
Expand Down
11 changes: 10 additions & 1 deletion src/lib/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function getDateParts(

export const formatDateObject = (datetimeRange) => {
if (!datetimeRange) return []

return datetimeRange.map((dateObject) => {
const startTime = new Date(dateObject.value)
const endTime = new Date(dateObject.end_value)
Expand All @@ -187,6 +188,15 @@ export const formatDateObject = (datetimeRange) => {
})
}

export const filterPastEvents = (eventTimes) => {
if (!eventTimes) return []
const now = new Date()
return eventTimes.filter((dateObject) => {
const endTime = new Date(dateObject.end_value)
return endTime > now // Keep only if end_time is in the future
})
}

export const deriveMostRecentDate = (
datetimeRange,
now = Date.now() // This is done so that we can mock the current time in tests.
Expand Down Expand Up @@ -220,7 +230,6 @@ export const deriveFormattedTimestamp = (datetime) => {
if (!datetime) return
const startTime = new Date(datetime.startTime)
const endTime = new Date(datetime.endTime)

const formattedStartTime = startTime.toLocaleTimeString('en-US', {
weekday: 'short',
year: 'numeric',
Expand Down
6 changes: 4 additions & 2 deletions src/templates/layouts/event/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
formatDateObject,
deriveFormattedTimestamp,
isEventInPast,
filterPastEvents,
} from '@/lib/utils/date'
import { ContentFooter } from '@/templates/common/contentFooter'
import { MediaImage } from '@/templates/common/mediaImage'
Expand Down Expand Up @@ -42,9 +43,10 @@ export const Event = ({

// Memoized because formateDateObject returns a map that will be recalculated on each render.
const formattedDates = useMemo(
() => formatDateObject(datetimeRange),
() => formatDateObject(filterPastEvents(datetimeRange)),
[datetimeRange]
)

const initialFormattedDates = formattedDates.slice(0, 5)
const [currentFormattedDates, setCurrentFormattedDates] = useState(
initialFormattedDates
Expand Down Expand Up @@ -274,7 +276,7 @@ export const Event = ({
{body && <div dangerouslySetInnerHTML={{ __html: body?.processed }} />}

{/* Recurring Events */}
{formattedDates.length > 1 && (
{currentFormattedDates.length > 1 && (
<div>
<va-accordion open-single id="expand-recurring-events">
<va-accordion-item
Expand Down

0 comments on commit 33aad5a

Please sign in to comment.