Skip to content

Commit c912e15

Browse files
Fix invalid date handling
- Validate and guard against null or invalid selectedDate before calling getCalendarDay - Ensure calendarDates are built from valid month/calendar boundaries to prevent Invalid time value errors - Add defensive checks around date computations to avoid passing invalid Date objects to format and related helpers X-Lovable-Edit-ID: edt-9cd6e449-3d68-4c68-9738-0ac439f8fc50
2 parents f069b01 + c298f9b commit c912e15

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/pages/admin/FactoryCalendar.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,23 @@ export default function FactoryCalendar() {
145145
setLoading(false);
146146
};
147147

148-
const isDefaultWorkingDay = (date: Date): boolean => {
148+
const isDefaultWorkingDay = (date: Date | null | undefined): boolean => {
149+
if (!date || isNaN(date.getTime())) return false;
149150
// JavaScript: 0=Sunday, 1=Monday, ..., 6=Saturday
150151
// Our mask: Mon=1, Tue=2, Wed=4, Thu=8, Fri=16, Sat=32, Sun=64
151152
const jsDay = getDay(date);
152153
const maskBits = [64, 1, 2, 4, 8, 16, 32]; // Map JS day to our mask bits
153154
return (workingDaysMask & maskBits[jsDay]) !== 0;
154155
};
155156

156-
const getCalendarDay = (date: Date): CalendarDay | null => {
157+
const getCalendarDay = (date: Date | null | undefined): CalendarDay | null => {
158+
if (!date || isNaN(date.getTime())) return null;
157159
const dateStr = format(date, 'yyyy-MM-dd');
158160
return calendarDays.find(d => d.date === dateStr) || null;
159161
};
160162

161-
const getDayType = (date: Date): string => {
163+
const getDayType = (date: Date | null | undefined): string => {
164+
if (!date || isNaN(date.getTime())) return 'closure';
162165
const calDay = getCalendarDay(date);
163166
if (calDay) return calDay.day_type;
164167
return isDefaultWorkingDay(date) ? 'working' : 'closure';
@@ -630,7 +633,7 @@ export default function FactoryCalendar() {
630633
</div>
631634

632635
<DialogFooter className="flex gap-2 pt-4">
633-
{getCalendarDay(selectedDate!)?.id && (
636+
{selectedDate && getCalendarDay(selectedDate)?.id && (
634637
<Button
635638
variant="destructive"
636639
onClick={handleDeleteClick}

0 commit comments

Comments
 (0)