@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,265 @@
|
|
|
1
|
+
import { useEffect, useCallback, Fragment } from "react";
|
|
2
|
+
import { Typography } from "@mui/material";
|
|
3
|
+
import {
|
|
4
|
+
format,
|
|
5
|
+
eachMinuteOfInterval,
|
|
6
|
+
isToday,
|
|
7
|
+
isBefore,
|
|
8
|
+
isAfter,
|
|
9
|
+
startOfDay,
|
|
10
|
+
endOfDay,
|
|
11
|
+
addDays,
|
|
12
|
+
addMinutes,
|
|
13
|
+
set,
|
|
14
|
+
} from "date-fns";
|
|
15
|
+
import TodayTypo from "../components/common/TodayTypo";
|
|
16
|
+
import EventItem from "../components/events/EventItem";
|
|
17
|
+
import { DefaultResource, ProcessedEvent } from "../types";
|
|
18
|
+
import {
|
|
19
|
+
calcCellHeight,
|
|
20
|
+
calcMinuteHeight,
|
|
21
|
+
filterMultiDaySlot,
|
|
22
|
+
filterTodayEvents,
|
|
23
|
+
getHourFormat,
|
|
24
|
+
getResourcedEvents,
|
|
25
|
+
} from "../helpers/generals";
|
|
26
|
+
import { WithResources } from "../components/common/WithResources";
|
|
27
|
+
import Cell from "../components/common/Cell";
|
|
28
|
+
import TodayEvents from "../components/events/TodayEvents";
|
|
29
|
+
import { TableGrid } from "../styles/styles";
|
|
30
|
+
import { MULTI_DAY_EVENT_HEIGHT } from "../helpers/constants";
|
|
31
|
+
import useStore from "../hooks/useStore";
|
|
32
|
+
import { DayAgenda } from "./DayAgenda";
|
|
33
|
+
|
|
34
|
+
const Day = () => {
|
|
35
|
+
const {
|
|
36
|
+
day,
|
|
37
|
+
selectedDate,
|
|
38
|
+
events,
|
|
39
|
+
height,
|
|
40
|
+
getRemoteEvents,
|
|
41
|
+
triggerLoading,
|
|
42
|
+
handleState,
|
|
43
|
+
resources,
|
|
44
|
+
resourceFields,
|
|
45
|
+
resourceViewMode,
|
|
46
|
+
fields,
|
|
47
|
+
direction,
|
|
48
|
+
locale,
|
|
49
|
+
hourFormat,
|
|
50
|
+
timeZone,
|
|
51
|
+
stickyNavigation,
|
|
52
|
+
agenda,
|
|
53
|
+
stickyNavigationOffset,
|
|
54
|
+
stickyNavigationHeight,
|
|
55
|
+
currentTime,
|
|
56
|
+
showCurrentTimeBar,
|
|
57
|
+
currentTimeBarColor,
|
|
58
|
+
forceInlineMultiDay,
|
|
59
|
+
} = useStore();
|
|
60
|
+
|
|
61
|
+
const { startHour, endHour, step, cellRenderer, headRenderer, hourRenderer } = day!;
|
|
62
|
+
const START_TIME = set(selectedDate, { hours: startHour, minutes: 0, seconds: 0 });
|
|
63
|
+
const END_TIME = set(selectedDate, { hours: endHour, minutes: -step, seconds: 0 });
|
|
64
|
+
const hours = eachMinuteOfInterval(
|
|
65
|
+
{
|
|
66
|
+
start: START_TIME,
|
|
67
|
+
end: END_TIME,
|
|
68
|
+
},
|
|
69
|
+
{ step: step }
|
|
70
|
+
);
|
|
71
|
+
const CELL_HEIGHT = calcCellHeight(height, hours.length);
|
|
72
|
+
const MINUTE_HEIGHT = calcMinuteHeight(CELL_HEIGHT, step);
|
|
73
|
+
const hFormat = getHourFormat(hourFormat);
|
|
74
|
+
|
|
75
|
+
const fetchEvents = useCallback(async () => {
|
|
76
|
+
try {
|
|
77
|
+
triggerLoading(true);
|
|
78
|
+
const start = addDays(START_TIME, -1);
|
|
79
|
+
const end = addDays(END_TIME, 1);
|
|
80
|
+
const events = await getRemoteEvents!({
|
|
81
|
+
start,
|
|
82
|
+
end,
|
|
83
|
+
view: "day",
|
|
84
|
+
});
|
|
85
|
+
if (events && events?.length) {
|
|
86
|
+
handleState(events, "events");
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw error;
|
|
90
|
+
} finally {
|
|
91
|
+
triggerLoading(false);
|
|
92
|
+
}
|
|
93
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
94
|
+
}, [getRemoteEvents]);
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (getRemoteEvents instanceof Function) {
|
|
98
|
+
fetchEvents();
|
|
99
|
+
}
|
|
100
|
+
}, [fetchEvents, getRemoteEvents]);
|
|
101
|
+
|
|
102
|
+
const renderMultiDayEvents = useCallback(
|
|
103
|
+
(events: ProcessedEvent[]) => {
|
|
104
|
+
const todayMulti = filterMultiDaySlot(events, selectedDate, timeZone, forceInlineMultiDay);
|
|
105
|
+
return (
|
|
106
|
+
<div
|
|
107
|
+
className="rs__block_col"
|
|
108
|
+
style={{ height: MULTI_DAY_EVENT_HEIGHT * todayMulti.length }}
|
|
109
|
+
>
|
|
110
|
+
{todayMulti.map((event, i) => {
|
|
111
|
+
const hasPrev = isBefore(event.start, startOfDay(selectedDate));
|
|
112
|
+
const hasNext = isAfter(event.end, endOfDay(selectedDate));
|
|
113
|
+
return (
|
|
114
|
+
<div
|
|
115
|
+
key={event.event_id}
|
|
116
|
+
className="rs__multi_day"
|
|
117
|
+
style={{
|
|
118
|
+
top: i * MULTI_DAY_EVENT_HEIGHT,
|
|
119
|
+
width: "99.9%",
|
|
120
|
+
overflowX: "hidden",
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
<EventItem event={event} multiday hasPrev={hasPrev} hasNext={hasNext} />
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
})}
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
},
|
|
130
|
+
[selectedDate, timeZone]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const renderTable = useCallback(
|
|
134
|
+
(resource?: DefaultResource) => {
|
|
135
|
+
let resourcedEvents = events;
|
|
136
|
+
if (resource) {
|
|
137
|
+
resourcedEvents = getResourcedEvents(events, resource, resourceFields, fields);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (agenda) {
|
|
141
|
+
return <DayAgenda resource={resource} events={resourcedEvents} />;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Equalizing multi-day section height
|
|
145
|
+
const shouldEqualize = resources.length && resourceViewMode === "default";
|
|
146
|
+
const allWeekMulti = filterMultiDaySlot(
|
|
147
|
+
shouldEqualize ? events : resourcedEvents,
|
|
148
|
+
selectedDate,
|
|
149
|
+
timeZone,
|
|
150
|
+
forceInlineMultiDay
|
|
151
|
+
);
|
|
152
|
+
const headerHeight = MULTI_DAY_EVENT_HEIGHT * allWeekMulti.length + 45;
|
|
153
|
+
return (
|
|
154
|
+
<>
|
|
155
|
+
{/* Header */}
|
|
156
|
+
<TableGrid
|
|
157
|
+
days={1}
|
|
158
|
+
sticky="1"
|
|
159
|
+
stickyNavigation={stickyNavigation}
|
|
160
|
+
stickyOffset={stickyNavigationOffset}
|
|
161
|
+
stickyHeight={stickyNavigationHeight}
|
|
162
|
+
>
|
|
163
|
+
<span className="rs__cell"></span>
|
|
164
|
+
<span
|
|
165
|
+
className={`rs__cell rs__header ${isToday(selectedDate) ? "rs__today_cell" : ""}`}
|
|
166
|
+
style={{ height: headerHeight }}
|
|
167
|
+
>
|
|
168
|
+
{typeof headRenderer === "function" ? (
|
|
169
|
+
<div>{headRenderer({ day: selectedDate, events: resourcedEvents, resource })}</div>
|
|
170
|
+
) : (
|
|
171
|
+
<TodayTypo date={selectedDate} locale={locale} />
|
|
172
|
+
)}
|
|
173
|
+
{renderMultiDayEvents(resourcedEvents)}
|
|
174
|
+
</span>
|
|
175
|
+
</TableGrid>
|
|
176
|
+
<TableGrid days={1}>
|
|
177
|
+
{/* Body */}
|
|
178
|
+
{hours.map((h, i) => {
|
|
179
|
+
const start = new Date(`${format(selectedDate, "yyyy/MM/dd")} ${format(h, hFormat)}`);
|
|
180
|
+
const end = addMinutes(start, step);
|
|
181
|
+
const field = resourceFields.idField;
|
|
182
|
+
return (
|
|
183
|
+
<Fragment key={i}>
|
|
184
|
+
{/* Time Cells */}
|
|
185
|
+
<span className="rs__cell rs__header rs__time" style={{ height: CELL_HEIGHT }}>
|
|
186
|
+
{typeof hourRenderer === "function" ? (
|
|
187
|
+
<div>{hourRenderer(format(h, hFormat, { locale }))}</div>
|
|
188
|
+
) : (
|
|
189
|
+
<Typography variant="caption">{format(h, hFormat, { locale })}</Typography>
|
|
190
|
+
)}
|
|
191
|
+
</span>
|
|
192
|
+
<span className={`rs__cell ${isToday(selectedDate) ? "rs__today_cell" : ""}`}>
|
|
193
|
+
{/* Events of this day - run once on the top hour column */}
|
|
194
|
+
{i === 0 && (
|
|
195
|
+
<TodayEvents
|
|
196
|
+
todayEvents={filterTodayEvents(
|
|
197
|
+
resourcedEvents,
|
|
198
|
+
selectedDate,
|
|
199
|
+
timeZone,
|
|
200
|
+
forceInlineMultiDay
|
|
201
|
+
)}
|
|
202
|
+
today={START_TIME}
|
|
203
|
+
minuteHeight={MINUTE_HEIGHT}
|
|
204
|
+
startHour={startHour}
|
|
205
|
+
endHour={endHour}
|
|
206
|
+
step={step}
|
|
207
|
+
direction={direction}
|
|
208
|
+
timeZone={timeZone}
|
|
209
|
+
currentTime={currentTime}
|
|
210
|
+
showCurrentTimeBar={showCurrentTimeBar}
|
|
211
|
+
currentTimeBarColor={currentTimeBarColor}
|
|
212
|
+
/>
|
|
213
|
+
)}
|
|
214
|
+
{/* Cell */}
|
|
215
|
+
<Cell
|
|
216
|
+
start={start}
|
|
217
|
+
end={end}
|
|
218
|
+
day={selectedDate}
|
|
219
|
+
height={CELL_HEIGHT}
|
|
220
|
+
resourceKey={field}
|
|
221
|
+
resourceVal={resource ? resource[field] : null}
|
|
222
|
+
cellRenderer={cellRenderer}
|
|
223
|
+
/>
|
|
224
|
+
</span>
|
|
225
|
+
</Fragment>
|
|
226
|
+
);
|
|
227
|
+
})}
|
|
228
|
+
</TableGrid>
|
|
229
|
+
</>
|
|
230
|
+
);
|
|
231
|
+
},
|
|
232
|
+
[
|
|
233
|
+
CELL_HEIGHT,
|
|
234
|
+
MINUTE_HEIGHT,
|
|
235
|
+
START_TIME,
|
|
236
|
+
agenda,
|
|
237
|
+
cellRenderer,
|
|
238
|
+
direction,
|
|
239
|
+
endHour,
|
|
240
|
+
events,
|
|
241
|
+
fields,
|
|
242
|
+
hFormat,
|
|
243
|
+
headRenderer,
|
|
244
|
+
hourRenderer,
|
|
245
|
+
hours,
|
|
246
|
+
locale,
|
|
247
|
+
renderMultiDayEvents,
|
|
248
|
+
resourceFields,
|
|
249
|
+
resourceViewMode,
|
|
250
|
+
resources.length,
|
|
251
|
+
selectedDate,
|
|
252
|
+
startHour,
|
|
253
|
+
step,
|
|
254
|
+
stickyNavigation,
|
|
255
|
+
timeZone,
|
|
256
|
+
currentTime,
|
|
257
|
+
showCurrentTimeBar,
|
|
258
|
+
currentTimeBarColor,
|
|
259
|
+
]
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
return resources.length ? <WithResources renderChildren={renderTable} /> : renderTable();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export { Day };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { format } from "date-fns";
|
|
3
|
+
import { AgendaDiv } from "../styles/styles";
|
|
4
|
+
import { DefaultResource, ProcessedEvent } from "../types";
|
|
5
|
+
import useStore from "../hooks/useStore";
|
|
6
|
+
import { Typography } from "@mui/material";
|
|
7
|
+
import { filterTodayAgendaEvents } from "../helpers/generals";
|
|
8
|
+
import AgendaEventsList from "../components/events/AgendaEventsList";
|
|
9
|
+
import EmptyAgenda from "../components/events/EmptyAgenda";
|
|
10
|
+
|
|
11
|
+
type Props = {
|
|
12
|
+
events: ProcessedEvent[];
|
|
13
|
+
resource?: DefaultResource;
|
|
14
|
+
};
|
|
15
|
+
const DayAgenda = ({ events, resource }: Props) => {
|
|
16
|
+
const {
|
|
17
|
+
day,
|
|
18
|
+
locale,
|
|
19
|
+
selectedDate,
|
|
20
|
+
translations,
|
|
21
|
+
alwaysShowAgendaDays,
|
|
22
|
+
stickyNavigationOffset,
|
|
23
|
+
stickyNavigationHeight,
|
|
24
|
+
} = useStore();
|
|
25
|
+
const { headRenderer } = day!;
|
|
26
|
+
|
|
27
|
+
const dayEvents = useMemo(() => {
|
|
28
|
+
return filterTodayAgendaEvents(events, selectedDate);
|
|
29
|
+
}, [events, selectedDate]);
|
|
30
|
+
|
|
31
|
+
if (!alwaysShowAgendaDays && !dayEvents.length) {
|
|
32
|
+
return <EmptyAgenda />;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<AgendaDiv stickyOffset={stickyNavigationOffset} stickyHeight={stickyNavigationHeight}>
|
|
37
|
+
<div className="rs__agenda_row rs__today_cell">
|
|
38
|
+
<div className="rs__cell rs__agenda__cell">
|
|
39
|
+
{typeof headRenderer === "function" ? (
|
|
40
|
+
<div>{headRenderer({ day: selectedDate, events, resource })}</div>
|
|
41
|
+
) : (
|
|
42
|
+
<Typography variant="body2">{format(selectedDate, "dd E", { locale })}</Typography>
|
|
43
|
+
)}
|
|
44
|
+
</div>
|
|
45
|
+
<div className="rs__cell rs__agenda_items">
|
|
46
|
+
{dayEvents.length > 0 ? (
|
|
47
|
+
<AgendaEventsList day={selectedDate} events={dayEvents} />
|
|
48
|
+
) : (
|
|
49
|
+
<Typography sx={{ padding: 1 }}>{translations.noDataToDisplay}</Typography>
|
|
50
|
+
)}
|
|
51
|
+
</div>
|
|
52
|
+
</div>
|
|
53
|
+
</AgendaDiv>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export { DayAgenda };
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Button,
|
|
3
|
+
Dialog,
|
|
4
|
+
DialogActions,
|
|
5
|
+
DialogContent,
|
|
6
|
+
DialogTitle,
|
|
7
|
+
Grid,
|
|
8
|
+
useMediaQuery,
|
|
9
|
+
useTheme,
|
|
10
|
+
} from "@mui/material";
|
|
11
|
+
import { addMinutes, differenceInMinutes } from "date-fns";
|
|
12
|
+
import { Fragment, useState } from "react";
|
|
13
|
+
import { EditorDatePicker } from "../components/inputs/DatePicker";
|
|
14
|
+
import { EditorInput } from "../components/inputs/Input";
|
|
15
|
+
import { EditorSelect } from "../components/inputs/SelectInput";
|
|
16
|
+
import { arraytizeFieldVal, revertTimeZonedDate } from "../helpers/generals";
|
|
17
|
+
import useStore from "../hooks/useStore";
|
|
18
|
+
import { SelectedRange } from "../store/types";
|
|
19
|
+
import {
|
|
20
|
+
EventActions,
|
|
21
|
+
FieldInputProps,
|
|
22
|
+
FieldProps,
|
|
23
|
+
InputTypes,
|
|
24
|
+
ProcessedEvent,
|
|
25
|
+
SchedulerHelpers,
|
|
26
|
+
} from "../types";
|
|
27
|
+
|
|
28
|
+
export type StateItem = {
|
|
29
|
+
value: any;
|
|
30
|
+
validity: boolean;
|
|
31
|
+
type: InputTypes;
|
|
32
|
+
config?: FieldInputProps;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type StateEvent = (ProcessedEvent & SelectedRange) | Record<string, any>;
|
|
36
|
+
|
|
37
|
+
const initialState = (fields: FieldProps[], event?: StateEvent): Record<string, StateItem> => {
|
|
38
|
+
const customFields = {} as Record<string, StateItem>;
|
|
39
|
+
for (const field of fields) {
|
|
40
|
+
const defVal = arraytizeFieldVal(field, field.default, event);
|
|
41
|
+
const eveVal = arraytizeFieldVal(field, event?.[field.name], event);
|
|
42
|
+
|
|
43
|
+
customFields[field.name] = {
|
|
44
|
+
value: eveVal.value || defVal.value || "",
|
|
45
|
+
validity: field.config?.required ? !!eveVal.validity || !!defVal.validity : true,
|
|
46
|
+
type: field.type,
|
|
47
|
+
config: field.config,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
event_id: {
|
|
53
|
+
value: event?.event_id || null,
|
|
54
|
+
validity: true,
|
|
55
|
+
type: "hidden",
|
|
56
|
+
},
|
|
57
|
+
title: {
|
|
58
|
+
value: event?.title || "",
|
|
59
|
+
validity: !!event?.title,
|
|
60
|
+
type: "input",
|
|
61
|
+
config: { label: "Title", required: true, min: 3 },
|
|
62
|
+
},
|
|
63
|
+
subtitle: {
|
|
64
|
+
value: event?.subtitle || "",
|
|
65
|
+
validity: true,
|
|
66
|
+
type: "input",
|
|
67
|
+
config: { label: "Subtitle", required: false },
|
|
68
|
+
},
|
|
69
|
+
start: {
|
|
70
|
+
value: event?._originalStart || event?.start || new Date(),
|
|
71
|
+
validity: true,
|
|
72
|
+
type: "date",
|
|
73
|
+
config: { label: "Start", sm: 6 },
|
|
74
|
+
},
|
|
75
|
+
end: {
|
|
76
|
+
value: event?._originalEnd || event?.end || new Date(),
|
|
77
|
+
validity: true,
|
|
78
|
+
type: "date",
|
|
79
|
+
config: { label: "End", sm: 6 },
|
|
80
|
+
},
|
|
81
|
+
...customFields,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const Editor = () => {
|
|
86
|
+
const {
|
|
87
|
+
fields,
|
|
88
|
+
dialog,
|
|
89
|
+
triggerDialog,
|
|
90
|
+
selectedRange,
|
|
91
|
+
selectedEvent,
|
|
92
|
+
resourceFields,
|
|
93
|
+
selectedResource,
|
|
94
|
+
triggerLoading,
|
|
95
|
+
onConfirm,
|
|
96
|
+
customEditor,
|
|
97
|
+
confirmEvent,
|
|
98
|
+
dialogMaxWidth,
|
|
99
|
+
translations,
|
|
100
|
+
timeZone,
|
|
101
|
+
} = useStore();
|
|
102
|
+
const [state, setState] = useState(initialState(fields, selectedEvent || selectedRange));
|
|
103
|
+
const [touched, setTouched] = useState(false);
|
|
104
|
+
const theme = useTheme();
|
|
105
|
+
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
|
106
|
+
|
|
107
|
+
const handleEditorState = (name: string, value: any, validity: boolean) => {
|
|
108
|
+
setState((prev) => {
|
|
109
|
+
return {
|
|
110
|
+
...prev,
|
|
111
|
+
[name]: { ...prev[name], value, validity },
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const handleClose = (clearState?: boolean) => {
|
|
117
|
+
if (clearState) {
|
|
118
|
+
setState(initialState(fields));
|
|
119
|
+
}
|
|
120
|
+
triggerDialog(false);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const handleConfirm = async () => {
|
|
124
|
+
let body = {} as ProcessedEvent;
|
|
125
|
+
for (const key in state) {
|
|
126
|
+
body[key] = state[key].value;
|
|
127
|
+
if (!customEditor && !state[key].validity) {
|
|
128
|
+
return setTouched(true);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
triggerLoading(true);
|
|
133
|
+
// Auto fix date
|
|
134
|
+
body.end =
|
|
135
|
+
body.start >= body.end
|
|
136
|
+
? addMinutes(body.start, differenceInMinutes(selectedRange?.end!, selectedRange?.start!))
|
|
137
|
+
: body.end;
|
|
138
|
+
// Specify action
|
|
139
|
+
const action: EventActions = selectedEvent?.event_id ? "edit" : "create";
|
|
140
|
+
// Trigger custom/remote when provided
|
|
141
|
+
if (onConfirm) {
|
|
142
|
+
body = await onConfirm(body, action);
|
|
143
|
+
} else {
|
|
144
|
+
// Create/Edit local data
|
|
145
|
+
body.event_id =
|
|
146
|
+
selectedEvent?.event_id || Date.now().toString(36) + Math.random().toString(36).slice(2);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
body.start = revertTimeZonedDate(body.start, timeZone);
|
|
150
|
+
body.end = revertTimeZonedDate(body.end, timeZone);
|
|
151
|
+
|
|
152
|
+
confirmEvent(body, action);
|
|
153
|
+
handleClose(true);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error(error);
|
|
156
|
+
} finally {
|
|
157
|
+
triggerLoading(false);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const renderInputs = (key: string) => {
|
|
161
|
+
const stateItem = state[key];
|
|
162
|
+
switch (stateItem.type) {
|
|
163
|
+
case "input":
|
|
164
|
+
return (
|
|
165
|
+
<EditorInput
|
|
166
|
+
value={stateItem.value}
|
|
167
|
+
name={key}
|
|
168
|
+
onChange={handleEditorState}
|
|
169
|
+
touched={touched}
|
|
170
|
+
{...stateItem.config}
|
|
171
|
+
label={translations.event[key] || stateItem.config?.label}
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
174
|
+
case "date":
|
|
175
|
+
return (
|
|
176
|
+
<EditorDatePicker
|
|
177
|
+
value={stateItem.value}
|
|
178
|
+
name={key}
|
|
179
|
+
onChange={(...args) => handleEditorState(...args, true)}
|
|
180
|
+
touched={touched}
|
|
181
|
+
{...stateItem.config}
|
|
182
|
+
label={translations.event[key] || stateItem.config?.label}
|
|
183
|
+
/>
|
|
184
|
+
);
|
|
185
|
+
case "select":
|
|
186
|
+
const field = fields.find((f) => f.name === key);
|
|
187
|
+
return (
|
|
188
|
+
<EditorSelect
|
|
189
|
+
value={stateItem.value}
|
|
190
|
+
name={key}
|
|
191
|
+
options={field?.options || []}
|
|
192
|
+
onChange={handleEditorState}
|
|
193
|
+
touched={touched}
|
|
194
|
+
{...stateItem.config}
|
|
195
|
+
label={translations.event[key] || stateItem.config?.label}
|
|
196
|
+
/>
|
|
197
|
+
);
|
|
198
|
+
default:
|
|
199
|
+
return "";
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const renderEditor = () => {
|
|
204
|
+
if (customEditor) {
|
|
205
|
+
const schedulerHelpers: SchedulerHelpers = {
|
|
206
|
+
state,
|
|
207
|
+
close: () => triggerDialog(false),
|
|
208
|
+
loading: (load) => triggerLoading(load),
|
|
209
|
+
edited: selectedEvent,
|
|
210
|
+
onConfirm: confirmEvent,
|
|
211
|
+
[resourceFields.idField]: selectedResource,
|
|
212
|
+
};
|
|
213
|
+
return customEditor(schedulerHelpers);
|
|
214
|
+
}
|
|
215
|
+
return (
|
|
216
|
+
<Fragment>
|
|
217
|
+
<DialogTitle>
|
|
218
|
+
{selectedEvent ? translations.form.editTitle : translations.form.addTitle}
|
|
219
|
+
</DialogTitle>
|
|
220
|
+
<DialogContent style={{ overflowX: "hidden" }}>
|
|
221
|
+
<Grid container spacing={2}>
|
|
222
|
+
{Object.keys(state).map((key) => {
|
|
223
|
+
const item = state[key];
|
|
224
|
+
return (
|
|
225
|
+
<Grid key={key} size={{ sm: item.config?.sm, xs: 12 }}>
|
|
226
|
+
{renderInputs(key)}
|
|
227
|
+
</Grid>
|
|
228
|
+
);
|
|
229
|
+
})}
|
|
230
|
+
</Grid>
|
|
231
|
+
</DialogContent>
|
|
232
|
+
<DialogActions>
|
|
233
|
+
<Button color="inherit" fullWidth onClick={() => handleClose()}>
|
|
234
|
+
{translations.form.cancel}
|
|
235
|
+
</Button>
|
|
236
|
+
<Button color="primary" fullWidth onClick={handleConfirm}>
|
|
237
|
+
{translations.form.confirm}
|
|
238
|
+
</Button>
|
|
239
|
+
</DialogActions>
|
|
240
|
+
</Fragment>
|
|
241
|
+
);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<Dialog
|
|
246
|
+
open={dialog}
|
|
247
|
+
fullScreen={isMobile}
|
|
248
|
+
maxWidth={dialogMaxWidth}
|
|
249
|
+
onClose={() => {
|
|
250
|
+
triggerDialog(false);
|
|
251
|
+
}}
|
|
252
|
+
>
|
|
253
|
+
{renderEditor()}
|
|
254
|
+
</Dialog>
|
|
255
|
+
);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export default Editor;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useEffect, useCallback } from "react";
|
|
2
|
+
import { addDays, eachWeekOfInterval, endOfMonth, startOfMonth } from "date-fns";
|
|
3
|
+
import { DefaultResource } from "../types";
|
|
4
|
+
import { getResourcedEvents, sortEventsByTheEarliest } from "../helpers/generals";
|
|
5
|
+
import { WithResources } from "../components/common/WithResources";
|
|
6
|
+
import useStore from "../hooks/useStore";
|
|
7
|
+
import { MonthAgenda } from "./MonthAgenda";
|
|
8
|
+
import MonthTable from "../components/month/MonthTable";
|
|
9
|
+
|
|
10
|
+
const Month = () => {
|
|
11
|
+
const {
|
|
12
|
+
month,
|
|
13
|
+
selectedDate,
|
|
14
|
+
events,
|
|
15
|
+
getRemoteEvents,
|
|
16
|
+
triggerLoading,
|
|
17
|
+
handleState,
|
|
18
|
+
resources,
|
|
19
|
+
resourceFields,
|
|
20
|
+
fields,
|
|
21
|
+
agenda,
|
|
22
|
+
} = useStore();
|
|
23
|
+
|
|
24
|
+
const { weekStartOn, weekDays } = month!;
|
|
25
|
+
const monthStart = startOfMonth(selectedDate);
|
|
26
|
+
const monthEnd = endOfMonth(selectedDate);
|
|
27
|
+
const eachWeekStart = eachWeekOfInterval(
|
|
28
|
+
{
|
|
29
|
+
start: monthStart,
|
|
30
|
+
end: monthEnd,
|
|
31
|
+
},
|
|
32
|
+
{ weekStartsOn: weekStartOn }
|
|
33
|
+
);
|
|
34
|
+
const daysList = weekDays.map((d) => addDays(eachWeekStart[0], d));
|
|
35
|
+
|
|
36
|
+
const fetchEvents = useCallback(async () => {
|
|
37
|
+
try {
|
|
38
|
+
triggerLoading(true);
|
|
39
|
+
const start = eachWeekStart[0];
|
|
40
|
+
const end = addDays(eachWeekStart[eachWeekStart.length - 1], daysList.length);
|
|
41
|
+
const events = await getRemoteEvents!({
|
|
42
|
+
start,
|
|
43
|
+
end,
|
|
44
|
+
view: "month",
|
|
45
|
+
});
|
|
46
|
+
if (events && events?.length) {
|
|
47
|
+
handleState(events, "events");
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw error;
|
|
51
|
+
} finally {
|
|
52
|
+
triggerLoading(false);
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
+
}, [daysList.length, getRemoteEvents]);
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (getRemoteEvents instanceof Function) {
|
|
59
|
+
fetchEvents();
|
|
60
|
+
}
|
|
61
|
+
}, [fetchEvents, getRemoteEvents]);
|
|
62
|
+
|
|
63
|
+
const renderTable = useCallback(
|
|
64
|
+
(resource?: DefaultResource) => {
|
|
65
|
+
if (agenda) {
|
|
66
|
+
let resourcedEvents = sortEventsByTheEarliest(events);
|
|
67
|
+
if (resource) {
|
|
68
|
+
resourcedEvents = getResourcedEvents(events, resource, resourceFields, fields);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return <MonthAgenda resource={resource} events={resourcedEvents} />;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return <MonthTable daysList={daysList} eachWeekStart={eachWeekStart} resource={resource} />;
|
|
75
|
+
},
|
|
76
|
+
[agenda, daysList, eachWeekStart, events, fields, resourceFields]
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
return resources.length ? <WithResources renderChildren={renderTable} /> : renderTable();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export { Month };
|