|
1 | | -import React from 'react'; |
2 | | -import { |
3 | | - add, |
4 | | - isSameHour, |
5 | | - addHours, |
6 | | - startOfDay, |
7 | | - startOfWeek, |
8 | | - format, |
9 | | - getDay, |
10 | | - setDay, |
11 | | - differenceInMinutes, |
12 | | -} from 'date-fns'; |
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import { add, format, getDay, setDay, differenceInMinutes } from 'date-fns'; |
13 | 3 | import { Table } from 'antd'; |
14 | 4 |
|
15 | 5 | import { |
16 | 6 | GenericEvent, |
17 | | - WeekObject, |
18 | 7 | CalendarBodyProps, |
19 | 8 | EventsObject, |
20 | 9 | EventBlockProps, |
21 | 10 | ColumnNode, |
22 | | - GetWeekDates, |
23 | 11 | } from './types'; |
| 12 | +import { getDayHoursEvents, sizeEventBox, MIN_BOX_SIZE } from './utils'; |
24 | 13 |
|
25 | 14 | const BOX_POSITION_OFFSET = 26; |
26 | 15 | const SCROLL_TO_ROW = 19; |
@@ -81,85 +70,12 @@ function Calendar<T extends GenericEvent>({ |
81 | 70 | weekends, |
82 | 71 | }: CalendarBodyProps<T>) { |
83 | 72 | const rowRef = useRef<null | HTMLDivElement>(null); |
84 | | - |
85 | 73 | useEffect(() => { |
86 | 74 | if (rowRef.current) { |
87 | 75 | rowRef.current?.scrollIntoView(); |
88 | 76 | } |
89 | 77 | }, [rowRef]); |
90 | 78 |
|
91 | | - const getDayHoursEvents = ( |
92 | | - value: GetWeekDates, |
93 | | - weekObject: WeekObject<T> | undefined |
94 | | - ) => { |
95 | | - const ALL_DAY_EVENT = 0; |
96 | | - const events: EventsObject<T>[] = []; |
97 | | - |
98 | | - for (let i = 0; i < 26; i++) { |
99 | | - const startDate = add(startOfDay(startOfWeek(value.startDate)), { |
100 | | - days: 1, |
101 | | - }); |
102 | | - const hour = addHours(startDate, i - 1); |
103 | | - |
104 | | - events.push({ |
105 | | - id: i, |
106 | | - hourObject: hour, |
107 | | - hour: i != ALL_DAY_EVENT ? format(hour, 'hh a') : 'all-day', |
108 | | - Monday: |
109 | | - weekObject?.monday && |
110 | | - weekObject?.monday.filter(e => { |
111 | | - return e.allDay |
112 | | - ? i === ALL_DAY_EVENT |
113 | | - : isSameHour(e.startTime, hour); |
114 | | - }), |
115 | | - Tuesday: |
116 | | - weekObject?.tuesday && |
117 | | - weekObject?.tuesday.filter(e => { |
118 | | - return e.allDay |
119 | | - ? i === ALL_DAY_EVENT |
120 | | - : isSameHour(e.startTime, add(hour, { days: 1 })); |
121 | | - }), |
122 | | - Wednesday: |
123 | | - weekObject?.wednesday && |
124 | | - weekObject?.wednesday.filter(e => { |
125 | | - return e.allDay |
126 | | - ? i === ALL_DAY_EVENT |
127 | | - : isSameHour(e.startTime, add(hour, { days: 2 })); |
128 | | - }), |
129 | | - Thursday: |
130 | | - weekObject?.thursday && |
131 | | - weekObject?.thursday.filter(e => { |
132 | | - return e.allDay |
133 | | - ? i === ALL_DAY_EVENT |
134 | | - : isSameHour(e.startTime, add(hour, { days: 3 })); |
135 | | - }), |
136 | | - Friday: |
137 | | - weekObject?.friday && |
138 | | - weekObject?.friday.filter(e => { |
139 | | - return e.allDay |
140 | | - ? i === ALL_DAY_EVENT |
141 | | - : isSameHour(e.startTime, add(hour, { days: 4 })); |
142 | | - }), |
143 | | - Saturday: |
144 | | - weekObject?.saturday && |
145 | | - weekObject?.saturday.filter(e => { |
146 | | - return e.allDay |
147 | | - ? i === ALL_DAY_EVENT |
148 | | - : isSameHour(e.startTime, add(hour, { days: 5 })); |
149 | | - }), |
150 | | - Sunday: |
151 | | - weekObject?.sunday && |
152 | | - weekObject?.sunday.filter(e => { |
153 | | - return e.allDay |
154 | | - ? i === ALL_DAY_EVENT |
155 | | - : isSameHour(e.startTime, add(hour, { days: 6 })); |
156 | | - }), |
157 | | - }); |
158 | | - } |
159 | | - |
160 | | - return events; |
161 | | - }; |
162 | | - |
163 | 79 | const dayList = weekends |
164 | 80 | ? [ |
165 | 81 | 'Monday', |
@@ -237,7 +153,7 @@ function Calendar<T extends GenericEvent>({ |
237 | 153 | const tableColumns = [hourColumn, ...dayColumns]; |
238 | 154 |
|
239 | 155 | return ( |
240 | | - <div className="dayViewContainer"> |
| 156 | + <div> |
241 | 157 | <Table |
242 | 158 | rowKey={record => record.id} |
243 | 159 | dataSource={getDayHoursEvents(weekDates, getDayEvents)} |
@@ -267,23 +183,4 @@ function Calendar<T extends GenericEvent>({ |
267 | 183 | ); |
268 | 184 | } |
269 | 185 |
|
270 | | -const sizeEventBox = <T extends GenericEvent>(event: T, hour: Date) => { |
271 | | - const eventStartTime = new Date(event.startTime); |
272 | | - const eventEndTime = new Date(event.endTime); |
273 | | - const boxSize = |
274 | | - Math.floor( |
275 | | - differenceInMinutes(eventEndTime, eventStartTime) * HOUR_TO_DECIMAL |
276 | | - ) < MIN_BOX_SIZE |
277 | | - ? MIN_BOX_SIZE |
278 | | - : Math.floor( |
279 | | - differenceInMinutes(eventEndTime, eventStartTime) * HOUR_TO_DECIMAL |
280 | | - ); |
281 | | - const boxPosition = |
282 | | - differenceInMinutes(hour, eventStartTime) * HOUR_TO_DECIMAL > 100 |
283 | | - ? 0 |
284 | | - : differenceInMinutes(eventStartTime, hour) * HOUR_TO_DECIMAL; |
285 | | - |
286 | | - return { boxPosition: boxPosition, boxSize: boxSize }; |
287 | | -}; |
288 | | - |
289 | 186 | export default Calendar; |
0 commit comments