Skip to content

Commit 46ffbdc

Browse files
committed
moving pure functions to util
1 parent da93cb1 commit 46ffbdc

File tree

3 files changed

+175
-174
lines changed

3 files changed

+175
-174
lines changed

src/components/CalendarBody.tsx

Lines changed: 4 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
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';
133
import { Table } from 'antd';
144

155
import {
166
GenericEvent,
17-
WeekObject,
187
CalendarBodyProps,
198
EventsObject,
209
EventBlockProps,
2110
ColumnNode,
22-
GetWeekDates,
2311
} from './types';
12+
import { getDayHoursEvents, sizeEventBox, MIN_BOX_SIZE } from './utils';
2413

2514
const BOX_POSITION_OFFSET = 26;
2615
const SCROLL_TO_ROW = 19;
@@ -81,85 +70,12 @@ function Calendar<T extends GenericEvent>({
8170
weekends,
8271
}: CalendarBodyProps<T>) {
8372
const rowRef = useRef<null | HTMLDivElement>(null);
84-
8573
useEffect(() => {
8674
if (rowRef.current) {
8775
rowRef.current?.scrollIntoView();
8876
}
8977
}, [rowRef]);
9078

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-
16379
const dayList = weekends
16480
? [
16581
'Monday',
@@ -237,7 +153,7 @@ function Calendar<T extends GenericEvent>({
237153
const tableColumns = [hourColumn, ...dayColumns];
238154

239155
return (
240-
<div className="dayViewContainer">
156+
<div>
241157
<Table
242158
rowKey={record => record.id}
243159
dataSource={getDayHoursEvents(weekDates, getDayEvents)}
@@ -267,23 +183,4 @@ function Calendar<T extends GenericEvent>({
267183
);
268184
}
269185

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-
289186
export default Calendar;

src/components/WeeklyCalendar.tsx

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
import React, { useState, useEffect } from 'react';
22
import { Card } from 'antd';
3-
import {
4-
getDay,
5-
eachDayOfInterval,
6-
startOfWeek,
7-
endOfWeek,
8-
isSameDay,
9-
isSameWeek,
10-
} from 'date-fns';
3+
import { startOfWeek, endOfWeek } from 'date-fns';
114

125
import Calendar from './CalendarBody';
136
import { CalendarHeader } from './CalendarHeader';
14-
15-
import {
16-
GenericEvent,
17-
CalendarContainerProps,
18-
WeekObject,
19-
DayName,
20-
} from './types';
7+
import { GenericEvent, CalendarContainerProps } from './types';
8+
import { daysToWeekObject } from './utils';
219

2210
export function WeeklyCalendar<T extends GenericEvent>({
2311
events,
@@ -42,58 +30,7 @@ export function WeeklyCalendar<T extends GenericEvent>({
4230
onSelectDate && onSelectDate(startWeek);
4331
}, [startWeek]);
4432

45-
const daysToWeekObject = <T extends GenericEvent>(events: T[]) => {
46-
const dayNames: DayName[] = [
47-
'sunday',
48-
'monday',
49-
'tuesday',
50-
'wednesday',
51-
'thursday',
52-
'friday',
53-
'saturday',
54-
];
55-
56-
const weekObject: WeekObject<T> = {
57-
sunday: [],
58-
monday: [],
59-
tuesday: [],
60-
wednesday: [],
61-
thursday: [],
62-
friday: [],
63-
saturday: [],
64-
};
65-
66-
if (events == null) {
67-
return weekObject;
68-
}
69-
70-
for (const googleEventIndex in events) {
71-
const eventStartTimeDay = events[googleEventIndex].startTime;
72-
const eventEndTimeDay = events[googleEventIndex].endTime;
73-
74-
if (!isSameDay(eventStartTimeDay, eventEndTimeDay)) {
75-
const result = eachDayOfInterval({
76-
start: eventStartTimeDay,
77-
end: eventEndTimeDay,
78-
});
79-
for (const dayInterval in result) {
80-
const splitedEvent = { ...events[googleEventIndex] };
81-
splitedEvent.startTime = result[dayInterval];
82-
splitedEvent.endTime = result[dayInterval];
83-
const weekObjectKey: DayName =
84-
dayNames[getDay(new Date(result[dayInterval]))];
85-
isSameWeek(startWeek, splitedEvent.startTime) &&
86-
weekObject[weekObjectKey].push(splitedEvent);
87-
}
88-
} else {
89-
const weekObjectKey: DayName = dayNames[getDay(eventStartTimeDay)];
90-
weekObject[weekObjectKey].push(events[googleEventIndex]);
91-
}
92-
}
93-
94-
return weekObject;
95-
};
96-
const weekObject = daysToWeekObject(events);
33+
const weekObject = daysToWeekObject(events, startWeek);
9734

9835
return (
9936
<Card>

0 commit comments

Comments
 (0)