We will create a Rails 6 calendar Application using StimulusJs and Tui Calendar. To begin, we first create a new Rails application.
railsnewcalendar--database=postgresql
Once we have created our application, we then proceed to install StimulusJs.
railswebpacker:install:stimulus
We then proceed to install Tui Calendar using yarn.
yarnaddtui-calendar
Modeling Data
Once we have all our dependencies installed we can proceed to scaffolding the data we wish to store in our application. We would create a model called Event with the following attributes and types:
Once the initial setup is done, we can now proceed to display our calendar. Navigate to
app/views/schedules/index.html.erb and clear the scaffolded index page. We create a div tag and give it an id of calendar. we also provide some json data to the div tag. We wrap this div tag in a stimulus controller called calendar.
We then proceed to create our stimulus controller called ‘calendar_controller.js’ under the path app/javascript/controllers/calendar_controller.js and export the class
Note: We simply follow the steps on the github document page to import Calendar and its stylesheets. Once we have done that, we create a new instance of Calendar, and define its attributes such as id, name, defaultView, color. This provided in the code below:
calendar=newCalendar(document.getElementById('calendar'),{id:"1",name:"My Calendar",defaultView:'month',color:'#00a9ff',bgColor:'#00a9ff',dragBgColor:'#00a9ff',borderColor:'red',milestone:true,// Can be also ['milestone', 'task']scheduleView:true,// Can be also ['allday', 'time']useCreationPopup:true,useDetailPopup:true,template:{popupDetailRepeat:function(schedule){return'Repeat : '+schedule.recurrenceRule;},popupStateFree:function(){return'Free';},milestone:function(schedule){return'<span style="color:red;"><i class="fa fa-flag"></i> '+schedule.title+'</span>';},milestoneTitle:function(){return'Milestone';},task:function(schedule){return' #'+schedule.title;},taskTitle:function(){return'<label><input type="checkbox" />Task</label>';},allday:function(schedule){returnschedule.title+' <i class="fa fa-refresh"></i>';},alldayTitle:function(){return'All Day';},time:function(schedule){returnschedule.title+' <i class="fa fa-refresh"></i>'+schedule.start;}},month:{daynames:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],startDayOfWeek:0,narrowWeekend:true},week:{daynames:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],startDayOfWeek:0,narrowWeekend:true}});
After saving our changes, our calendar becomes viewable.
Displaying Data on the Calendar
Getting Calendar Data
We must define a method that helps us obtain the calendar data. We have already passed the data as json to our index page, so we must parse the data to make it available in our calendar controller. We store this parse information in a variable called schedules. Once we have done that, we iterate over the schedule and create a calendar Schedule that would display each element on the calendar.
We then call this method underr connect(){} in Our Calendar Controller.
connect(){this.getCalendardata()}
Creating Calendar schedules
First we must make some changes to our schedules controller. After a successful update, create or destroy action, we do not want to be redirected. To solve this, we simply comment out several lines from our controller.
To begin display events on the calendar we must first define a javascript method that creates events. We define a method called createCalendarSchedule. Tui provides certain event handlers that we can use to create events. We will use ‘beforeCreateSchedule’ to create a schedule. When a user clicks on the calendar, a popup form is presented. When data is filled in the popup, we collect the information, create a formdata, and submit it via Rails.ajax
reateCalendarSchedule(){letcalendar=this.calendar;calendar.on('beforeCreateSchedule',function(event){vartriggerEventName=event.triggerEventName;varschedule={id:1,calendarId:'1',title:event.title,category:'time',location:event.location,start:event.start,end:event.end}if (triggerEventName==='click'){// open writing simple schedule popup// schedule = {...};}elseif (triggerEventName==='dblclick'){// open writing detail schedule popup// schedule = {...};}calendar.createSchedules([schedule]);letformData=newFormData()formData.append('title',schedule.title);formData.append('category',schedule.category);formData.append('start',schedule.start._date);formData.append('end',schedule.end._date);formData.append('location',schedule.location);Rails.ajax({type:"POST",url:'/schedules',data:formData})});}
We then call this method under connect(){} in Our Calendar Controller.
We would use another event handler to update calendar schedules. When we click on an already created schedule, a pop would appear that would allow us to edit or delete this schedule. We would use ‘beforeUpdateSchedule’ event to handle submitting our data.
We now define a method that uses ‘the BeforeDeleteSchedule’ event handler to delete a schedule entry. This event is called when we click on a schedule on the calendar and click on ‘Delete’. The function makes a request via Rails.ajax to delete with selected schedule based on its ID.