Skip to content

Commit

Permalink
refactor: remove zonedDateTime from week days array
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Oct 3, 2024
1 parent 91efe0e commit 958cdb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
26 changes: 7 additions & 19 deletions src/hooks/useDatePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,16 @@ describe('useDatePicker hook', () => {
})
})
describe('highlighting today', () => {
const getDayByDate: (
const getDayByDate = (
calendarWeekDays: {
isToday: boolean
zdt: Temporal.ZonedDateTime
dateValue: string
}[][],
dayToFind: string
) => { calendarDate: string; isToday: boolean }[] = (
calendarWeekDays,
dayToFind
) => {
const days = calendarWeekDays
.flatMap((week) => week)
.map((day) => ({
...day,
calendarDate: formatDate(day.zdt, undefined, 'YYYY-MM-DD'),
}))

return days.filter((day) => day.calendarDate === dayToFind)
const days = calendarWeekDays.flatMap((week) => week)

return days.filter((day) => day.dateValue === dayToFind)
}

it('should highlight today date in a an ethiopic calendar', () => {
Expand Down Expand Up @@ -494,11 +486,7 @@ describe('clicking a day', () => {

// find and click the day passed to the calendar
for (let i = 0; i < days.length; i++) {
const formattedDate = formatDate(
days[i].zdt,
undefined,
'YYYY-MM-DD'
)
const formattedDate = days[i].dateValue
if (formattedDate === date) {
days[i].onClick()
break
Expand Down Expand Up @@ -528,7 +516,7 @@ describe('clicking a day', () => {
})
it('should call the callback with correct info for a custom (Nepali) calendar', () => {
const date = '2077-12-30'
const { calendarDate, calendarDateString } = renderForClick({
const { calendarDateString } = renderForClick({
calendar: 'nepali',
date,
})
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type DatePickerOptions = {
export type UseDatePickerReturn = UseNavigationReturnType & {
weekDayLabels: string[]
calendarWeekDays: {
zdt: Temporal.ZonedDateTime
dateValue: string
label: string | number
onClick: () => void
isSelected: boolean | undefined
Expand Down Expand Up @@ -185,10 +185,10 @@ export const useDatePicker: UseDatePickerHookType = ({
temporalCalendar,
temporalTimeZone,
])
return {
const result: UseDatePickerReturn = {
calendarWeekDays: calendarWeekDaysZdts.map((week) =>
week.map((weekDayZdt) => ({
zdt: weekDayZdt,
dateValue: formatDate(weekDayZdt, undefined, format),
label: localisationHelpers.localiseWeekLabel(
weekDayZdt.withCalendar(localeOptions.calendar),
localeOptions
Expand All @@ -208,4 +208,6 @@ export const useDatePicker: UseDatePickerHookType = ({
...navigation,
weekDayLabels,
}

return result
}
2 changes: 1 addition & 1 deletion src/utils/localisationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const localiseMonth = (
)

return isCustom
? customLocale?.monthNames[zdt.month - 1]
? customLocale?.monthNames[zdt.month! - 1]

Check warning on line 134 in src/utils/localisationHelpers.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
: zdt.toLocaleString(localeOptions.locale, format)
}

Expand Down

0 comments on commit 958cdb3

Please sign in to comment.