Skip to content

Commit 946fee5

Browse files
authored
Use date-fns (#109)
* Replace dayjs with date-fns for date handling across the application, updating package dependencies and modifying date formatting and comparison logic in various components and hooks. * Replace DayJS with date-fns in README for date utility functions
1 parent 3d6a9c3 commit 946fee5

9 files changed

Lines changed: 179 additions & 160 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The application production technology stack includes:
5050
- React Hook Form - form management
5151
- Zod - schema-based validation
5252
- Lodash - utility functions
53-
- DayJS - date utility functions
53+
- date-fns - date utility functions
5454
- i18next - internationalization framework
5555

5656
The application development technology stack includes:

package-lock.json

Lines changed: 160 additions & 143 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@tanstack/react-query-devtools": "5.91.3",
4444
"axios": "1.13.5",
4545
"classnames": "2.5.1",
46-
"dayjs": "1.11.19",
46+
"date-fns": "4.1.0",
4747
"i18next": "25.8.13",
4848
"i18next-browser-languagedetector": "8.2.1",
4949
"lodash": "4.17.23",

src/common/api/useGetUserTokens.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useQuery } from '@tanstack/react-query';
2-
import dayjs from 'dayjs';
2+
import { isBefore } from 'date-fns';
33

44
import { UserTokens } from 'common/models/auth';
55
import { QueryKey, StorageKey } from 'common/utils/constants';
@@ -20,8 +20,8 @@ export const useGetUserTokens = () => {
2020
if (storedTokens) {
2121
// tokens found
2222
const tokens = JSON.parse(storedTokens) as unknown as UserTokens;
23-
const now = dayjs();
24-
if (now.isBefore(tokens.expires_at)) {
23+
const now = new Date();
24+
if (isBefore(now, new Date(tokens.expires_at))) {
2525
// tokens not expired
2626
return resolve(tokens);
2727
} else {

src/common/components/Input/DateInput.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ModalCustomEvent } from '@ionic/core';
33
import { DatetimeCustomEvent, IonButton, IonDatetime, IonInput, IonModal } from '@ionic/react';
44
import { Control, FieldPath, FieldValues, useController } from 'react-hook-form';
55
import classNames from 'classnames';
6-
import dayjs from 'dayjs';
6+
import { format } from 'date-fns';
77

88
import './DateInput.scss';
99
import { PropsWithTestId } from '../types';
@@ -83,7 +83,7 @@ const DateInput = <T extends FieldValues>({
8383
const onChange = async (e: DatetimeCustomEvent): Promise<void> => {
8484
const value = e.detail.value as DateValue;
8585
if (value) {
86-
const isoDate = dayjs(value).format('YYYY-MM-DD');
86+
const isoDate = format(new Date(value), 'yyyy-MM-dd');
8787
field.onChange(isoDate);
8888
} else {
8989
field.onChange(null);
@@ -117,7 +117,7 @@ const DateInput = <T extends FieldValues>({
117117

118118
// format the value for the IonDatetime. it must be a local ISO date or null/undefined
119119
const datetimeValue = useMemo(() => {
120-
return field.value ? dayjs(field.value).format('YYYY-MM-DD[T]HH:mm') : null;
120+
return field.value ? format(new Date(field.value), "yyyy-MM-dd'T'HH:mm") : null;
121121
}, [field.value]);
122122

123123
return (

src/common/components/Input/DatetimeInput.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ModalCustomEvent } from '@ionic/core';
33
import { DatetimeCustomEvent, IonButton, IonDatetime, IonInput, IonModal } from '@ionic/react';
44
import { Control, FieldPath, FieldValues, useController } from 'react-hook-form';
55
import classNames from 'classnames';
6-
import dayjs from 'dayjs';
6+
import { format } from 'date-fns';
77

88
import './DatetimeInput.scss';
99
import { PropsWithTestId } from '../types';
@@ -94,7 +94,7 @@ const DatetimeInput = <T extends FieldValues>({
9494
const onChange = async (e: DatetimeCustomEvent): Promise<void> => {
9595
const value = e.detail.value as DatetimeValue;
9696
if (value) {
97-
const isoDate = dayjs(value).toISOString();
97+
const isoDate = new Date(value).toISOString();
9898
field.onChange(isoDate);
9999
} else {
100100
field.onChange(null);
@@ -129,7 +129,7 @@ const DatetimeInput = <T extends FieldValues>({
129129

130130
// format the value for the IonDatetime. it must be a local ISO date or null/undefined
131131
const datetimeValue = useMemo(() => {
132-
return field.value ? dayjs(field.value).format('YYYY-MM-DD[T]HH:mm') : null;
132+
return field.value ? format(new Date(field.value), "yyyy-MM-dd'T'HH:mm") : null;
133133
}, [field.value]);
134134

135135
return (

src/pages/Account/AccountPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@ionic/react';
1313
import { useState } from 'react';
1414
import { useTranslation } from 'react-i18next';
15-
import dayjs from 'dayjs';
15+
import { format } from 'date-fns';
1616

1717
import { PropsWithTestId } from 'common/components/types';
1818
import { config } from 'common/utils/config';
@@ -31,7 +31,7 @@ const AccountPage = ({ testid = 'page-account' }: PropsWithTestId) => {
3131
const router = useIonRouter();
3232
const { t } = useTranslation();
3333

34-
const versionTs = dayjs(config.VITE_BUILD_TS).format('YY.MM.DD.hhmm');
34+
const versionTs = format(new Date(config.VITE_BUILD_TS), 'yy.MM.dd.HHmm');
3535
const sha = config.VITE_BUILD_COMMIT_SHA.substring(0, 7);
3636
const version = `${versionTs}.${sha}`;
3737

src/pages/Account/components/Diagnostics/BuildDiagnostics.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IonItem, IonLabel, IonListHeader, IonText } from '@ionic/react';
22
import classNames from 'classnames';
3-
import dayjs from 'dayjs';
3+
import { format } from 'date-fns';
44
import { useTranslation } from 'react-i18next';
55

66
import { BaseComponentProps } from 'common/components/types';
@@ -31,7 +31,9 @@ const BuildDiagnostics = ({ className, testid = 'diagnostics-build' }: BaseCompo
3131
</IonItem>
3232
<IonItem className="text-sm">
3333
<IonLabel className="font-medium ion-margin-end">{t('diagnostics.label.time', { ns: 'account' })}</IonLabel>
34-
<IonText data-testid={`${testid}-time`}>{dayjs(config.VITE_BUILD_TS).format('YYYY-MM-DD HH:mm:ss Z')}</IonText>
34+
<IonText data-testid={`${testid}-time`}>
35+
{format(new Date(config.VITE_BUILD_TS), 'yyyy-MM-dd HH:mm:ss xxx')}
36+
</IonText>
3537
</IonItem>
3638
<IonItem className="text-sm">
3739
<IonLabel className="font-medium ion-margin-end">{t('diagnostics.label.sha', { ns: 'account' })}</IonLabel>

src/pages/Auth/SignIn/api/useSignIn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMutation, useQueryClient } from '@tanstack/react-query';
22
import find from 'lodash/find';
3-
import dayjs from 'dayjs';
3+
import { add } from 'date-fns';
44

55
import { User } from 'common/models/user';
66
import { UserTokens } from 'common/models/auth';
@@ -40,7 +40,7 @@ export const useSignIn = () => {
4040
storage.setItem(StorageKey.User, JSON.stringify(user));
4141

4242
// simulate the creation of authentication tokens
43-
const expires_at = dayjs().add(1, 'hour').toISOString();
43+
const expires_at = add(new Date(), { hours: 1 }).toISOString();
4444
const tokens: UserTokens = {
4545
access_token: 'access_token',
4646
id_token: 'id_token',

0 commit comments

Comments
 (0)