@isma91/react-scheduler 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +29 -0
- package/.github/workflows/tests.yml +35 -0
- package/.gitignore +32 -0
- package/.husky/pre-commit +2 -0
- package/.prettierignore +1 -0
- package/.prettierrc.json +7 -0
- package/.yarnrc.yml +1 -0
- package/LICENSE +24 -0
- package/README.md +172 -0
- package/dist/LICENSE +24 -0
- package/dist/README.md +172 -0
- package/dist/SchedulerComponent.d.ts +3 -0
- package/dist/components/common/Cell.d.ts +13 -0
- package/dist/components/common/LocaleArrow.d.ts +8 -0
- package/dist/components/common/ResourceHeader.d.ts +6 -0
- package/dist/components/common/Tabs.d.ts +16 -0
- package/dist/components/common/TodayTypo.d.ts +8 -0
- package/dist/components/common/WithResources.d.ts +6 -0
- package/dist/components/events/Actions.d.ts +8 -0
- package/dist/components/events/AgendaEventsList.d.ts +7 -0
- package/dist/components/events/CurrentTimeBar.d.ts +11 -0
- package/dist/components/events/EmptyAgenda.d.ts +2 -0
- package/dist/components/events/EventItem.d.ts +10 -0
- package/dist/components/events/EventItemPopover.d.ts +9 -0
- package/dist/components/events/MonthEvents.d.ts +13 -0
- package/dist/components/events/TodayEvents.d.ts +16 -0
- package/dist/components/hoc/DateProvider.d.ts +5 -0
- package/dist/components/inputs/DatePicker.d.ts +14 -0
- package/dist/components/inputs/Input.d.ts +19 -0
- package/dist/components/inputs/SelectInput.d.ts +22 -0
- package/dist/components/month/MonthTable.d.ts +8 -0
- package/dist/components/nav/DayDateBtn.d.ts +6 -0
- package/dist/components/nav/MonthDateBtn.d.ts +6 -0
- package/dist/components/nav/Navigation.d.ts +3 -0
- package/dist/components/nav/WeekDateBtn.d.ts +8 -0
- package/dist/components/week/WeekTable.d.ts +11 -0
- package/dist/helpers/constants.d.ts +4 -0
- package/dist/helpers/generals.d.ts +78 -0
- package/dist/hooks/useArrowDisable.d.ts +5 -0
- package/dist/hooks/useCellAttributes.d.ts +18 -0
- package/dist/hooks/useDragAttributes.d.ts +10 -0
- package/dist/hooks/useEventPermissions.d.ts +7 -0
- package/dist/hooks/useStore.d.ts +2 -0
- package/dist/hooks/useSyncScroll.d.ts +8 -0
- package/dist/hooks/useWindowResize.d.ts +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2853 -0
- package/dist/package.json +65 -0
- package/dist/positionManger/context.d.ts +14 -0
- package/dist/positionManger/provider.d.ts +5 -0
- package/dist/positionManger/usePosition.d.ts +4 -0
- package/dist/store/context.d.ts +2 -0
- package/dist/store/default.d.ts +245 -0
- package/dist/store/provider.d.ts +7 -0
- package/dist/store/types.d.ts +27 -0
- package/dist/styles/styles.d.ts +30 -0
- package/dist/types.d.ts +372 -0
- package/dist/views/Day.d.ts +2 -0
- package/dist/views/DayAgenda.d.ts +7 -0
- package/dist/views/Editor.d.ts +11 -0
- package/dist/views/Month.d.ts +2 -0
- package/dist/views/MonthAgenda.d.ts +7 -0
- package/dist/views/Week.d.ts +2 -0
- package/dist/views/WeekAgenda.d.ts +8 -0
- package/eslint.config.js +79 -0
- package/index.html +41 -0
- package/jest.config.ts +194 -0
- package/package.json +137 -0
- package/public/favicon.ico +0 -0
- package/public/logo192.png +0 -0
- package/public/logo512.png +0 -0
- package/public/manifest.json +25 -0
- package/public/robots.txt +3 -0
- package/scripts/post-pack.js +34 -0
- package/src/App.tsx +25 -0
- package/src/Page1.tsx +67 -0
- package/src/events.tsx +227 -0
- package/src/index.tsx +21 -0
- package/src/lib/SchedulerComponent.tsx +78 -0
- package/src/lib/__tests__/index.test.tsx +24 -0
- package/src/lib/components/common/Cell.tsx +52 -0
- package/src/lib/components/common/LocaleArrow.tsx +38 -0
- package/src/lib/components/common/ResourceHeader.tsx +73 -0
- package/src/lib/components/common/Tabs.tsx +119 -0
- package/src/lib/components/common/TodayTypo.tsx +44 -0
- package/src/lib/components/common/WithResources.tsx +98 -0
- package/src/lib/components/events/Actions.tsx +65 -0
- package/src/lib/components/events/AgendaEventsList.tsx +115 -0
- package/src/lib/components/events/CurrentTimeBar.tsx +59 -0
- package/src/lib/components/events/EmptyAgenda.tsx +27 -0
- package/src/lib/components/events/EventItem.tsx +180 -0
- package/src/lib/components/events/EventItemPopover.tsx +179 -0
- package/src/lib/components/events/MonthEvents.tsx +141 -0
- package/src/lib/components/events/TodayEvents.tsx +99 -0
- package/src/lib/components/hoc/DateProvider.tsx +19 -0
- package/src/lib/components/inputs/DatePicker.tsx +95 -0
- package/src/lib/components/inputs/Input.tsx +113 -0
- package/src/lib/components/inputs/SelectInput.tsx +164 -0
- package/src/lib/components/month/MonthTable.tsx +207 -0
- package/src/lib/components/nav/DayDateBtn.tsx +77 -0
- package/src/lib/components/nav/MonthDateBtn.tsx +80 -0
- package/src/lib/components/nav/Navigation.tsx +201 -0
- package/src/lib/components/nav/WeekDateBtn.tsx +89 -0
- package/src/lib/components/week/WeekTable.tsx +229 -0
- package/src/lib/helpers/constants.ts +4 -0
- package/src/lib/helpers/generals.tsx +354 -0
- package/src/lib/hooks/useArrowDisable.ts +26 -0
- package/src/lib/hooks/useCellAttributes.ts +67 -0
- package/src/lib/hooks/useDragAttributes.ts +31 -0
- package/src/lib/hooks/useEventPermissions.ts +42 -0
- package/src/lib/hooks/useStore.ts +8 -0
- package/src/lib/hooks/useSyncScroll.ts +31 -0
- package/src/lib/hooks/useWindowResize.ts +37 -0
- package/src/lib/index.tsx +14 -0
- package/src/lib/positionManger/context.ts +14 -0
- package/src/lib/positionManger/provider.tsx +113 -0
- package/src/lib/positionManger/usePosition.ts +8 -0
- package/src/lib/store/context.ts +5 -0
- package/src/lib/store/default.ts +157 -0
- package/src/lib/store/provider.tsx +211 -0
- package/src/lib/store/types.ts +33 -0
- package/src/lib/styles/styles.ts +256 -0
- package/src/lib/types.ts +423 -0
- package/src/lib/views/Day.tsx +265 -0
- package/src/lib/views/DayAgenda.tsx +57 -0
- package/src/lib/views/Editor.tsx +258 -0
- package/src/lib/views/Month.tsx +82 -0
- package/src/lib/views/MonthAgenda.tsx +84 -0
- package/src/lib/views/Week.tsx +92 -0
- package/src/lib/views/WeekAgenda.tsx +81 -0
- package/src/vite-env.d.ts +3 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +27 -0
- package/vite.config.js +40 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { PositionManagerState, PositionContext } from "./context";
|
|
3
|
+
import useStore from "../hooks/useStore";
|
|
4
|
+
import { DefaultResource, FieldProps, ProcessedEvent, ResourceFields } from "../types";
|
|
5
|
+
import {
|
|
6
|
+
getResourcedEvents,
|
|
7
|
+
sortEventsByTheEarliest,
|
|
8
|
+
sortEventsByTheLengthest,
|
|
9
|
+
} from "../helpers/generals";
|
|
10
|
+
import { eachDayOfInterval, format } from "date-fns";
|
|
11
|
+
import { View } from "../components/nav/Navigation";
|
|
12
|
+
|
|
13
|
+
type Props = {
|
|
14
|
+
children: React.ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const setEventPositions = (events: ProcessedEvent[]) => {
|
|
18
|
+
const slots: PositionManagerState["renderedSlots"][string] = {};
|
|
19
|
+
let position = 0;
|
|
20
|
+
for (let i = 0; i < events.length; i++) {
|
|
21
|
+
const event = events[i];
|
|
22
|
+
const eventLength = eachDayOfInterval({ start: event.start, end: event.end });
|
|
23
|
+
for (let i = 0; i < eventLength.length; i++) {
|
|
24
|
+
const day = format(eventLength[i], "yyyy-MM-dd");
|
|
25
|
+
if (slots[day]) {
|
|
26
|
+
const positions = Object.values(slots[day]);
|
|
27
|
+
while (positions.includes(position)) {
|
|
28
|
+
position += 1;
|
|
29
|
+
}
|
|
30
|
+
slots[day][event.event_id] = position;
|
|
31
|
+
} else {
|
|
32
|
+
slots[day] = { [event.event_id]: position };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// rest
|
|
37
|
+
position = 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return slots;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const setEventPositionsWithResources = (
|
|
44
|
+
events: ProcessedEvent[],
|
|
45
|
+
resources: DefaultResource[],
|
|
46
|
+
rFields: ResourceFields,
|
|
47
|
+
fields: FieldProps[],
|
|
48
|
+
view: View
|
|
49
|
+
) => {
|
|
50
|
+
const sorted =
|
|
51
|
+
view === "month" ? sortEventsByTheLengthest(events) : sortEventsByTheEarliest(events);
|
|
52
|
+
const slots: PositionManagerState["renderedSlots"] = {};
|
|
53
|
+
|
|
54
|
+
if (resources.length) {
|
|
55
|
+
for (const resource of resources) {
|
|
56
|
+
const resourcedEvents = getResourcedEvents(sorted, resource, rFields, fields);
|
|
57
|
+
const positions = setEventPositions(resourcedEvents);
|
|
58
|
+
slots[resource[rFields.idField]] = positions;
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
slots.all = setEventPositions(sorted);
|
|
62
|
+
}
|
|
63
|
+
return slots;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const PositionProvider = ({ children }: Props) => {
|
|
67
|
+
const { events, resources, resourceFields, fields, view } = useStore();
|
|
68
|
+
const [state, set] = useState<PositionManagerState>({
|
|
69
|
+
renderedSlots: setEventPositionsWithResources(events, resources, resourceFields, fields, view),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
set((prev) => ({
|
|
74
|
+
...prev,
|
|
75
|
+
renderedSlots: setEventPositionsWithResources(
|
|
76
|
+
events,
|
|
77
|
+
resources,
|
|
78
|
+
resourceFields,
|
|
79
|
+
fields,
|
|
80
|
+
view
|
|
81
|
+
),
|
|
82
|
+
}));
|
|
83
|
+
}, [events, fields, resourceFields, resources, view]);
|
|
84
|
+
|
|
85
|
+
const setRenderedSlot = (day: string, eventId: string, position: number, resourceId?: string) => {
|
|
86
|
+
set((prev) => ({
|
|
87
|
+
...prev,
|
|
88
|
+
renderedSlots: {
|
|
89
|
+
...prev.renderedSlots,
|
|
90
|
+
[resourceId || "all"]: {
|
|
91
|
+
...prev.renderedSlots?.[resourceId || "all"],
|
|
92
|
+
[day]: prev.renderedSlots?.[resourceId || "all"]?.[day]
|
|
93
|
+
? {
|
|
94
|
+
...prev.renderedSlots?.[resourceId || "all"]?.[day],
|
|
95
|
+
[eventId]: position,
|
|
96
|
+
}
|
|
97
|
+
: { [eventId]: position },
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<PositionContext.Provider
|
|
105
|
+
value={{
|
|
106
|
+
...state,
|
|
107
|
+
setRenderedSlot,
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
{children}
|
|
111
|
+
</PositionContext.Provider>
|
|
112
|
+
);
|
|
113
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { enUS } from "date-fns/locale";
|
|
2
|
+
import { SchedulerProps } from "../types";
|
|
3
|
+
import { getOneView, getTimeZonedDate } from "../helpers/generals";
|
|
4
|
+
|
|
5
|
+
const defaultMonth = {
|
|
6
|
+
weekDays: [0, 1, 2, 3, 4, 5, 6],
|
|
7
|
+
weekStartOn: 6,
|
|
8
|
+
startHour: 9,
|
|
9
|
+
endHour: 17,
|
|
10
|
+
navigation: true,
|
|
11
|
+
disableGoToDay: false,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const defaultWeek = {
|
|
15
|
+
weekDays: [0, 1, 2, 3, 4, 5, 6],
|
|
16
|
+
weekStartOn: 6,
|
|
17
|
+
startHour: 9,
|
|
18
|
+
endHour: 17,
|
|
19
|
+
step: 60,
|
|
20
|
+
navigation: true,
|
|
21
|
+
disableGoToDay: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const defaultDay = {
|
|
25
|
+
startHour: 9,
|
|
26
|
+
endHour: 17,
|
|
27
|
+
step: 60,
|
|
28
|
+
navigation: true,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const defaultResourceFields = {
|
|
32
|
+
idField: "assignee",
|
|
33
|
+
textField: "text",
|
|
34
|
+
subTextField: "subtext",
|
|
35
|
+
avatarField: "avatar",
|
|
36
|
+
colorField: "color",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const defaultTranslations = (trans: Partial<SchedulerProps["translations"]> = {}) => {
|
|
40
|
+
const { navigation, form, event, ...other } = trans;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
navigation: Object.assign(
|
|
44
|
+
{
|
|
45
|
+
month: "Month",
|
|
46
|
+
week: "Week",
|
|
47
|
+
day: "Day",
|
|
48
|
+
agenda: "Agenda",
|
|
49
|
+
today: "Today",
|
|
50
|
+
},
|
|
51
|
+
navigation
|
|
52
|
+
),
|
|
53
|
+
form: Object.assign(
|
|
54
|
+
{
|
|
55
|
+
addTitle: "Add Event",
|
|
56
|
+
editTitle: "Edit Event",
|
|
57
|
+
confirm: "Confirm",
|
|
58
|
+
delete: "Delete",
|
|
59
|
+
cancel: "Cancel",
|
|
60
|
+
},
|
|
61
|
+
form
|
|
62
|
+
),
|
|
63
|
+
event: Object.assign(
|
|
64
|
+
{
|
|
65
|
+
title: "Title",
|
|
66
|
+
start: "Start",
|
|
67
|
+
end: "End",
|
|
68
|
+
allDay: "All Day",
|
|
69
|
+
},
|
|
70
|
+
event
|
|
71
|
+
),
|
|
72
|
+
...Object.assign(
|
|
73
|
+
{ moreEvents: "More...", loading: "Loading...", noDataToDisplay: "No data to display" },
|
|
74
|
+
other
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const defaultViews = (props: Partial<SchedulerProps>) => {
|
|
80
|
+
const { month, week, day } = props;
|
|
81
|
+
return {
|
|
82
|
+
month: month !== null ? Object.assign(defaultMonth, month) : null,
|
|
83
|
+
week: week !== null ? Object.assign(defaultWeek, week) : null,
|
|
84
|
+
day: day !== null ? Object.assign(defaultDay, day) : null,
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const defaultProps = (props: Partial<SchedulerProps>) => {
|
|
89
|
+
// We're pulling values out of props that we don't want to
|
|
90
|
+
// pass on, so there are 'unused' ones here.
|
|
91
|
+
const {
|
|
92
|
+
translations,
|
|
93
|
+
resourceFields,
|
|
94
|
+
view,
|
|
95
|
+
agenda,
|
|
96
|
+
selectedDate,
|
|
97
|
+
resourceViewMode,
|
|
98
|
+
direction,
|
|
99
|
+
dialogMaxWidth,
|
|
100
|
+
hourFormat,
|
|
101
|
+
...otherProps
|
|
102
|
+
} = props;
|
|
103
|
+
|
|
104
|
+
const views = defaultViews(props);
|
|
105
|
+
const defaultView = view || "week";
|
|
106
|
+
const initialView = views[defaultView] ? defaultView : getOneView(views);
|
|
107
|
+
return {
|
|
108
|
+
...views,
|
|
109
|
+
translations: defaultTranslations(translations),
|
|
110
|
+
resourceFields: Object.assign(defaultResourceFields, resourceFields),
|
|
111
|
+
view: initialView,
|
|
112
|
+
selectedDate: getTimeZonedDate(selectedDate || new Date(), props.timeZone),
|
|
113
|
+
height: 600,
|
|
114
|
+
navigation: true,
|
|
115
|
+
disableViewNavigator: false,
|
|
116
|
+
events: [],
|
|
117
|
+
fields: [],
|
|
118
|
+
loading: undefined,
|
|
119
|
+
customEditor: undefined,
|
|
120
|
+
onConfirm: undefined,
|
|
121
|
+
onDelete: undefined,
|
|
122
|
+
viewerExtraComponent: undefined,
|
|
123
|
+
resources: [],
|
|
124
|
+
resourceHeaderComponent: undefined,
|
|
125
|
+
resourceViewMode: resourceViewMode || "default",
|
|
126
|
+
direction: direction || "ltr",
|
|
127
|
+
dialogMaxWidth: dialogMaxWidth || "md",
|
|
128
|
+
locale: enUS,
|
|
129
|
+
deletable: true,
|
|
130
|
+
editable: true,
|
|
131
|
+
hourFormat: hourFormat || "12",
|
|
132
|
+
draggable: true,
|
|
133
|
+
agenda,
|
|
134
|
+
enableAgenda: typeof agenda === "undefined" || agenda,
|
|
135
|
+
showCurrentTimeBar: true,
|
|
136
|
+
forceInlineMultiDay: false,
|
|
137
|
+
...otherProps,
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const initialStore = {
|
|
142
|
+
...defaultProps({}),
|
|
143
|
+
setProps: () => {},
|
|
144
|
+
dialog: false,
|
|
145
|
+
selectedRange: undefined,
|
|
146
|
+
selectedEvent: undefined,
|
|
147
|
+
selectedResource: undefined,
|
|
148
|
+
handleState: () => {},
|
|
149
|
+
getViews: () => [],
|
|
150
|
+
toggleAgenda: () => {},
|
|
151
|
+
triggerDialog: () => {},
|
|
152
|
+
triggerLoading: () => {},
|
|
153
|
+
handleGotoDay: () => {},
|
|
154
|
+
confirmEvent: () => {},
|
|
155
|
+
setCurrentDragged: () => {},
|
|
156
|
+
onDrop: () => {},
|
|
157
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { DragEvent, useEffect, useState } from "react";
|
|
2
|
+
import { EventActions, ProcessedEvent, SchedulerProps } from "../types";
|
|
3
|
+
import { defaultProps, initialStore } from "./default";
|
|
4
|
+
import { StoreContext } from "./context";
|
|
5
|
+
import { SchedulerState, SelectedRange, Store } from "./types";
|
|
6
|
+
import { arraytizeFieldVal, getAvailableViews } from "../helpers/generals";
|
|
7
|
+
import { addMinutes, differenceInMinutes, isEqual } from "date-fns";
|
|
8
|
+
import { View } from "../components/nav/Navigation";
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
initial: Partial<SchedulerProps>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const StoreProvider = ({ children, initial }: Props) => {
|
|
16
|
+
const [state, set] = useState<Store>({ ...initialStore, ...defaultProps(initial) });
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
set((prev) => ({
|
|
20
|
+
...prev,
|
|
21
|
+
onEventDrop: initial.onEventDrop,
|
|
22
|
+
customEditor: initial.customEditor,
|
|
23
|
+
customHeaderContent: initial.customHeaderContent,
|
|
24
|
+
events: initial.events || [],
|
|
25
|
+
}));
|
|
26
|
+
}, [initial.onEventDrop, initial.customEditor, initial.events, initial.customHeaderContent]);
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if ("undefined" !== typeof initial.loading) {
|
|
30
|
+
set((prev) => ({ ...prev, loading: initial.loading }));
|
|
31
|
+
}
|
|
32
|
+
}, [initial.loading]);
|
|
33
|
+
|
|
34
|
+
const handleState = (value: SchedulerState[keyof SchedulerState], name: keyof SchedulerState) => {
|
|
35
|
+
set((prev) => ({ ...prev, [name]: value }));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const getViews = () => {
|
|
39
|
+
return getAvailableViews(state);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const toggleAgenda = () => {
|
|
43
|
+
set((prev) => {
|
|
44
|
+
const newStatus = !prev.agenda;
|
|
45
|
+
|
|
46
|
+
if (state.onViewChange && typeof state.onViewChange === "function") {
|
|
47
|
+
state.onViewChange(state.view, newStatus);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { ...prev, agenda: newStatus };
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const triggerDialog = (status: boolean, selected?: SelectedRange | ProcessedEvent) => {
|
|
55
|
+
const isEvent = selected as ProcessedEvent;
|
|
56
|
+
|
|
57
|
+
set((prev) => ({
|
|
58
|
+
...prev,
|
|
59
|
+
dialog: status,
|
|
60
|
+
selectedRange: isEvent?.event_id
|
|
61
|
+
? undefined
|
|
62
|
+
: isEvent || {
|
|
63
|
+
start: new Date(),
|
|
64
|
+
end: new Date(Date.now() + 60 * 60 * 1000),
|
|
65
|
+
},
|
|
66
|
+
selectedEvent: isEvent?.event_id ? isEvent : undefined,
|
|
67
|
+
selectedResource: isEvent?.[state.resourceFields?.idField],
|
|
68
|
+
}));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const triggerLoading = (status: boolean) => {
|
|
72
|
+
// Trigger if not out-sourced by props
|
|
73
|
+
if (typeof initial.loading === "undefined") {
|
|
74
|
+
set((prev) => ({ ...prev, loading: status }));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const handleGotoDay = (day: Date) => {
|
|
79
|
+
const currentViews = getViews();
|
|
80
|
+
let view: View | undefined;
|
|
81
|
+
if (currentViews.includes("day")) {
|
|
82
|
+
view = "day";
|
|
83
|
+
set((prev) => ({ ...prev, view: "day", selectedDate: day }));
|
|
84
|
+
} else if (currentViews.includes("week")) {
|
|
85
|
+
view = "week";
|
|
86
|
+
set((prev) => ({ ...prev, view: "week", selectedDate: day }));
|
|
87
|
+
} else {
|
|
88
|
+
console.warn("No Day/Week views available");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!!view && state.onViewChange && typeof state.onViewChange === "function") {
|
|
92
|
+
state.onViewChange(view, state.agenda);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!!view && state.onSelectedDateChange && typeof state.onSelectedDateChange === "function") {
|
|
96
|
+
state.onSelectedDateChange(day);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const confirmEvent = (event: ProcessedEvent | ProcessedEvent[], action: EventActions) => {
|
|
101
|
+
let updatedEvents: ProcessedEvent[];
|
|
102
|
+
if (action === "edit") {
|
|
103
|
+
if (Array.isArray(event)) {
|
|
104
|
+
updatedEvents = state.events.map((e) => {
|
|
105
|
+
const exist = event.find((ex) => ex.event_id === e.event_id);
|
|
106
|
+
return exist ? { ...e, ...exist } : e;
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
updatedEvents = state.events.map((e) =>
|
|
110
|
+
e.event_id === event.event_id ? { ...e, ...event } : e
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
updatedEvents = state.events.concat(event);
|
|
115
|
+
}
|
|
116
|
+
set((prev) => ({ ...prev, events: updatedEvents }));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const setCurrentDragged = (event?: ProcessedEvent) => {
|
|
120
|
+
set((prev) => ({ ...prev, currentDragged: event }));
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const onDrop = async (
|
|
124
|
+
event: DragEvent<HTMLButtonElement>,
|
|
125
|
+
eventId: string,
|
|
126
|
+
startTime: Date,
|
|
127
|
+
resKey?: string,
|
|
128
|
+
resVal?: string | number
|
|
129
|
+
) => {
|
|
130
|
+
// Get dropped event
|
|
131
|
+
const droppedEvent = state.events.find((e) => {
|
|
132
|
+
if (typeof e.event_id === "number") {
|
|
133
|
+
return e.event_id === +eventId;
|
|
134
|
+
}
|
|
135
|
+
return e.event_id === eventId;
|
|
136
|
+
}) as ProcessedEvent;
|
|
137
|
+
|
|
138
|
+
// Check if has resource and if is multiple
|
|
139
|
+
const resField = state.fields.find((f) => f.name === resKey);
|
|
140
|
+
const isMultiple = !!resField?.config?.multiple;
|
|
141
|
+
let newResource = resVal as string | number | string[] | number[];
|
|
142
|
+
if (resField) {
|
|
143
|
+
const eResource = droppedEvent[resKey as string];
|
|
144
|
+
const currentRes = arraytizeFieldVal(resField, eResource, droppedEvent).value;
|
|
145
|
+
if (isMultiple) {
|
|
146
|
+
// if dropped on already owned resource
|
|
147
|
+
if (currentRes.includes(resVal)) {
|
|
148
|
+
// Omit if dropped on same time slot for multiple event
|
|
149
|
+
if (isEqual(droppedEvent.start, startTime)) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
newResource = currentRes;
|
|
153
|
+
} else {
|
|
154
|
+
// if have multiple resource ? add other : move to other
|
|
155
|
+
newResource = currentRes.length > 1 ? [...currentRes, resVal] : [resVal];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Omit if dropped on same time slot for non multiple events
|
|
161
|
+
if (isEqual(droppedEvent.start, startTime)) {
|
|
162
|
+
if (!newResource || (!isMultiple && newResource === droppedEvent[resKey as string])) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Update event time according to original duration & update resources/owners
|
|
168
|
+
const diff = differenceInMinutes(droppedEvent.end, droppedEvent.start);
|
|
169
|
+
const updatedEvent: ProcessedEvent = {
|
|
170
|
+
...droppedEvent,
|
|
171
|
+
start: startTime,
|
|
172
|
+
end: addMinutes(startTime, diff),
|
|
173
|
+
recurring: undefined,
|
|
174
|
+
[resKey as string]: newResource || "",
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// Local
|
|
178
|
+
if (!state.onEventDrop || typeof state.onEventDrop !== "function") {
|
|
179
|
+
return confirmEvent(updatedEvent, "edit");
|
|
180
|
+
}
|
|
181
|
+
// Remote
|
|
182
|
+
try {
|
|
183
|
+
triggerLoading(true);
|
|
184
|
+
const _event = await state.onEventDrop(event, startTime, updatedEvent, droppedEvent);
|
|
185
|
+
if (_event) {
|
|
186
|
+
confirmEvent(_event, "edit");
|
|
187
|
+
}
|
|
188
|
+
} finally {
|
|
189
|
+
triggerLoading(false);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return (
|
|
194
|
+
<StoreContext.Provider
|
|
195
|
+
value={{
|
|
196
|
+
...state,
|
|
197
|
+
handleState,
|
|
198
|
+
getViews,
|
|
199
|
+
toggleAgenda,
|
|
200
|
+
triggerDialog,
|
|
201
|
+
triggerLoading,
|
|
202
|
+
handleGotoDay,
|
|
203
|
+
confirmEvent,
|
|
204
|
+
setCurrentDragged,
|
|
205
|
+
onDrop,
|
|
206
|
+
}}
|
|
207
|
+
>
|
|
208
|
+
{children}
|
|
209
|
+
</StoreContext.Provider>
|
|
210
|
+
);
|
|
211
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { DragEvent } from "react";
|
|
2
|
+
import { View } from "../components/nav/Navigation";
|
|
3
|
+
import { DefaultResource, EventActions, ProcessedEvent, SchedulerProps } from "../types";
|
|
4
|
+
|
|
5
|
+
export type SelectedRange = { start: Date; end: Date };
|
|
6
|
+
|
|
7
|
+
export interface SchedulerState extends SchedulerProps {
|
|
8
|
+
dialog: boolean;
|
|
9
|
+
selectedRange?: SelectedRange;
|
|
10
|
+
selectedEvent?: ProcessedEvent;
|
|
11
|
+
selectedResource?: DefaultResource["assignee"] | DefaultResource["assignee"][];
|
|
12
|
+
selectedTab?: DefaultResource["assignee"];
|
|
13
|
+
currentDragged?: ProcessedEvent;
|
|
14
|
+
enableAgenda?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Store extends SchedulerState {
|
|
18
|
+
handleState(value: SchedulerState[keyof SchedulerState], name: keyof SchedulerState): void;
|
|
19
|
+
getViews(): View[];
|
|
20
|
+
toggleAgenda: () => void;
|
|
21
|
+
triggerDialog(status: boolean, event?: SelectedRange | ProcessedEvent): void;
|
|
22
|
+
triggerLoading(status: boolean): void;
|
|
23
|
+
handleGotoDay(day: Date): void;
|
|
24
|
+
confirmEvent(event: ProcessedEvent | ProcessedEvent[], action: EventActions): void;
|
|
25
|
+
setCurrentDragged(event?: ProcessedEvent): void;
|
|
26
|
+
onDrop(
|
|
27
|
+
event: DragEvent<HTMLButtonElement>,
|
|
28
|
+
eventId: string,
|
|
29
|
+
droppedStartTime: Date,
|
|
30
|
+
resourceKey?: string,
|
|
31
|
+
resourceVal?: string | number
|
|
32
|
+
): void;
|
|
33
|
+
}
|