-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcalendar.tsx
More file actions
260 lines (236 loc) · 7.25 KB
/
Copy pathcalendar.tsx
File metadata and controls
260 lines (236 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use client';
import React, { useState } from 'react';
import { Calendar as BigCalendar, momentLocalizer, Views, Event as BigCalendarEvent } from 'react-big-calendar';
import moment from 'moment';
import 'react-big-calendar/lib/css/react-big-calendar.css';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
moment.locale('en-GB');
const localizer = momentLocalizer(moment);
interface CalendarEvent {
id: number;
title: string;
start: Date;
end: Date;
allDay?: boolean;
}
const events: CalendarEvent[] = [
{
id: 0,
title: 'Board meeting',
start: new Date(2024, 7, 29, 9, 0, 0), // August 29, 2024, 9:00 AM
end: new Date(2024, 7, 29, 13, 0, 0), // August 29, 2024, 1:00 PM
},
{
id: 1,
title: 'MS training',
allDay: true,
start: new Date(2024, 7, 29, 14, 0, 0), // August 29, 2024, 2:00 PM
end: new Date(2024, 7, 29, 16, 30, 0), // August 29, 2024, 4:30 PM
},
{
id: 2,
title: 'Team lead meeting',
start: new Date(2024, 7, 29, 8, 30, 0), // August 29, 2024, 8:30 AM
end: new Date(2024, 7, 29, 12, 30, 0), // August 29, 2024, 12:30 PM
},
{
id: 11,
title: 'Birthday Party',
start: new Date(2024, 7, 30, 7, 0, 0), // August 30, 2024, 7:00 AM
end: new Date(2024, 7, 30, 10, 30, 0), // August 30, 2024, 10:30 AM
}
];
const styles = {
container: {
width: '80vw',
height: '60vh',
margin: '2em'
}
};
const TaskCalendar: React.FC = () => {
const [taskModalOpen, setTaskModalOpen] = useState(false);
const [taskDetail, setTaskDetail] = useState<{ selectedDate: string }>({ selectedDate: 'No Date Selected' });
const handleSelectEvent = (event: BigCalendarEvent) => {
const calendarEvent = event as CalendarEvent; // Type assertion
setTaskDetail({ selectedDate: calendarEvent.title });
setTaskModalOpen(true);
};
const handleClose = () => {
setTaskModalOpen(false);
setSelectedDate(null);
};
const handleSubmit = async () => {
try {
const newFormData: FormData = {
studentId,
date: formData.date,
timeSpent: workingHours,
notes,
};
const response = await fetch('http://localhost:3000/api/blogs', {
method: editingEvent ? 'PATCH' : 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...newFormData,
id: editingEvent?.id
}),
});
if (response.ok) {
const updatedEvent: CalendarEvent = await response.json();
// Refetch events to update the calendar
const fetchEvents = async () => {
try {
const response = await fetch(`http://localhost:3000/api/blogs?studentId=${studentId}`);
const data = await response.json();
setEvents(data);
} catch (error) {
console.error('Failed to fetch events:', error);
}
};
fetchEvents();
handleClose();
} else {
console.error('Failed to save event:', await response.json());
}
} catch (error) {
console.error('Error submitting form:', error);
}
};
const eventPropGetter = (event: CalendarEvent) => {
return {
style: {
backgroundColor: event.color || '#3174ad',
color: '#fff',
},
};
};
const handleNextMonth = () => {
setCurrentDate(moment(currentDate).add(1, 'months').toDate());
};
const handlePrevMonth = () => {
setCurrentDate(moment(currentDate).subtract(1, 'months').toDate());
};
const components = {
month: {
dateHeader: ({ date, label }: { date: Date; label: string }) => {
const dayEvents = events.filter((event) =>
moment(event.start).isSame(date, 'day')
);
return (
<div className="rbc-date-cell" style={{ padding: '5px' }}>
<span>{label}</span>
<div
style={{
maxHeight: '80px',
overflowY: 'auto',
marginTop: '5px',
}}
>
{dayEvents.map((event) => (
<div
key={event.id}
className="rbc-event"
style={{
backgroundColor: event.color,
padding: '2px',
margin: '2px 0',
cursor: 'pointer',
}}
onClick={() => handleSelectEvent(event)}
>
{event.title}
</div>
))}
</div>
</div>
);
},
},
};
// Filter events based on search query
// const filteredEvents = events.filter(event =>
// event.title.toLowerCase().includes(searchQuery.toLowerCase())
//);
return (
<>
<AlertDialog open={taskModalOpen} onOpenChange={setTaskModalOpen}>
<AlertDialogTrigger asChild>
<div />
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Task Detail</AlertDialogTitle>
</AlertDialogHeader>
<AlertDialogDescription>
<form>
<div className="mb-4">
<label>Date</label>
<Input
type="date"
// value={formData.date}
disabled
className="text-black"
/>
</div>
<div className="mb-4">
<label>Working Hours</label>
<Input
type="number"
// value={workingHours}
onChange={(e) => setWorkingHours(Number(e.target.value) || 0)}
placeholder="Enter working hours"
// disabled={!isEditable}
className="text-black"
/>
</div>
<div className="mb-4">
<label>Notes</label>
<Textarea
// value={notes}
onChange={(e) => setNotes(e.target.value)}
placeholder="Enter notes"
// disabled={!isEditable}
className="text-black"
/>
</div>
</form>
</AlertDialogDescription>
<AlertDialogFooter>
<AlertDialogCancel onClick={handleClose}>Close</AlertDialogCancel>
<AlertDialogAction onClick={handleClose}>OK</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<div style={styles.container}>
<BigCalendar
selectable
localizer={localizer}
events={events}
defaultView={Views.WEEK}
views={[Views.DAY, Views.WEEK, Views.MONTH]}
step={60}
defaultDate={new Date(2024, 7, 29)} // Default to August 29, 2024
startAccessor="start"
endAccessor="end"
onSelectEvent={handleSelectEvent}
/>
</div>
</>
);
};
export default TaskCalendar;