1
|
|
|
import React, { useEffect, useRef, useState } from 'react'; |
2
|
|
|
import { Button, Card, CardBody, CardHeader, Col, Row, UncontrolledTooltip } from 'reactstrap'; |
3
|
|
|
import FullCalendar from '@fullcalendar/react'; |
4
|
|
|
import dayGridPlugin from '@fullcalendar/daygrid'; |
5
|
|
|
import bootstrapPlugin from '@fullcalendar/bootstrap'; |
6
|
|
|
import timeGridPlugin from '@fullcalendar/timegrid'; |
7
|
|
|
import listPlugin from '@fullcalendar/list'; |
8
|
|
|
import interactionPlugin from '@fullcalendar/interaction'; |
9
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
10
|
|
|
import DropdownFilter from '../email/inbox/DropdownFilter'; |
11
|
|
|
import CalendarEventModal from './CalendarEventModal'; |
12
|
|
|
import AddScheduleModal from './AddScheduleModal'; |
13
|
|
|
import Flex from '../common/Flex'; |
14
|
|
|
|
15
|
|
|
import events from '../../data/calendar/events'; |
16
|
|
|
|
17
|
|
|
const Calendar = () => { |
18
|
|
|
const calendarRef = useRef(); |
19
|
|
|
const [calendarApi, setCalendarApi] = useState({}); |
20
|
|
|
const [title, setTitle] = useState(''); |
21
|
|
|
const [currentFilter, setCurrentFilter] = useState('Month View'); |
22
|
|
|
const [isOpenModal, setIsOpenModal] = useState(false); |
23
|
|
|
const [isOpenScheduleModal, setIsOpenScheduleModal] = useState(false); |
24
|
|
|
const [modalEventContent, setModalEventContent] = useState(false); |
25
|
|
|
const [addScheduleStartDate, setAddScheduleStartDate] = useState(); |
26
|
|
|
|
27
|
|
|
const buttonText = { |
28
|
|
|
today: 'Today', |
29
|
|
|
month: 'Month view', |
30
|
|
|
week: 'week', |
31
|
|
|
day: 'day', |
32
|
|
|
listWeek: 'list view', |
33
|
|
|
listYear: 'year' |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
const eventTimeFormat = { |
37
|
|
|
hour: 'numeric', |
38
|
|
|
minute: '2-digit', |
39
|
|
|
omitZeroMinute: true, |
40
|
|
|
meridiem: true |
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
const viewName = ['Month View', 'Week View', 'Day View', 'List View', 'Year View']; |
44
|
|
|
|
45
|
|
|
const views = { |
46
|
|
|
week: { |
47
|
|
|
eventLimit: 3 |
48
|
|
|
} |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
const eventList = events.reduce( |
52
|
|
|
(acc, event) => (event.schedules ? acc.concat(event.schedules.concat(event)) : acc.concat(event)), |
53
|
|
|
[] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
const [initialEvents, setInitialEvents] = useState(eventList); |
57
|
|
|
|
58
|
|
|
useEffect(() => { |
59
|
|
|
setCalendarApi(calendarRef.current.getApi()); |
60
|
|
|
}, []); |
61
|
|
|
|
62
|
|
|
const handleFilter = filter => { |
63
|
|
|
setCurrentFilter(filter); |
64
|
|
|
switch (filter) { |
65
|
|
|
case 'Month View': |
66
|
|
|
calendarApi.changeView('dayGridMonth'); |
67
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
68
|
|
|
break; |
69
|
|
|
case 'Week View': |
70
|
|
|
calendarApi.changeView('timeGridWeek'); |
71
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
72
|
|
|
break; |
73
|
|
|
case 'Day View': |
74
|
|
|
calendarApi.changeView('timeGridDay'); |
75
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
76
|
|
|
break; |
77
|
|
|
case 'List View': |
78
|
|
|
calendarApi.changeView('listWeek'); |
79
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
80
|
|
|
break; |
81
|
|
|
default: |
82
|
|
|
calendarApi.changeView('listYear'); |
83
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
84
|
|
|
} |
85
|
|
|
}; |
86
|
|
|
|
87
|
|
|
const handleEventClick = info => { |
88
|
|
|
if (info.event.url) { |
89
|
|
|
window.open(info.event.url); |
90
|
|
|
info.jsEvent.preventDefault(); |
91
|
|
|
} else { |
92
|
|
|
setModalEventContent(info); |
93
|
|
|
setIsOpenModal(true); |
94
|
|
|
} |
95
|
|
|
}; |
96
|
|
|
|
97
|
|
|
return ( |
98
|
|
|
<> |
99
|
|
|
<Card className="mb-3"> |
100
|
|
|
<CardHeader> |
101
|
|
|
<Row noGutters className="align-items-center"> |
102
|
|
|
<Col xs="auto" className="d-flex justify-content-end order-md-1"> |
103
|
|
|
<UncontrolledTooltip placement="bottom" target="previous"> |
104
|
|
|
Previous |
105
|
|
|
</UncontrolledTooltip> |
106
|
|
|
<UncontrolledTooltip placement="bottom" target="next"> |
107
|
|
|
Next |
108
|
|
|
</UncontrolledTooltip> |
109
|
|
|
<Button |
110
|
|
|
onClick={() => { |
111
|
|
|
calendarApi.prev(); |
112
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
113
|
|
|
}} |
114
|
|
|
color="link" |
115
|
|
|
className="icon-item icon-item-sm icon-item-hover shadow-none p-0 mr-1 ml-md-2" |
116
|
|
|
id="previous" |
117
|
|
|
> |
118
|
|
|
<FontAwesomeIcon icon="arrow-left" /> |
119
|
|
|
</Button> |
120
|
|
|
<Button |
121
|
|
|
onClick={() => { |
122
|
|
|
calendarApi.next(); |
123
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
124
|
|
|
}} |
125
|
|
|
color="link" |
126
|
|
|
className="icon-item icon-item-sm icon-item-hover shadow-none p-0 mr-1" |
127
|
|
|
id="next" |
128
|
|
|
> |
129
|
|
|
<FontAwesomeIcon icon="arrow-right" /> |
130
|
|
|
</Button> |
131
|
|
|
</Col> |
132
|
|
|
<Col xs="auto" md="auto" className="order-md-2"> |
133
|
|
|
<h4 className="mb-0 fs-0 fs-sm-1 fs-lg-2 calendar-title"> |
134
|
|
|
{title || `${calendarApi.currentDataManager?.data?.viewTitle}`} |
135
|
|
|
</h4> |
136
|
|
|
</Col> |
137
|
|
|
<Col xs md="auto" tag={Flex} justify="end" className="order-md-3"> |
138
|
|
|
<Button |
139
|
|
|
size="sm" |
140
|
|
|
color="falcon-primary" |
141
|
|
|
onClick={() => { |
142
|
|
|
calendarApi.today(); |
143
|
|
|
setTitle(calendarApi.getCurrentData().viewTitle); |
144
|
|
|
}} |
145
|
|
|
> |
146
|
|
|
Today |
147
|
|
|
</Button> |
148
|
|
|
</Col> |
149
|
|
|
<Col md="auto" className="d-md-none"> |
150
|
|
|
<hr /> |
151
|
|
|
</Col> |
152
|
|
|
<Col xs="auto" className="d-flex order-md-0"> |
153
|
|
|
<Button color="primary" size="sm" onClick={() => setIsOpenScheduleModal(true)}> |
154
|
|
|
<FontAwesomeIcon icon="plus" className="mr-1" /> Add Schedule |
155
|
|
|
</Button> |
156
|
|
|
</Col> |
157
|
|
|
<Col className="d-flex justify-content-end order-md-2"> |
158
|
|
|
<DropdownFilter |
159
|
|
|
className="mr-2" |
160
|
|
|
filters={viewName} |
161
|
|
|
currentFilter={currentFilter} |
162
|
|
|
handleFilter={handleFilter} |
163
|
|
|
icon="sort" |
164
|
|
|
right |
165
|
|
|
/> |
166
|
|
|
</Col> |
167
|
|
|
</Row> |
168
|
|
|
</CardHeader> |
169
|
|
|
<CardBody className="p-0"> |
170
|
|
|
<FullCalendar |
171
|
|
|
ref={calendarRef} |
172
|
|
|
headerToolbar={false} |
173
|
|
|
plugins={[dayGridPlugin, bootstrapPlugin, timeGridPlugin, interactionPlugin, listPlugin]} |
174
|
|
|
initialView="dayGridMonth" |
175
|
|
|
themeSystem="bootstrap" |
176
|
|
|
dayMaxEvents={2} |
177
|
|
|
height={800} |
178
|
|
|
stickyHeaderDates={false} |
179
|
|
|
editable |
180
|
|
|
selectable |
181
|
|
|
selectMirror |
182
|
|
|
select={info => { |
183
|
|
|
setIsOpenScheduleModal(true); |
184
|
|
|
setAddScheduleStartDate(info.start); |
185
|
|
|
}} |
186
|
|
|
views={views} |
187
|
|
|
eventTimeFormat={eventTimeFormat} |
188
|
|
|
eventClick={handleEventClick} |
189
|
|
|
events={initialEvents} |
190
|
|
|
buttonText={buttonText} |
191
|
|
|
/> |
192
|
|
|
</CardBody> |
193
|
|
|
</Card> |
194
|
|
|
|
195
|
|
|
<AddScheduleModal |
196
|
|
|
isOpenScheduleModal={isOpenScheduleModal} |
197
|
|
|
setIsOpenScheduleModal={setIsOpenScheduleModal} |
198
|
|
|
initialEvents={initialEvents} |
199
|
|
|
setInitialEvents={setInitialEvents} |
200
|
|
|
addScheduleStartDate={addScheduleStartDate} |
201
|
|
|
setAddScheduleStartDate={setAddScheduleStartDate} |
202
|
|
|
/> |
203
|
|
|
|
204
|
|
|
<CalendarEventModal |
205
|
|
|
isOpenModal={isOpenModal} |
206
|
|
|
setIsOpenModal={setIsOpenModal} |
207
|
|
|
modalEventContent={modalEventContent} |
208
|
|
|
/> |
209
|
|
|
</> |
210
|
|
|
); |
211
|
|
|
}; |
212
|
|
|
|
213
|
|
|
export default Calendar; |
214
|
|
|
|