Backend API
import React, { useCallback, useRef, useState } from 'react';
import { Scheduler, Resource } from 'devextreme-react/scheduler';
import type { SchedulerRef, SchedulerTypes } from 'devextreme-react/scheduler';
import { ContextMenu } from 'devextreme-react/context-menu';
import type { ContextMenuTypes } from 'devextreme-react/context-menu';
import ItemTemplate from './itemTemplate.tsx';
import { data, resourcesData } from './data.ts';
import type { ContextMenuItem } from './types';
const views: SchedulerTypes.ViewType[] = ['day', 'month'];
const onContextMenuItemClick = (e: ContextMenuTypes.ItemClickEvent<ContextMenuItem>) => {
e.itemData?.onItemClick?.(e);
};
const App = () => {
const schedulerRef = useRef<SchedulerRef>(null);
const [currentDate, setCurrentDate] = useState(new Date(2020, 10, 25));
const [contextMenuItems, setContextMenuItems] = useState<ContextMenuItem[]>([]);
const [groups, setGroups] = useState<string[]>([]);
const [crossScrollingEnabled, setCrossScrollingEnabled] = useState(false);
const getAppointmentContextMenuItems = useCallback((e: SchedulerTypes.AppointmentContextMenuEvent) => {
const { appointmentData: appointment, targetedAppointmentData: targetedAppointment } = e;
const scheduler = e.component;
return [
{
text: 'Open',
onItemClick: () => scheduler?.showAppointmentPopup(appointment),
},
{
text: 'Delete',
onItemClick: () => scheduler?.deleteAppointment(appointment),
},
{
text: 'Repeat Weekly',
beginGroup: true,
onItemClick: () => scheduler?.updateAppointment(appointment, {
...appointment,
startDate: targetedAppointment?.startDate,
recurrenceRule: 'FREQ=WEEKLY',
}),
},
{
text: 'Set Room',
beginGroup: true,
disabled: true,
},
...resourcesData.map((item) => ({
...item,
onItemClick: (clickEvent: ContextMenuTypes.ItemClickEvent<ContextMenuItem>) => {
scheduler?.updateAppointment(appointment, {
...appointment,
roomId: [clickEvent.itemData?.id],
});
},
})),
];
}, []);
const getCellContextMenuItems = useCallback((e: SchedulerTypes.CellContextMenuEvent) => {
const scheduler = e.component;
return [
{
text: 'New Appointment',
onItemClick: () => {
scheduler?.showAppointmentPopup(
{ startDate: e.cellData.startDateUTC },
true,
);
},
},
{
text: 'New Recurring Appointment',
onItemClick: () => {
scheduler?.showAppointmentPopup(
{
startDate: e.cellData.startDateUTC,
recurrenceRule: 'FREQ=DAILY',
},
true,
);
},
},
{
text: 'Group by Room/Ungroup',
beginGroup: true,
onItemClick: () => {
if (groups.length) {
setCrossScrollingEnabled(false);
setGroups([]);
} else {
setCrossScrollingEnabled(true);
setGroups(['roomId']);
}
},
},
{
text: 'Go to Today',
onItemClick: () => {
setCurrentDate(new Date());
},
},
];
}, [groups]);
const onAppointmentContextMenu = useCallback((e: SchedulerTypes.AppointmentContextMenuEvent) => {
const items = getAppointmentContextMenuItems(e);
setContextMenuItems(items);
}, [getAppointmentContextMenuItems]);
const onCellContextMenu = useCallback((e: SchedulerTypes.CellContextMenuEvent) => {
const items = getCellContextMenuItems(e);
setContextMenuItems(items);
}, [getCellContextMenuItems]);
const onContextMenuHiding = useCallback(() => {
setContextMenuItems([]);
}, []);
return (
<>
<Scheduler
ref={schedulerRef}
timeZone="America/Los_Angeles"
dataSource={data}
views={views}
groups={groups}
crossScrollingEnabled={crossScrollingEnabled}
defaultCurrentView="month"
currentDate={currentDate}
startDayHour={9}
recurrenceEditMode="series"
onAppointmentContextMenu={onAppointmentContextMenu}
onCellContextMenu={onCellContextMenu}
height={730}
>
<Resource
dataSource={resourcesData}
fieldExpr="roomId"
label="Room"
icon="conferenceroomoutline"
/>
</Scheduler>
<ContextMenu
dataSource={contextMenuItems}
width={200}
target='.dx-scheduler'
onItemClick={onContextMenuItemClick}
onHiding={onContextMenuHiding}
itemComponent={ItemTemplate}
/>
</>
);
};
export default App;
import React, { useCallback, useRef, useState } from 'react';
import { Scheduler, Resource } from 'devextreme-react/scheduler';
import { ContextMenu } from 'devextreme-react/context-menu';
import ItemTemplate from './itemTemplate.js';
import { data, resourcesData } from './data.js';
const views = ['day', 'month'];
const onContextMenuItemClick = (e) => {
e.itemData?.onItemClick?.(e);
};
const App = () => {
const schedulerRef = useRef(null);
const [currentDate, setCurrentDate] = useState(new Date(2020, 10, 25));
const [contextMenuItems, setContextMenuItems] = useState([]);
const [groups, setGroups] = useState([]);
const [crossScrollingEnabled, setCrossScrollingEnabled] = useState(false);
const getAppointmentContextMenuItems = useCallback((e) => {
const { appointmentData: appointment, targetedAppointmentData: targetedAppointment } = e;
const scheduler = e.component;
return [
{
text: 'Open',
onItemClick: () => scheduler?.showAppointmentPopup(appointment),
},
{
text: 'Delete',
onItemClick: () => scheduler?.deleteAppointment(appointment),
},
{
text: 'Repeat Weekly',
beginGroup: true,
onItemClick: () =>
scheduler?.updateAppointment(appointment, {
...appointment,
startDate: targetedAppointment?.startDate,
recurrenceRule: 'FREQ=WEEKLY',
}),
},
{
text: 'Set Room',
beginGroup: true,
disabled: true,
},
...resourcesData.map((item) => ({
...item,
onItemClick: (clickEvent) => {
scheduler?.updateAppointment(appointment, {
...appointment,
roomId: [clickEvent.itemData?.id],
});
},
})),
];
}, []);
const getCellContextMenuItems = useCallback(
(e) => {
const scheduler = e.component;
return [
{
text: 'New Appointment',
onItemClick: () => {
scheduler?.showAppointmentPopup({ startDate: e.cellData.startDateUTC }, true);
},
},
{
text: 'New Recurring Appointment',
onItemClick: () => {
scheduler?.showAppointmentPopup(
{
startDate: e.cellData.startDateUTC,
recurrenceRule: 'FREQ=DAILY',
},
true,
);
},
},
{
text: 'Group by Room/Ungroup',
beginGroup: true,
onItemClick: () => {
if (groups.length) {
setCrossScrollingEnabled(false);
setGroups([]);
} else {
setCrossScrollingEnabled(true);
setGroups(['roomId']);
}
},
},
{
text: 'Go to Today',
onItemClick: () => {
setCurrentDate(new Date());
},
},
];
},
[groups],
);
const onAppointmentContextMenu = useCallback(
(e) => {
const items = getAppointmentContextMenuItems(e);
setContextMenuItems(items);
},
[getAppointmentContextMenuItems],
);
const onCellContextMenu = useCallback(
(e) => {
const items = getCellContextMenuItems(e);
setContextMenuItems(items);
},
[getCellContextMenuItems],
);
const onContextMenuHiding = useCallback(() => {
setContextMenuItems([]);
}, []);
return (
<>
<Scheduler
ref={schedulerRef}
timeZone="America/Los_Angeles"
dataSource={data}
views={views}
groups={groups}
crossScrollingEnabled={crossScrollingEnabled}
defaultCurrentView="month"
currentDate={currentDate}
startDayHour={9}
recurrenceEditMode="series"
onAppointmentContextMenu={onAppointmentContextMenu}
onCellContextMenu={onCellContextMenu}
height={730}
>
<Resource
dataSource={resourcesData}
fieldExpr="roomId"
label="Room"
icon="conferenceroomoutline"
/>
</Scheduler>
<ContextMenu
dataSource={contextMenuItems}
width={200}
target=".dx-scheduler"
onItemClick={onContextMenuItemClick}
onHiding={onContextMenuHiding}
itemComponent={ItemTemplate}
/>
</>
);
};
export default App;
import React from 'react';
type ItemTemplateProps = {
data: {
color: string;
text: string;
}
};
const ItemTemplate = (props: ItemTemplateProps) => (
<div>
{props.data.color && <div className="item-badge" style={{ backgroundColor: props.data.color }} />}
{props.data.text}
</div>
);
export default ItemTemplate;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import type { Appointment, Resource } from './types';
export const data: Appointment[] = [
{
text: 'Watercolor Landscape',
roomId: [1],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T19:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=MO,TH;COUNT=10',
}, {
text: 'Oil Painting for Beginners',
roomId: [2],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T19:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=SU,WE;COUNT=10',
}, {
text: 'Testing',
roomId: [3],
startDate: new Date('2020-11-01T20:00:00.000Z'),
endDate: new Date('2020-11-01T21:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=SU;WKST=TU;INTERVAL=2;COUNT=2',
}, {
text: 'Meeting of Instructors',
roomId: [4],
startDate: new Date('2020-11-01T17:00:00.000Z'),
endDate: new Date('2020-11-01T17:15:00.000Z'),
recurrenceRule: 'FREQ=DAILY;BYDAY=TU;UNTIL=20201203',
}, {
text: 'Recruiting students',
roomId: [5],
startDate: new Date('2020-10-24T18:00:00.000Z'),
endDate: new Date('2020-10-24T19:00:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYWEEKNO=50;WKST=SU',
recurrenceException: '20201212T190000Z',
}, {
text: 'Final exams',
roomId: [3],
startDate: new Date('2020-10-24T20:00:00.000Z'),
endDate: new Date('2020-10-24T21:35:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYWEEKNO=51;BYDAY=WE,TH',
}, {
text: 'Monthly Planning',
roomId: [4],
startDate: new Date('2020-11-24T22:30:00.000Z'),
endDate: new Date('2020-11-24T23:45:00.000Z'),
recurrenceRule: 'FREQ=MONTHLY;BYMONTHDAY=28;COUNT=1',
}, {
text: 'Open Day',
roomId: [5],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T21:00:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYYEARDAY=333',
},
];
export const resourcesData: Resource[] = [
{
text: 'Room 101',
id: 1,
color: '#1564C0',
}, {
text: 'Room 102',
id: 2,
color: '#C2185B',
}, {
text: 'Room 103',
id: 3,
color: '#7B1FA2',
}, {
text: 'Meeting room',
id: 4,
color: '#DD2C00',
}, {
text: 'Conference hall',
id: 5,
color: '#2E7D32',
},
];
import type { SchedulerTypes } from 'devextreme-react/scheduler';
import type { ContextMenuTypes } from 'devextreme-react/context-menu';
export type Appointment = SchedulerTypes.Appointment & { roomId: number[] };
export type Resource = {
id?: number;
text: string;
color?: string;
};
export type ContextMenuItem = ContextMenuTypes.Item & Partial<Resource> & { onItemClick?: (e: ContextMenuTypes.ItemClickEvent<ContextMenuItem>) => void };
import React from 'react';
const ItemTemplate = (props) => (
<div>
{props.data.color && (
<div
className="item-badge"
style={{ backgroundColor: props.data.color }}
/>
)}
{props.data.text}
</div>
);
export default ItemTemplate;
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
export const data = [
{
text: 'Watercolor Landscape',
roomId: [1],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T19:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=MO,TH;COUNT=10',
},
{
text: 'Oil Painting for Beginners',
roomId: [2],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T19:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=SU,WE;COUNT=10',
},
{
text: 'Testing',
roomId: [3],
startDate: new Date('2020-11-01T20:00:00.000Z'),
endDate: new Date('2020-11-01T21:00:00.000Z'),
recurrenceRule: 'FREQ=WEEKLY;BYDAY=SU;WKST=TU;INTERVAL=2;COUNT=2',
},
{
text: 'Meeting of Instructors',
roomId: [4],
startDate: new Date('2020-11-01T17:00:00.000Z'),
endDate: new Date('2020-11-01T17:15:00.000Z'),
recurrenceRule: 'FREQ=DAILY;BYDAY=TU;UNTIL=20201203',
},
{
text: 'Recruiting students',
roomId: [5],
startDate: new Date('2020-10-24T18:00:00.000Z'),
endDate: new Date('2020-10-24T19:00:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYWEEKNO=50;WKST=SU',
recurrenceException: '20201212T190000Z',
},
{
text: 'Final exams',
roomId: [3],
startDate: new Date('2020-10-24T20:00:00.000Z'),
endDate: new Date('2020-10-24T21:35:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYWEEKNO=51;BYDAY=WE,TH',
},
{
text: 'Monthly Planning',
roomId: [4],
startDate: new Date('2020-11-24T22:30:00.000Z'),
endDate: new Date('2020-11-24T23:45:00.000Z'),
recurrenceRule: 'FREQ=MONTHLY;BYMONTHDAY=28;COUNT=1',
},
{
text: 'Open Day',
roomId: [5],
startDate: new Date('2020-11-01T17:30:00.000Z'),
endDate: new Date('2020-11-01T21:00:00.000Z'),
recurrenceRule: 'FREQ=YEARLY;BYYEARDAY=333',
},
];
export const resourcesData = [
{
text: 'Room 101',
id: 1,
color: '#1564C0',
},
{
text: 'Room 102',
id: 2,
color: '#C2185B',
},
{
text: 'Room 103',
id: 3,
color: '#7B1FA2',
},
{
text: 'Meeting room',
id: 4,
color: '#DD2C00',
},
{
text: 'Conference hall',
id: 5,
color: '#2E7D32',
},
];
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
.item-badge {
text-align: center;
float: left;
margin-right: 12px;
color: white;
width: 18px;
height: 18px;
font-size: 19.5px;
border-radius: 18px;
margin-top: 2px;
}