Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2031: Refactor getDate method #2512

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export { prepareFeatureLocations, prepareFeatureLocation } from './utils/geoJson
export { default as CategoriesMapModel } from './models/CategoriesMapModel'
export { default as CategoryModel } from './models/CategoryModel'
export { default as CityModel } from './models/CityModel'
export { default as DateModel } from './models/DateModel'
export { default as DateModel, type DateIcon } from './models/DateModel'
export { default as EventModel } from './models/EventModel'
export { default as LocalNewsModel } from './models/LocalNewsModel'
export { default as TunewsModel } from './models/TunewsModel'
Expand Down
18 changes: 18 additions & 0 deletions api-client/src/models/DateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RRule as RRuleType } from 'rrule'

const MAX_RECURRENCE_YEARS = 5

export type DateIcon = 'CalendarTodayRecurringIcon' | 'CalendarRecurringIcon' | 'CalendarTodayIcon'

class DateModel {
_startDate: DateTime
_endDate: DateTime
Expand Down Expand Up @@ -101,6 +103,22 @@ class DateModel {
return `${localizedStartDate} - ${localizedEndDate.toFormat(format)}`
}

getDateIcon(): { icon: DateIcon; label: string } | null {
const isRecurring = this.hasMoreRecurrencesThan(1)
const isToday = this.isToday

if (isRecurring && isToday) {
return { icon: 'CalendarTodayRecurringIcon', label: 'todayRecurring' }
}
if (isRecurring) {
return { icon: 'CalendarRecurringIcon', label: 'recurring' }
}
if (isToday) {
return { icon: 'CalendarTodayIcon', label: 'today' }
}
return null
}

private rruleToDateTime(date: Date): DateTime {
const dateTime = DateTime.fromJSDate(date)
// rrule is not correctly handling timezones and always returning local time in the shape of UTC
Expand Down
25 changes: 12 additions & 13 deletions native/src/components/EventListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
import { SvgProps } from 'react-native-svg'
import styled from 'styled-components/native'

import { DateModel, EventModel, parseHTML } from 'api-client'
import { DateModel, DateIcon, EventModel, parseHTML } from 'api-client'

import {
CalendarRecurringIcon,
Expand All @@ -25,19 +25,18 @@ const Description = styled.Text`
const placeholderThumbnails = [EventThumbnailPlaceholder1, EventThumbnailPlaceholder2, EventThumbnailPlaceholder3]

const getDateIcon = (date: DateModel): { icon: JSXElementConstructor<SvgProps>; label: string } | null => {
const isRecurring = date.hasMoreRecurrencesThan(1)
const isToday = date.isToday

if (isRecurring && isToday) {
return { icon: CalendarTodayRecurringIcon, label: 'todayRecurring' }
}
if (isRecurring) {
return { icon: CalendarRecurringIcon, label: 'recurring' }
}
if (isToday) {
return { icon: CalendarTodayIcon, label: 'today' }
const icons: { [key in DateIcon]: JSXElementConstructor<SvgProps> } = {
CalendarTodayRecurringIcon,
CalendarRecurringIcon,
CalendarTodayIcon,
}
return null
const iconToUse = date.getDateIcon()
return iconToUse
? {
icon: icons[iconToUse.icon],
label: iconToUse.label,
}
: null
}

type EventListItemProps = {
Expand Down
25 changes: 12 additions & 13 deletions web/src/components/EventListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ReactElement } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'

import { DateModel, EventModel, getExcerpt } from 'api-client'
import { DateModel, DateIcon, EventModel, getExcerpt } from 'api-client'

import {
CalendarRecurringIcon,
Expand Down Expand Up @@ -38,19 +38,18 @@ const getEventPlaceholder = (path: string): string => {
}

const getDateIcon = (date: DateModel): { icon: string; tooltip: string } | null => {
const isRecurring = date.hasMoreRecurrencesThan(1)
const isToday = date.isToday

if (isRecurring && isToday) {
return { icon: CalendarTodayRecurringIcon, tooltip: 'todayRecurring' }
}
if (isRecurring) {
return { icon: CalendarRecurringIcon, tooltip: 'recurring' }
}
if (isToday) {
return { icon: CalendarTodayIcon, tooltip: 'today' }
const icons: { [key in DateIcon]: string } = {
CalendarTodayRecurringIcon,
CalendarRecurringIcon,
CalendarTodayIcon,
}
return null
const iconToUse = date.getDateIcon()
return iconToUse
? {
icon: icons[iconToUse.icon],
tooltip: iconToUse.label,
}
: null
}

const EventListItem = ({ event, languageCode }: EventListItemProps): ReactElement => {
Expand Down