nielsr HF staff commited on
Commit
a541d63
·
1 Parent(s): c6fd919

Fix date parsing

Browse files
src/components/ConferenceDialog.tsx CHANGED
@@ -6,7 +6,7 @@ import {
6
  } from "@/components/ui/dialog";
7
  import { CalendarDays, Globe, Tag, Clock, AlarmClock, CalendarPlus } from "lucide-react";
8
  import { Conference } from "@/types/conference";
9
- import { formatDistanceToNow, parseISO, isValid, format, parse } from "date-fns";
10
  import { Button } from "@/components/ui/button";
11
  import {
12
  DropdownMenu,
@@ -58,17 +58,59 @@ const ConferenceDialog = ({ conference, open, onOpenChange }: ConferenceDialogPr
58
 
59
  const createCalendarEvent = (type: 'google' | 'apple') => {
60
  try {
61
- let startDate = new Date();
62
- let endDate = new Date();
63
 
 
64
  if (conference.start && conference.end) {
65
- startDate = parseISO(conference.start);
66
- endDate = parseISO(conference.end);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  } else {
68
- startDate = parseDateFromString(conference.date);
69
- // Set end date to the same day for single-day conferences or when end date is not clear
70
- endDate = new Date(startDate);
71
- endDate.setDate(endDate.getDate() + 1); // Add one day for end date
 
 
72
  }
73
 
74
  const formatDateForGoogle = (date: Date) => format(date, "yyyyMMdd");
@@ -114,6 +156,7 @@ END:VCALENDAR`;
114
  }
115
  } catch (error) {
116
  console.error("Error creating calendar event:", error);
 
117
  }
118
  };
119
 
 
6
  } from "@/components/ui/dialog";
7
  import { CalendarDays, Globe, Tag, Clock, AlarmClock, CalendarPlus } from "lucide-react";
8
  import { Conference } from "@/types/conference";
9
+ import { formatDistanceToNow, parseISO, isValid, format, parse, addDays } from "date-fns";
10
  import { Button } from "@/components/ui/button";
11
  import {
12
  DropdownMenu,
 
58
 
59
  const createCalendarEvent = (type: 'google' | 'apple') => {
60
  try {
61
+ let startDate: Date;
62
+ let endDate: Date;
63
 
64
+ // Primary: Use start and end fields
65
  if (conference.start && conference.end) {
66
+ // If the dates are already Date objects, use them directly
67
+ if (conference.start instanceof Date && conference.end instanceof Date) {
68
+ startDate = conference.start;
69
+ endDate = conference.end;
70
+ } else {
71
+ // Otherwise, parse the string dates
72
+ startDate = parseISO(String(conference.start));
73
+ endDate = parseISO(String(conference.end));
74
+ }
75
+
76
+ // Validate parsed dates
77
+ if (!isValid(startDate) || !isValid(endDate)) {
78
+ throw new Error('Invalid conference dates');
79
+ }
80
+
81
+ // Add one day to end date to include the full last day
82
+ endDate = addDays(endDate, 1);
83
+ }
84
+ // Fallback: Parse from date field
85
+ else if (conference.date && typeof conference.date === 'string') {
86
+ const [monthDay, year] = conference.date.split(', ');
87
+ const [month, days] = monthDay.split(' ');
88
+ const [startDay, endDay] = days.split(/[-–]/);
89
+
90
+ try {
91
+ startDate = parse(`${month} ${startDay} ${year}`, 'MMMM d yyyy', new Date()) ||
92
+ parse(`${month} ${startDay} ${year}`, 'MMM d yyyy', new Date());
93
+
94
+ if (endDay) {
95
+ endDate = parse(`${month} ${endDay} ${year}`, 'MMMM d yyyy', new Date()) ||
96
+ parse(`${month} ${endDay} ${year}`, 'MMM d yyyy', new Date());
97
+ // Add one day to end date to include the full last day
98
+ endDate = addDays(endDate, 1);
99
+ } else {
100
+ // For single-day conferences
101
+ startDate = startDate || new Date();
102
+ endDate = addDays(startDate, 1);
103
+ }
104
+ } catch (parseError) {
105
+ throw new Error('Invalid date format');
106
+ }
107
  } else {
108
+ throw new Error('No valid date information found');
109
+ }
110
+
111
+ // Validate dates
112
+ if (!isValid(startDate) || !isValid(endDate)) {
113
+ throw new Error('Invalid conference dates');
114
  }
115
 
116
  const formatDateForGoogle = (date: Date) => format(date, "yyyyMMdd");
 
156
  }
157
  } catch (error) {
158
  console.error("Error creating calendar event:", error);
159
+ alert("Sorry, there was an error creating the calendar event. Please try again.");
160
  }
161
  };
162
 
src/pages/Calendar.tsx CHANGED
@@ -510,15 +510,20 @@ const CalendarPage = () => {
510
  showOutsideDays={false}
511
  className="bg-white rounded-lg p-6 shadow-sm mx-auto w-full"
512
  components={{
513
- Day: ({ date, ...props }) => {
514
- const isOutsideDay = date.getMonth() !== props.displayMonth.getMonth();
515
  if (isOutsideDay) {
516
  return null;
517
  }
518
  return (
519
- <button {...props} className="w-full h-full p-2">
 
 
 
 
 
520
  {renderDayContent(date)}
521
- </button>
522
  );
523
  },
524
  }}
@@ -549,12 +554,15 @@ const CalendarPage = () => {
549
  open={selectedDayEvents.date !== null}
550
  onOpenChange={() => setSelectedDayEvents({ date: null, events: { deadlines: [], conferences: [] } })}
551
  >
552
- <DialogContent className="max-w-2xl max-h-[80vh] overflow-y-auto">
 
 
 
553
  <DialogHeader>
554
  <DialogTitle>
555
  Events for {selectedDayEvents.date ? format(selectedDayEvents.date, 'MMMM d, yyyy') : ''}
556
  </DialogTitle>
557
- <div className="text-sm text-neutral-600">
558
  View conference details and deadlines for this date.
559
  </div>
560
  </DialogHeader>
 
510
  showOutsideDays={false}
511
  className="bg-white rounded-lg p-6 shadow-sm mx-auto w-full"
512
  components={{
513
+ Day: ({ date, displayMonth, ...props }) => {
514
+ const isOutsideDay = date.getMonth() !== displayMonth.getMonth();
515
  if (isOutsideDay) {
516
  return null;
517
  }
518
  return (
519
+ <div
520
+ role="button"
521
+ tabIndex={0}
522
+ {...props}
523
+ className="w-full h-full p-2 cursor-pointer"
524
+ >
525
  {renderDayContent(date)}
526
+ </div>
527
  );
528
  },
529
  }}
 
554
  open={selectedDayEvents.date !== null}
555
  onOpenChange={() => setSelectedDayEvents({ date: null, events: { deadlines: [], conferences: [] } })}
556
  >
557
+ <DialogContent
558
+ className="max-w-2xl max-h-[80vh] overflow-y-auto"
559
+ aria-describedby="dialog-description"
560
+ >
561
  <DialogHeader>
562
  <DialogTitle>
563
  Events for {selectedDayEvents.date ? format(selectedDayEvents.date, 'MMMM d, yyyy') : ''}
564
  </DialogTitle>
565
+ <div id="dialog-description" className="text-sm text-neutral-600">
566
  View conference details and deadlines for this date.
567
  </div>
568
  </DialogHeader>