Skip to content

Commit 31895bf

Browse files
refactor(calendar-web): enhance type definitions and improve function signatures in calendar-utils
1 parent fc4b461 commit 31895bf

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

packages/pluggableWidgets/calendar-web/src/utils/calendar-utils.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as dateFns from "date-fns";
2-
import { Calendar, dateFnsLocalizer, ViewsProps } from "react-big-calendar";
3-
import withDragAndDrop from "react-big-calendar/lib/addons/dragAndDrop";
2+
import { Calendar, CalendarProps, dateFnsLocalizer, ViewsProps } from "react-big-calendar";
3+
import withDragAndDrop, { withDragAndDropProps } from "react-big-calendar/lib/addons/dragAndDrop";
44
import { CalendarContainerProps } from "../../typings/CalendarProps";
55
import { CustomToolbar } from "../components/Toolbar";
66

@@ -27,7 +27,7 @@ const localizer = dateFnsLocalizer({
2727

2828
export const DnDCalendar = withDragAndDrop(Calendar);
2929

30-
function getViewRange(view: string, date: Date) {
30+
function getViewRange(view: string, date: Date): { start: Date; end: Date } {
3131
switch (view) {
3232
case "month":
3333
return { start: dateFns.startOfMonth(date), end: dateFns.endOfMonth(date) };
@@ -44,13 +44,25 @@ function getViewRange(view: string, date: Date) {
4444
}
4545
}
4646

47-
export function eventPropGetter(event: CalEvent) {
47+
type EventPropGetterReturnType = {
48+
style:
49+
| {
50+
backgroundColor: string;
51+
}
52+
| undefined;
53+
};
54+
55+
export function eventPropGetter(event: CalEvent): EventPropGetterReturnType {
4856
return {
4957
style: event.color ? { backgroundColor: event.color } : undefined
5058
};
5159
}
5260

53-
export function extractCalendarProps(props: CalendarContainerProps) {
61+
interface DragAndDropCalendarProps<TEvent extends object = Event, TResource extends object = object>
62+
extends CalendarProps<TEvent, TResource>,
63+
withDragAndDropProps<TEvent, TResource> {}
64+
65+
export function extractCalendarProps(props: CalendarContainerProps): DragAndDropCalendarProps<CalEvent, object> {
5466
const items = props.databaseDataSource?.items ?? [];
5567
const events: CalEvent[] = items.map(item => {
5668
const title =
@@ -69,7 +81,7 @@ export function extractCalendarProps(props: CalendarContainerProps) {
6981
const viewsOption: ViewsProps<CalEvent, object> =
7082
props.view === "standard" ? ["day", "week", "month"] : ["month", "week", "work_week", "day", "agenda"];
7183

72-
const handleSelectEvent = (event: CalEvent) => {
84+
const handleSelectEvent = (event: CalEvent): void => {
7385
if (props.onClickEvent?.canExecute) {
7486
props.onClickEvent.execute({
7587
startDate: event.start,
@@ -80,7 +92,7 @@ export function extractCalendarProps(props: CalendarContainerProps) {
8092
}
8193
};
8294

83-
const handleSelectSlot = (slotInfo: { start: Date; end: Date; action: string }) => {
95+
const handleSelectSlot = (slotInfo: { start: Date; end: Date; action: string }): void => {
8496
if (props.enableCreate && props.onCreateEvent?.canExecute) {
8597
props.onCreateEvent.execute({
8698
startDate: slotInfo.start,
@@ -90,7 +102,7 @@ export function extractCalendarProps(props: CalendarContainerProps) {
90102
}
91103
};
92104

93-
const handleEventDropOrResize = ({ event, start, end }: { event: CalEvent; start: Date; end: Date }) => {
105+
const handleEventDropOrResize = ({ event, start, end }: { event: CalEvent; start: Date; end: Date }): void => {
94106
if (props.onChange?.canExecute) {
95107
props.onChange.execute({
96108
oldStart: event.start,
@@ -101,7 +113,7 @@ export function extractCalendarProps(props: CalendarContainerProps) {
101113
}
102114
};
103115

104-
const handleRangeChange = (date: Date, view: string) => {
116+
const handleRangeChange = (date: Date, view: string): void => {
105117
if (props.onRangeChange?.canExecute) {
106118
const { start, end } = getViewRange(view, date);
107119
props.onRangeChange.execute({

0 commit comments

Comments
 (0)