Skip to content

Commit

Permalink
Merge pull request #83 from dhis2/alpha
Browse files Browse the repository at this point in the history
chore: merge alpha to main
  • Loading branch information
kabaros authored Nov 21, 2024
2 parents 2508bed + d069b63 commit dc780ba
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 287 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [2.0.0-alpha.6](https://github.com/dhis2/multi-calendar-dates/compare/v2.0.0-alpha.5...v2.0.0-alpha.6) (2024-11-21)


### Bug Fixes

* check first week of subsequent year when geting period by date [LIBS-688] ([#74](https://github.com/dhis2/multi-calendar-dates/issues/74)) ([8662fe0](https://github.com/dhis2/multi-calendar-dates/commit/8662fe0e4f263c1abde1b813097e30b9b65ee31e))

## [1.3.2](https://github.com/dhis2/multi-calendar-dates/compare/v1.3.1...v1.3.2) (2024-10-09)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dhis2/multi-calendar-dates",
"version": "1.3.2",
"version": "2.0.0-alpha.6",
"license": "BSD-3-Clause",
"publishConfig": {
"access": "public"
Expand Down
32 changes: 25 additions & 7 deletions src/custom-calendars/nepaliCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ class NepaliCalendar extends Temporal.Calendar {
*
* A custom implementation of these methods is used to convert the calendar-space arguments to the ISO calendar.
*/
dateFromFields(fields: CalendarYMD): Temporal.PlainDate {
const { year, day, month } = _nepaliToIso({
year: fields.year,
month: fields.month,
day: fields.day,
})
dateFromFields(
fields: CalendarYMD,
options: Temporal.AssignmentOptions
): Temporal.PlainDate {
const { year, day, month } = _nepaliToIso(
{
year: fields.year,
month: fields.month,
day: fields.day,
},
options
)
return new Temporal.PlainDate(year, month, day, this)
}
yearMonthFromFields(fields: CalendarYMD): Temporal.PlainYearMonth {
Expand All @@ -96,7 +102,10 @@ const lastSupportedNepaliYear = Number(
supportedNepaliYears[supportedNepaliYears.length - 1]
)

const _nepaliToIso = (fields: { day: number; year: number; month: number }) => {
const _nepaliToIso = (
fields: { day: number; year: number; month: number },
{ overflow }: Temporal.AssignmentOptions = {}
) => {
let { year: nepaliYear } = fields

if (
Expand All @@ -109,6 +118,15 @@ const _nepaliToIso = (fields: { day: number; year: number; month: number }) => {
}
const { month: nepaliMonth, day: nepaliDay = 1 } = fields

if (
overflow === 'reject' &&
(nepaliMonth < 1 ||
nepaliMonth > 12 ||
nepaliDay > NEPALI_CALENDAR_DATA[nepaliYear][nepaliMonth])
) {
throw new Error('Invalid date in Nepali calendar')
}

let gregorianDayOfYear = 0

let monthCounter = nepaliMonth
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { useResolvedDirection } from './useResolvedDirection'
export { useDatePicker } from './useDatePicker'
export type { DatePickerOptions, OnDateSelectPayload } from './useDatePicker'
45 changes: 19 additions & 26 deletions src/hooks/useDatePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render } from '@testing-library/react'
import { renderHook } from '@testing-library/react-hooks'
import React from 'react'
import { SupportedCalendar } from '../types'
import { convertToIso8601 } from '../utils'
import localisationHelpers from '../utils/localisationHelpers'
import { useDatePicker, UseDatePickerReturn } from './useDatePicker'

Expand Down Expand Up @@ -270,16 +271,16 @@ describe('useDatePicker hook', () => {
})
})
describe('highlighting today', () => {
const getDayByDate: (
calendarWeekDays: { calendarDate: string; isToday: boolean }[][],
const getDayByDate = (
calendarWeekDays: {
isToday: boolean
dateValue: string
}[][],
dayToFind: string
) => { calendarDate: string; isToday: boolean }[] = (
calendarWeekDays,
dayToFind
) => {
const days = calendarWeekDays.flatMap((week) => week)

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

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

// find and click the day passed to the calendar
for (let i = 0; i < days.length; i++) {
if (days[i].calendarDate === date) {
const formattedDate = days[i].dateValue
if (formattedDate === date) {
days[i].onClick()
break
}
Expand All @@ -496,45 +498,36 @@ describe('clicking a day', () => {
}
it('should call the callback with correct info for Gregorian calendar', () => {
const date = '2018-01-22'
const { calendarDate, calendarDateString } = renderForClick({
const { calendarDateString } = renderForClick({
calendar: 'gregory',
date,
})
expect(calendarDate.toString()).toEqual(
'2018-01-22T00:00:00+02:00[Africa/Khartoum][u-ca=gregory]'
)
expect(calendarDateString).toEqual('2018-01-22')
})
it('should call the callback with correct info for Ethiopic calendar', () => {
const date = '2015-13-02'
const { calendarDate, calendarDateString } = renderForClick({
const { calendarDateString } = renderForClick({
calendar: 'ethiopic',
date,
})
expect(calendarDateString).toEqual('2015-13-02')
expect(
calendarDate.withCalendar('iso8601').toLocaleString('en-GB')
).toMatch('07/09/2023')

expect(
calendarDate.toLocaleString('en-GB', {
month: 'long',
year: 'numeric',
day: 'numeric',
calendar: 'ethiopic',
})
).toEqual('2 Pagumen 2015 ERA1')
const calendarDate = convertToIso8601(calendarDateString, 'ethiopic')
expect(calendarDate).toEqual({ day: 7, month: 9, year: 2023 })
})
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,
})
expect(calendarDateString).toEqual('2077-12-30')
expect(
localisationHelpers.localiseMonth(
calendarDate,
{
year: 20777,
month: 12,
day: 30,
},
{
locale: 'en-NP',
calendar: 'nepali',
Expand Down
35 changes: 11 additions & 24 deletions src/hooks/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import {
import { useResolvedLocaleOptions } from './internal/useResolvedLocaleOptions'
import { useWeekDayLabels } from './internal/useWeekDayLabels'

type DatePickerOptions = {
export type OnDateSelectPayload = {
calendarDateString: string
} | null

export type DatePickerOptions = {
date: string
options: PickerOptions
onDateSelect: ({
calendarDate,
calendarDateString,
}: {
calendarDate: Temporal.ZonedDateTime
calendarDateString: string
}) => void
onDateSelect: (payload: OnDateSelectPayload) => void
minDate?: string
maxDate?: string
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY'
Expand All @@ -36,26 +34,18 @@ type DatePickerOptions = {
export type UseDatePickerReturn = UseNavigationReturnType & {
weekDayLabels: string[]
calendarWeekDays: {
zdt: Temporal.ZonedDateTime
dateValue: string
label: string | number
calendarDate: string
onClick: () => void
isSelected: boolean | undefined
isToday: boolean
isInCurrentMonth: boolean
}[][]
isValid: boolean
warningMessage: string
errorMessage: string
}

type UseDatePickerHookType = (options: DatePickerOptions) => UseDatePickerReturn
type ValidatedDate = Temporal.YearOrEraAndEraYear &
Temporal.MonthOrMonthCode & {
day: number
isValid: boolean
warningMessage: string
errorMessage: string
format?: string
}

Expand Down Expand Up @@ -158,7 +148,6 @@ export const useDatePicker: UseDatePickerHookType = ({
const selectDate = useCallback(
(zdt: Temporal.ZonedDateTime) => {
onDateSelect({
calendarDate: zdt,
calendarDateString: formatDate(zdt, undefined, date.format),
})
},
Expand Down Expand Up @@ -196,11 +185,10 @@ export const useDatePicker: UseDatePickerHookType = ({
temporalCalendar,
temporalTimeZone,
])
return {
const result: UseDatePickerReturn = {
calendarWeekDays: calendarWeekDaysZdts.map((week) =>
week.map((weekDayZdt) => ({
zdt: weekDayZdt,
calendarDate: formatDate(weekDayZdt, undefined, date.format),
dateValue: formatDate(weekDayZdt, undefined, format),
label: localisationHelpers.localiseWeekLabel(
weekDayZdt.withCalendar(localeOptions.calendar),
localeOptions
Expand All @@ -219,8 +207,7 @@ export const useDatePicker: UseDatePickerHookType = ({
),
...navigation,
weekDayLabels,
isValid: date.isValid,
warningMessage: date.warningMessage,
errorMessage: date.errorMessage,
}

return result
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './period-calculation'
export {
getNowInCalendar,
validateDateString,
DateValidationResult,
convertFromIso8601,
convertToIso8601,
} from './utils'
35 changes: 7 additions & 28 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export const padWithZeroes = (number: number, count = 2) =>
type DayType = 'endOfMonth' | 'startOfMonth'

type customDate = Temporal.PlainDateLike & {
isValid: boolean
warningMessage?: string
errorMessage?: string
format?: string
}

Expand Down Expand Up @@ -77,20 +74,16 @@ export const extractAndValidateDateString = (
strictValidation?: boolean
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY'
}
): Temporal.PlainDateLike & {
isValid: boolean
warningMessage?: string
errorMessage?: string
} => {
): Temporal.PlainDateLike => {
if (!date) {
return getCurrentDateResult(options)
}

const validation = validateDateString(date, options)
if (validation.isValid) {
return getValidDateResult(date, validation, options)
if (!validation.error) {
return getValidDateResult(date, options)
} else {
return getInvalidDateResult(options, validation.errorMessage)
return getInvalidDateResult(options)
}
}

Expand All @@ -102,24 +95,13 @@ const getCurrentDateResult = (options: PickerOptions) => {
return { year, month, day, isValid: true }
}

const getValidDateResult = (
date: string,
validation: {
isValid: boolean
warningMessage?: string
errorMessage?: string
},
options: PickerOptions
) => {
const getValidDateResult = (date: string, options: PickerOptions) => {
const { year, month, day, format } = extractDatePartsFromDateString(date)
let result: customDate = {
year,
month,
day,
format,
isValid: validation.isValid,
warningMessage: validation.warningMessage,
errorMessage: validation.errorMessage,
}

if (options.calendar === 'ethiopic') {
Expand All @@ -129,15 +111,12 @@ const getValidDateResult = (
return result
}

const getInvalidDateResult = (
options: PickerOptions,
errorMessage?: string
) => {
const getInvalidDateResult = (options: PickerOptions) => {
const { year, month, day } = getNowInCalendar(
options.calendar,
options.timeZone
)
return { year, month, day, isValid: false, errorMessage }
return { year, month, day }
}

const adjustForEthiopicCalendar = (result: customDate) => {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export { default as fromAnyDate } from './from-any-date'
export { default as getNowInCalendar } from './getNowInCalendar'
export * from './helpers'
export { default as localisationHelpers } from './localisationHelpers'
export { validateDateString } from './validate-date-string'
export {
validateDateString,
DateValidationResult,
} from './validate-date-string'

export { convertFromIso8601, convertToIso8601 } from './convert-date'
8 changes: 6 additions & 2 deletions src/utils/localisationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ const localiseWeekLabel = (
}

const localiseMonth = (
zdt: Temporal.ZonedDateTime | Temporal.PlainYearMonth | Temporal.PlainDate,
zdt:
| Temporal.ZonedDateTime
| Temporal.PlainYearMonth
| Temporal.PlainDate
| Temporal.PlainDateLike,
localeOptions: PickerOptions,
format: Intl.DateTimeFormatOptions
) => {
Expand All @@ -127,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
Loading

0 comments on commit dc780ba

Please sign in to comment.