please consider this
import React, { useState, useEffect, useRef } from 'react';
import { useSelector } from 'react-redux';
import { DayPilotCalendar} from "@daypilot/daypilot-lite-react";
export const Rooms = () => {
// ref
const calendarRef = useRef(null);
// state
const [open, setModal] = useState(false);
const [pivotState, setPivotState] = useState({
viewType: "Resources",
durationBarVisible: true,
timeRangeSelectedHandling: "Enabled",
events: roomEvents,
columns: [
{ name: "Room 1", id: "Room 1" },
{ name: "Room 2", id: "Room 2" },
{ name: "Room 3", id: "Room 3" },
{ name: "Room 4", id: "Room 4" },
{ name: "Room 5", id: "Room 5" },
{ name: "Room 6", id: "Room 6" },
{ name: "Room 7", id: "Room 7" },
],
startdate: roomsDate
});
// local state
let events = [{
end: "2022-11-23T11:30:00",
id: 0,
resource: "Room 1",
start: "2022-11-23T10:30:00",
text: "test",
},
{
id: 1,
text: "Event 1",
start: "2022-11-23T10:30:00",
end: "2022-11-23T13:00:00",
resource: "Room 2"
},
]
// data from store
const roomEvents = useSelector((store) => store?.app?.events);
// this if for timeRange Selected opening modal and creating event for that time slot
const onTimeRangeSelected = () => async args => {
const dp = obj.calendar;
const form = [
{ name: "Text", id: "text" },
{ name: "Start", id: "start", type: "datetime" },
{ name: "End", id: "end", type: "datetime" },
];
console.log(args.resource);
setModal(true);
}
// updating calendar on getting data from api call
useEffect(() => {
const columns = [
{ name: "Room 1", id: "Room 1" },
{ name: "Room 2", id: "Room 2" },
{ name: "Room 3", id: "Room 3" },
{ name: "Room 4", id: "Room 4" },
{ name: "Room 5", id: "Room 5" },
{ name: "Room 6", id: "Room 6" },
{ name: "Room 7", id: "Room 7" },
];
const startDate = "2023-03-07";
console.log('roomEvents', roomEvents);
console.log('calendat', calendarRef);
// setPivotState({ ...pivotState, events: events });
obj.calendar.update({ startDate, columns, roomEvents});
}, [roomEvents]);
// calendar Ref
const obj = {
get calendar() {
return calendarRef.current.control;
}
}
retrun(
<div style={styles.main} id="dp">
<DayPilotCalendar
{...pivotState}
ref={calendarRef}
onTimeRangeSelected={onTimeRangeSelected()}
/>
</div>
)
}