|
| 1 | +import React from 'react'; |
| 2 | +import FullCalendar from '@fullcalendar/react'; |
| 3 | +import dayGridPlugin from '@fullcalendar/daygrid'; |
| 4 | +import timeGridPlugin from '@fullcalendar/timegrid'; |
| 5 | +import moment from 'moment'; |
| 6 | +import interactionPlugin from '@fullcalendar/interaction'; // needed for dayClick |
| 7 | +import { motion } from 'framer-motion'; |
| 8 | +import PropTypes from 'prop-types'; |
| 9 | +// import '@fullcalendar/common/main.css'; |
| 10 | +// import '@fullcalendar/daygrid/main.css'; |
| 11 | +// import '@fullcalendar/timegrid/main.css'; |
| 12 | + |
| 13 | +// this is used to make custom view in calendar for event |
| 14 | + |
| 15 | +export default class Schedule extends React.Component { |
| 16 | + constructor(props) { |
| 17 | + super(props); |
| 18 | + this.calendarRef = React.createRef(); |
| 19 | + this.state = { |
| 20 | + events: [], |
| 21 | + }; |
| 22 | + } |
| 23 | + |
| 24 | + componentDidMount() { |
| 25 | + const cleanedEvents = this.props?.data.map( |
| 26 | + ({ movieName, startTime, endTime, showId }) => { |
| 27 | + const startDateTime = moment(startTime).format('YYYY-MM-DD HH:mm:ss'); |
| 28 | + const endDateTime = moment(endTime).format('YYYY-MM-DD HH:mm:ss'); |
| 29 | + return { |
| 30 | + title: movieName, |
| 31 | + start: startDateTime, |
| 32 | + end: endDateTime, |
| 33 | + id: `/show/${showId}`, |
| 34 | + }; |
| 35 | + }, |
| 36 | + ); |
| 37 | + this.setState({ events: cleanedEvents }); |
| 38 | + } |
| 39 | + |
| 40 | + redirectToEventPage = (selectionInfo) => { |
| 41 | + window.open(selectionInfo.event._def.publicId, '_blank').focus(); |
| 42 | + }; |
| 43 | + |
| 44 | + setDate = (e) => { |
| 45 | + const calendarApi = this.calendarRef.current.getApi(); |
| 46 | + calendarApi.gotoDate(e.target.value); |
| 47 | + }; |
| 48 | + |
| 49 | + changeView = () => { |
| 50 | + const calendarApi = this.calendarRef.current.getApi(); |
| 51 | + const currentOption = calendarApi.getOption('eventDisplay'); |
| 52 | + const circleOfLife = [ |
| 53 | + 'auto', |
| 54 | + 'block', |
| 55 | + 'list-item', |
| 56 | + 'background', |
| 57 | + 'inverse-background', |
| 58 | + 'none', |
| 59 | + ]; |
| 60 | + const i = circleOfLife.indexOf(currentOption); |
| 61 | + const newI = (i + 1) % (circleOfLife.length - 1); |
| 62 | + calendarApi.setOption('eventDisplay', circleOfLife[newI]); |
| 63 | + }; |
| 64 | + |
| 65 | + render() { |
| 66 | + return ( |
| 67 | + <div> |
| 68 | + <FullCalendar |
| 69 | + ref={this.calendarRef} |
| 70 | + height="800px" |
| 71 | + stickyHeaderDates={true} |
| 72 | + stickyFooterScrollbar={true} |
| 73 | + plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin]} |
| 74 | + headerToolbar={{ |
| 75 | + center: 'dayGridMonth,timeGridWeek,timeGridDay', |
| 76 | + }} |
| 77 | + eventColor="#B388DD" |
| 78 | + // eventBorderColor="" |
| 79 | + // dateClick={this.handleDateClick} |
| 80 | + // eventContent={renderEventContent} |
| 81 | + defaultView="timeGridWeek" |
| 82 | + events={this.state.events} |
| 83 | + eventDisplay="auto" |
| 84 | + eventTimeFormat={{ |
| 85 | + // like '14:30:00' |
| 86 | + hour: '2-digit', |
| 87 | + minute: '2-digit', |
| 88 | + meridiem: false, |
| 89 | + }} |
| 90 | + eventClick={this.redirectToEventPage} |
| 91 | + eventMouseEnter={this.popOver} |
| 92 | + dayMaxEventRows={true} |
| 93 | + dayMaxEvents={true} |
| 94 | + /> |
| 95 | + {this.state.alert} |
| 96 | + <div className="flex w-full mt-4"> |
| 97 | + <div className="font-semibold text-lg"> |
| 98 | + Jump to : |
| 99 | + <input type="date" className="ml-2 " onChange={this.setDate} /> |
| 100 | + </div> |
| 101 | + <div className="ml-auto"> |
| 102 | + <motion.button |
| 103 | + type="button" |
| 104 | + className="rounded-lg font-semibold p-2 px-4 text-base text-white hover:lightalpha active:lightalpha " |
| 105 | + onClick={this.changeView} |
| 106 | + style={{ backgroundColor: '#b388dd' }} |
| 107 | + whileHover={{ scale: 1.05 }} |
| 108 | + whileTap={{ scale: 0.9 }} |
| 109 | + > |
| 110 | + {' '} |
| 111 | + <i className="fas fa-wrench text-sm" /> Toggle View |
| 112 | + </motion.button> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +Schedule.propTypes = { |
| 121 | + data: PropTypes.arrayOf( |
| 122 | + PropTypes.shape({ |
| 123 | + movieName: PropTypes.string.isRequired, |
| 124 | + startTime: PropTypes.string.isRequired, |
| 125 | + endTime: PropTypes.string.isRequired, |
| 126 | + showId: PropTypes.string.isRequired, |
| 127 | + }), |
| 128 | + ).isRequired, |
| 129 | +}; |
0 commit comments