fix: improve date handling in PersonBirthdayCell and update formatting [skip ci]
This commit is contained in:
1 parent
fe5946dc94
commit
930e66396e
1 file changed
+40
-8
@@ -1,11 +1,10 @@
|
|||||||
import { IPerson } from '@/types/person'
|
import { IPerson } from '@/types/person'
|
||||||
import React, { useState } from 'react'
|
import React, { useEffect, useMemo, useState } from 'react'
|
||||||
import { DatePicker } from '../ui/datepicker'
|
import { DatePicker } from '../ui/datepicker'
|
||||||
import { updatePerson } from '@/handlers/api/people.handler';
|
import { updatePerson } from '@/handlers/api/people.handler';
|
||||||
import { formatDate } from '@/helpers/date.helper';
|
|
||||||
import { Input } from '../ui/input';
|
import { Input } from '../ui/input';
|
||||||
import { useToast } from '../ui/use-toast';
|
import { useToast } from '../ui/use-toast';
|
||||||
import { formatInTimeZone } from 'date-fns-tz';
|
import { format } from 'date-fns';
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import chrono from 'chrono-node'
|
import chrono from 'chrono-node'
|
||||||
@@ -16,16 +15,50 @@ interface IProps {
|
|||||||
person: IPerson
|
person: IPerson
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizeDateOnly = (value?: Date | string | null): Date | null => {
|
||||||
|
if (!value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
const dateOnlyMatch = value.match(/^(\d{4})-(\d{2})-(\d{2})/);
|
||||||
|
if (dateOnlyMatch) {
|
||||||
|
const [, year, month, day] = dateOnlyMatch;
|
||||||
|
return new Date(Number(year), Number(month) - 1, Number(day));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedDate = new Date(value);
|
||||||
|
if (Number.isNaN(parsedDate.getTime())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Date(
|
||||||
|
parsedDate.getUTCFullYear(),
|
||||||
|
parsedDate.getUTCMonth(),
|
||||||
|
parsedDate.getUTCDate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default function PersonBirthdayCell(
|
export default function PersonBirthdayCell(
|
||||||
{ person }: IProps
|
{ person }: IProps
|
||||||
) {
|
) {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||||
const [textDate, setTextDate] = useState<string | null>(person.birthDate ? formatInTimeZone(new Date(person.birthDate), 'UTC', 'PPP') : null);
|
const normalizedBirthDate = useMemo(() => normalizeDateOnly(person.birthDate), [person.birthDate]);
|
||||||
|
const [selectedDate, setSelectedDate] = useState<Date | null>(normalizedBirthDate);
|
||||||
|
const [textDate, setTextDate] = useState<string>(normalizedBirthDate ? format(normalizedBirthDate, 'PPP') : '');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setSelectedDate(normalizedBirthDate);
|
||||||
|
setTextDate(normalizedBirthDate ? format(normalizedBirthDate, 'PPP') : '');
|
||||||
|
}, [normalizedBirthDate]);
|
||||||
|
|
||||||
const handleEdit = (date?: Date | null) => {
|
const handleEdit = (date?: Date | null) => {
|
||||||
const formatedDate = date ? formatDate(date.toString(), 'yyyy-MM-dd') : null;
|
const formatedDate = date ? format(date, 'yyyy-MM-dd') : null;
|
||||||
|
setSelectedDate(date || null);
|
||||||
|
setTextDate(date ? format(date, 'PPP') : '');
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
return updatePerson(person.id, {
|
return updatePerson(person.id, {
|
||||||
birthDate: formatedDate,
|
birthDate: formatedDate,
|
||||||
@@ -48,14 +81,13 @@ export default function PersonBirthdayCell(
|
|||||||
<div className='flex gap-1'>
|
<div className='flex gap-1'>
|
||||||
<Input
|
<Input
|
||||||
className=''
|
className=''
|
||||||
value={textDate || ""}
|
value={textDate}
|
||||||
placeholder='Enter a date'
|
placeholder='Enter a date'
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
const parsedDate = chrono.parseDate(textDate || "");
|
const parsedDate = chrono.parseDate(textDate || "");
|
||||||
if (parsedDate) {
|
if (parsedDate) {
|
||||||
handleEdit(parsedDate);
|
handleEdit(parsedDate);
|
||||||
setTextDate(formatDate(parsedDate.toString(), 'PPP'));
|
|
||||||
} else {
|
} else {
|
||||||
handleEdit(null);
|
handleEdit(null);
|
||||||
}
|
}
|
||||||
@@ -64,7 +96,7 @@ export default function PersonBirthdayCell(
|
|||||||
disabled={loading}
|
disabled={loading}
|
||||||
onChange={(e) => setTextDate(e.target.value)}
|
onChange={(e) => setTextDate(e.target.value)}
|
||||||
/>
|
/>
|
||||||
<DatePicker date={person.birthDate} onSelect={handleEdit} iconOnly/>
|
<DatePicker date={selectedDate} onSelect={handleEdit} iconOnly/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user