1
+ import { LightningElement , api } from 'lwc' ;
2
+ import { ShowToastEvent } from "lightning/platformShowToastEvent" ;
3
+ import { loadStyle , loadScript } from "lightning/platformResourceLoader" ;
4
+ import { createRecord , updateRecord , deleteRecord } from "lightning/uiRecordApi" ;
5
+ // Static resources
6
+ import schedulerFiles from "@salesforce/resourceUrl/syncfusionscheduler" ;
7
+
8
+ // Controllers
9
+ import getEvents from "@salesforce/apex/SchedulerData.getEvents" ;
10
+ function getEventsData ( eventData ) {
11
+ const data = eventData . events . map ( ( a ) => ( {
12
+ Id : a . Id ,
13
+ Subject : a . Name ,
14
+ Location : a . Location__c ,
15
+ StartTime : a . Start_Time__c ,
16
+ EndTime : a . End_Time__c ,
17
+ IsAllDay : a . IsAllDay__c ,
18
+ RecurrenceRule : a . RecurrenceRule__c ,
19
+ RecurrenceID : a . Recurrence_Id__c ,
20
+ RecurrenceException : a . RecurrenceException__c
21
+ } ) ) ;
22
+ return data ;
23
+ }
24
+
25
+ export default class Scheduler extends LightningElement {
26
+ static delegatesFocus = true ;
27
+ @api height ;
28
+ schedulerInitialized = false ;
29
+ renderedCallback ( ) {
30
+ if ( this . schedulerInitialized ) {
31
+ return ;
32
+ }
33
+ this . schedulerInitialized = true ;
34
+ Promise . all ( [
35
+ loadScript ( this , schedulerFiles + "/syncscheduler.js" ) ,
36
+ loadStyle ( this , schedulerFiles + "/syncscheduler.css" )
37
+ ] )
38
+ . then ( ( ) => {
39
+ this . initializeUI ( ) ;
40
+ } )
41
+ . catch ( ( error ) => {
42
+ this . dispatchEvent (
43
+ new ShowToastEvent ( {
44
+ title : "Error loading scheduler" ,
45
+ message : error . message ,
46
+ variant : "error"
47
+ } )
48
+ ) ;
49
+ } ) ;
50
+ }
51
+ initializeUI ( ) {
52
+ const root = this . template . querySelector ( ".syncfusionscheduler" ) ;
53
+ root . style . height = this . height + "px" ;
54
+ const scheduleOptions = {
55
+ height : this . height + "px" ,
56
+ selectedDate : new Date ( ) ,
57
+ actionComplete : function ( args ) {
58
+ //To perform CRUD in salesforce backend
59
+ if ( args . addedRecords && args . addedRecords . length > 0 ) {
60
+ var data = args . addedRecords [ 0 ] ;
61
+ var insert = {
62
+ apiName : "SchedulerEvent__c" ,
63
+ fields : {
64
+ Name : data . Subject ,
65
+ Location__c : data . Location ,
66
+ Start_Time__c : data . StartTime ,
67
+ End_Time__c : data . EndTime ,
68
+ IsAllDay__c : data . IsAllDay ,
69
+ RecurrenceRule__c : data . RecurrenceRule ,
70
+ Recurrence_Id__c : data . RecurrenceID ,
71
+ RecurrenceException__c : data . RecurrenceException
72
+ }
73
+ } ;
74
+ createRecord ( insert ) . then ( ( res ) => {
75
+ if ( scheduleObj )
76
+ {
77
+ scheduleObj . eventSettings . dataSource [ scheduleObj . eventSettings . dataSource . length - 1 ] . Id = res . id ;
78
+ scheduleObj . refreshEvents ( ) ;
79
+ }
80
+ return { tid : res . id , ...res } ;
81
+ } ) ;
82
+ }
83
+ if ( args . changedRecords && args . changedRecords . length > 0 ) {
84
+ var data = args . changedRecords [ 0 ] ;
85
+ var update = {
86
+ fields : {
87
+ Id : data . Id ,
88
+ Name : data . Subject ,
89
+ Location__c : data . Location ,
90
+ Start_Time__c : data . StartTime ,
91
+ End_Time__c : data . EndTime ,
92
+ IsAllDay__c : data . IsAllDay ,
93
+ RecurrenceRule__c : data . RecurrenceRule ,
94
+ RecurrenceException__c : data . RecurrenceException ,
95
+ Recurrence_Id__c : data . RecurrenceID
96
+ }
97
+ } ;
98
+ updateRecord ( update ) . then ( ( ) => ( { } ) ) ;
99
+ }
100
+ if ( args . deletedRecords && args . deletedRecords . length > 0 ) {
101
+ args . deletedRecords . forEach ( event => {
102
+ deleteRecord ( event . Id ) . then ( ( ) => ( { } ) ) ;
103
+ } ) ;
104
+ }
105
+ }
106
+ } ;
107
+ const scheduleObj = new ej . schedule . Schedule ( scheduleOptions , root ) ;
108
+ getEvents ( ) . then ( ( data ) => {
109
+ const eventData = getEventsData ( data ) ;
110
+ scheduleObj . eventSettings . dataSource = eventData ;
111
+ scheduleObj . dataBind ( ) ;
112
+ } ) ;
113
+ }
114
+ }
0 commit comments