Hi Team
We want to have the schedular with resources. In the didMount the following line is giving the error-:
this.schedulerRef.update({
resources,
events
});
Please find the class. We tried using this.schedulerRef.current.control.update(); also but issue is not resolved
class Scheduler extends Component {
undoService = new UndoService();
constructor(props) {
super(props);
this.schedulerRef = React.createRef();
this.state = {
events:[],
rowMoveHandling: "Update",
onRowMove: args => {
console.log("onRowMove", args);
},
rowMoveSameLevelOnly: true,
config: {
timeHeaders: [{"groupBy":"Month"},{"groupBy":"Day","format":"d"}],
rowHeaderColumns : [
{ text: 'Resource', display: "name", sort: "name" }
],
scale: "Day",
days: 30,
startDate: "2021-09-01",
rowMarginBottom: 1,
onBeforeEventRender: args => {
if (args.data.backColor) {
args.data.borderColor = "darker";
args.data.fontColor = "white";
}
args.data.barHidden = true;
},
eventDeleteHandling: "Update",
onTimeRangeSelected: async (args) => {
const modal = await DayPilot.Modal.prompt("Create a new event:", "Event 1");
this.schedulerRef.clearSelection();
if (modal.canceled) { return; }
const e = {
start: args.start,
end: args.end,
id: DayPilot.guid(),
resource: args.resource,
text: modal.result
};
this.schedulerRef.events.add(e);
this.undoService.add(e, "Event added");
},
onEventMoved: (args) => {
this.undoService.update(args.e.data, "Event moved");
},
onEventResized: (args) => {
this.undoService.update(args.e.data, "Event resized");
},
onEventDeleted: (args) => {
this.undoService.remove(args.e.data, "Event removed");
},
onEventClick: async args => {
const dp = this.schedulerRef;
const modal =await DayPilot.Modal.prompt("Update event text:", args.e.text());
this.schedulerRef.clearSelection();
if (!modal.result) { return; }
const e = args.e;
e.data.text = modal.result;
dp.events.update(e);
console.log("the events are");
console.log(this.schedulerRef.events)
},
treeEnabled: true,
rowCreateHandling: "Enabled",
rowCreateText: "New group...",
onRowCreate: args => {
const row = {
name: args.text,
id: DayPilot.guid()
}
this.schedulerRef.rows.add(row);
},
rowMoveSameLevelOnly: true,
rowMoveHandling: "Update",
onRowMove: args => {
console.log("onRowMove", args);
},
onBeforeRowHeaderRender: args => {
args.row.areas = [
{
right: 6,
top: 6,
width: 18,
height: 18,
action: "ContextMenu",
backColor: "#ffffff",
symbol: "icons/daypilot.svg#minichevron-down-4",
fontColor: "#cccccc",
style: "border: 1px solid #ccc; cursor: pointer;",
visibility: "Hover"
}
]
},
contextMenuSelection: new DayPilot.Menu({
items: [
{
text: "Paste",
onClick: args => {
console.log("the args are ");
console.log(args);
console.log("the clipboard is");
console.log(this.clipboard)
let e = this.clipboard[0];
console.log("the event is");
console.log(e);
if (!e) {
return;
}
let start = e.data.start;
let end =e.data.end;
let targetResource = args.source.resource;
let params = {
start: start,
end: end,
resource: targetResource,
text: e.text(),
backColor: "#e69138",
id: DayPilot.guid(),
};
console.log("the params are");
console.log(params);
this.schedulerRef.events.add(params);
//this.createEvent(params).subscribe(result => this.schedulerRef.control.events.add(result));
}
}
]
}),
contextMenu: new DayPilot.Menu({
items: [
{ text: "Edit", onClick: function (args) { } },
{ text: "Delete", onClick: function (args) { } },
{
text: "Cut",
onClick: args => {
this.clipboard = [ args.source ];
}
},
{ text: "-"},
{ text: "Change background color", onClick: function (args) {
var e = args.source;
e.data.backColor = "red";
}
},
{text: "-"},
{ text: "Select", onClick: function (args) { } },
{
text: "Copy",
onClick: args => {
this.clipboard = [ args.source ];
}
},
{
text: "Replace",
onClick: args => {
console.log("the args are ");
console.log(args);
console.log("the clipboard is");
console.log(this.clipboard)
let e = this.clipboard[0];
console.log("the event is");
console.log(e);
if (!e) {
return;
}
let start = e.data.start;
let end =e.data.end;
let targetResource = args.source.resource;
let params = {
start: start,
end: end,
resource: targetResource,
text: e.text(),
backColor: "#e69138",
id: DayPilot.guid(),
};
console.log("the params are");
console.log(params);
this.schedulerRef.events.add(params);
//this.createEvent(params).subscribe(result => this.schedulerRef.control.events.add(result));
}
}
]
}),
contextMenuResource: new DayPilot.Menu({
onShow: args => {
const row = args.source;
const isParent = row.level === 0;
args.menu.items[3].disabled = !isParent;
},
items: [
{text: "Edit...", onClick: args => this.edit(args) },
{text: "Delete...", onClick: args => this.delete(args) },
{text: "-"},
{text: "Add resource....", onClick: args => this.add(args)}
]
})
}
};
}
doUndo() {
if (typeof this.props.onUndo === "function") {
this.props.onUndo();
}
}
componentDidMount() {
const events = [
{
id: 1,
text: "Event 1",
start: "2021-09-02T00:00:00",
end: "2021-09-05T00:00:00",
resource: "A",
backColor: "#e69138"
},
{
id: 2,
text: "Event 2",
start: "2021-09-03T00:00:00",
end: "2021-09-10T00:00:00",
resource: "C",
backColor: "#6aa84f"
},
{
id: 3,
text: "Event 3",
start: "2021-09-02T00:00:00",
end: "2021-09-08T00:00:00",
resource: "D",
backColor: "#3d85c6"
},
{
id: 4,
text: "Event 3",
start: "2021-09-04T00:30:00",
end: "2021-09-10T00:00:00",
resource: "F",
backColor: "#cc4125"
},
{
id: 5,
text: "Event 4",
start: "2021-09-02T00:00:00",
end: "2021-09-05T00:00:00",
resource: "A",
backColor: "#e69138"
},
{
id: 6,
text: "Event 5",
start: "2021-09-03T00:00:00",
end: "2021-09-10T00:00:00",
resource: "C",
backColor: "#6aa84f"
},
{
id: 7,
text: "Event 6",
start: "2021-09-02T00:00:00",
end: "2021-09-08T00:00:00",
resource: "D",
backColor: "#3d85c6"
},
{
id: 8,
text: "Event 7",
start: "2021-09-04T00:00:00",
end: "2021-09-10T00:00:00",
resource: "F",
backColor: "#cc4125"
}
];
const resources = [
{name: "Resource A", id: "A"},
{name: "Resource B", id: "B"},
{name: "Resource C", id: "C"},
{name: "Resource D", id: "D"},
{name: "Resource E", id: "E"},
{name: "Resource F", id: "F"},
{name: "Resource G", id: "G"}
];
this.setState({events:events})
// load resource and event data
this.schedulerRef.update({
resources,
events
});
this.undoService.initialize(events);
}
undo() {
const action = this.undoService.undo();
switch (action.type) {
case "add":
// added, need to delete now
this.schedulerRef.events.remove(action.id);
break;
case "remove":
// removed, need to add now
this.schedulerRef.events.add(action.previous);
break;
case "update":
// updated
this.schedulerRef.events.update(action.previous);
break;
default:
throw new Error("Unexpected action");
}
}
redo() {
const action = this.undoService.redo();
switch (action.type) {
case "add":
// added, need to re-add
this.schedulerRef.events.add(action.current);
break;
case "remove":
// removed, need to remove again
this.schedulerRef.events.remove(action.id);
break;
case "update":
// updated, use the new version
this.schedulerRef.events.update(action.current);
break;
default:
throw new Error("Unexpected action");
}
}
render() {
return <div>
<Buttons service={this.undoService} onUndo={() => this.undo()} onRedo={() => this.redo()} />
<DayPilotScheduler
{...this.state.config}
ref={component => {
this.schedulerRef = component && component.control;
}}
onChange={(s) => console.log(s)}
/>
<h2>History</h2>
<History service={this.undoService} />
</div>;
}
}
export default Scheduler;