AdminMonthly.js 5.9 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import { useState, useEffect, useRef } from "react";
Kim, Subin's avatar
Kim, Subin committed
2
3
4
import CalendarBtn from "../Buttons/CalendarBtn";
import DatePickerModal from "../Modal/DatePickerModal";
import ScheduleModal from "../Modal/ScheduleModal";
Kim, Subin's avatar
Kim, Subin committed
5
import scheduleApi from "../../apis/schedule.api";
Kim, Subin's avatar
Kim, Subin committed
6
import catchErrors from "../../utils/catchErrors";
Kim, Subin's avatar
Kim, Subin committed
7
8
9
10
11
12
13
14
15
16
17
import moment from 'moment';
import FullCalendar from '@fullcalendar/react';
import dayGridPlugin from '@fullcalendar/daygrid';
import interactionPlugin from "@fullcalendar/interaction";
import bootstrapPlugin from '@fullcalendar/bootstrap';
import '@fortawesome/fontawesome-free/css/all.css';

const AdminMonthly = () => {
    const [initialDate, setInitialDate] = useState(moment().format('YYYY-MM-DD'))
    const [changeDate, setChangeDate] = useState(moment().format('YYYY-MM-DD'))
    const [pickerShow, setPickerShow] = useState(false)
Kim, Subin's avatar
Kim, Subin committed
18
19
20
    const [dateShow, setDateShow] = useState({ date: moment().format('YYYY-MM-DD'), show: false })
    const [scheduleList, setScheduleList] = useState([])
    const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
21
22
23
24
25
26
27
28
29
30
    const calendarRef = useRef(null)
    const calenIconRef = useRef(null)
    let calendar = null

    useEffect(() => {
        if (calendarRef && calendarRef.current) {
            calendar = calendarRef.current.getApi()
        }
    })

Kim, Subin's avatar
Kim, Subin committed
31
32
33
34
35
36
    useEffect(() => {
        getAll()
        return () => {
            getAll()
        }
    }, [dateShow.show])
Kim, Subin's avatar
Kim, Subin committed
37

Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    useEffect(() => {
        if (calenIconRef && calenIconRef.current) {
            calenIconRef.current.addEventListener('click', () => {
                calendar.today()
                let date = moment(calendar.getDate()).format('YYYY-MM-DD')
                setChangeDate(date)
            })
        }
        return () => {
            if (calenIconRef && calenIconRef.current) {
                calenIconRef.current.removeEventListener('click', () => {
                    calendar.today()
                    let date = moment(calendar.getDate()).format('YYYY-MM-DD')
                    setChangeDate(date)
                })
            }
        }
    }, [calenIconRef.current])

    useEffect(() => {
        calendar.gotoDate(changeDate)
Kim, Subin's avatar
Kim, Subin committed
59
        getAll()
Kim, Subin's avatar
Kim, Subin committed
60
61
    }, [changeDate])

Kim, Subin's avatar
Kim, Subin committed
62
    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
63
        calendar.removeAllEvents()
Kim, Subin's avatar
Kim, Subin committed
64
65
66
67
68
69
70
71
72
73
74
75
76
        calendar.addEventSource(scheduleList)
    }, [scheduleList])

    async function getAll() {
        try {
            setError("")
            const resList = await scheduleApi.getbyMonth(changeDate)
            setScheduleList(resList)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    return (
        <>
            <div ref={calenIconRef} className="position-absolute" style={{ top: "7px", left: "7px" }}>
                <CalendarBtn date={moment(initialDate).format('DD')} />
            </div>
            <FullCalendar
                ref={calendarRef}
                plugins={[dayGridPlugin, interactionPlugin, bootstrapPlugin]}
                initialView="dayGridMonth"
                initialDate={initialDate}
                headerToolbar={{
                    start: 'prev',
                    center: 'myCustomButton',
                    end: 'next'
                }}
                dayHeaderContent={(date) => {
                    const weekList = ["", "", "", "", "", "", ""]
                    return weekList[date.dow]
                }}
Kim, Subin's avatar
more    
Kim, Subin committed
96
97
98
99
100
                views={{
                    dayGridMonth: {
                        dayMaxEvents: 3
                    }
                }}
Kim, Subin's avatar
Kim, Subin committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
                validRange={{
                    start: moment(initialDate).subtract(3, 'years').format('YYYY-MM[-01]'),
                    end: moment(initialDate).add(3, 'years').add(1, 'months').format('YYYY-MM[-01]')
                }}
                customButtons={{
                    myCustomButton: {
                        text: moment(changeDate).format('YYYY.MM'),
                        click: () => {
                            setPickerShow(true)
                            return <button className="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#datePicker" aria-controls="datePicker" />
                        }
                    },
                    prev: {
                        icon: "fa-chevron-left",
                        click: () => {
                            calendar.prev()
                            let date = moment(calendar.getDate()).format('YYYY-MM-DD')
                            setChangeDate(date)
                        }
                    },
                    next: {
                        icon: "fa-chevron-right",
                        click: () => {
                            calendar.next()
                            let date = moment(calendar.getDate()).format('YYYY-MM-DD')
                            setChangeDate(date)
                        }
                    }
                }}
                dateClick={({ dateStr }) => {
                    setDateShow({ ...dateShow, date: dateStr, show: true })
                    return <button type="button" className="btn btn-primary" data-bs-toggle="modal" data-bs-target="#scheduleModal"></button>
                }}
                timeZone="local"
135
                events={scheduleList}
Kim, Subin's avatar
more    
Kim, Subin committed
136
                eventLimit={3}
Kim, Subin's avatar
Kim, Subin committed
137
138
139
140
                moreLinkContent={arg => arg.shortText}
                moreLinkClick={info => {
                    setDateShow({ ...dateShow, date: moment(info.date).format('YYYY-MM-DD'), show: true })
                    return <button type="button" className="btn btn-primary" data-bs-toggle="modal" data-bs-target="#scheduleModal"></button>
Kim, Subin's avatar
more    
Kim, Subin committed
141
                }}
Kim, Subin's avatar
Kim, Subin committed
142
143
144
                themeSystem='bootstrap'
                height='78vh'
            />
Kim, Subin's avatar
Kim, Subin committed
145
            <DatePickerModal initialDate={initialDate} changeDate={changeDate} setChangeDate={setChangeDate} show={pickerShow} setShow={setPickerShow} />
Kim, Subin's avatar
Kim, Subin committed
146
147
148
149
150
151
            <ScheduleModal dateShow={dateShow} setDateShow={setDateShow} />
        </>
    )
}

export default AdminMonthly