Schedule.js 3.54 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
Lee Jin Ju's avatar
Lee Jin Ju committed
2
3
4
5
import Calendar from '@toast-ui/react-calendar';
import "tui-calendar/dist/tui-calendar.css";
import "tui-date-picker/dist/tui-date-picker.css";
import "tui-time-picker/dist/tui-time-picker.css";
6
import moment from 'moment';
CHAERIN KIM's avatar
CHAERIN KIM committed
7
8
9
import axios from 'axios';

function Cal(props) {
10
  const calendarRef = useRef(null);
CHAERIN KIM's avatar
CHAERIN KIM committed
11
  const [reserve, setReserve] = useState([]);
12
  const [period, setPeriod] = useState();
Lee Jin Ju's avatar
Lee Jin Ju committed
13
14
15
  const [myTheme, setMyTheme] = useState({
    'common.dayname.color': '#333',
    'common.today.color': '#333',
Kim, Subin's avatar
Kim, Subin committed
16
    'common.creationGuide.backgroundColor': 'gray',
Lee Jin Ju's avatar
Lee Jin Ju committed
17
18
  });

CHAERIN KIM's avatar
CHAERIN KIM committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  function getReserve(room) {
    axios.get(`/reserves/room/${room}`, {
      headers: { authorization: localStorage.getItem('token') },
    })
      .then(res => {
        const reserves = res.data.map(item => ({
          id: item._id,
          start: item.start,
          end: item.end,
          calendarId: 'Subject',
          category: 'time',
        }))
        setReserve(reserves);
      })
      .catch(err => {
        alert(err.error)
      });
  }

38
39
40
  function getDataAction(target) {
    return target.dataset ? target.dataset.action : target.getAttribute('data-action');
  }
Kim, Subin's avatar
Kim, Subin committed
41

42
43
44
  function onClickNavi(e) {
    const cal = calendarRef.current.getInstance();
    const action = getDataAction(e.target);
Kim, Subin's avatar
Kim, Subin committed
45

46
47
48
49
50
51
52
53
54
55
56
57
58
    switch (action) {
      case 'move-prev':
        cal.prev();
        break;
      case 'move-next':
        cal.next();
        break;
      case 'move-today':
        cal.today();
        break;
      default:
        return;
    }
Kim, Subin's avatar
Kim, Subin committed
59

60
61
    setRenderRangeText();
  }
Kim, Subin's avatar
Kim, Subin committed
62

63
64
65
66
67
68
69
70
  function setRenderRangeText() {
    const cal = calendarRef.current.getInstance();
    let html = [];
    html.push(moment(cal.getDateRangeStart().getTime()).format('YYYY.MM.DD'));
    html.push(' ~ ');
    html.push(moment(cal.getDateRangeEnd().getTime()).format(' MM.DD'));
    setPeriod(html.join(''))
  }
Kim, Subin's avatar
Kim, Subin committed
71

72
73
  useEffect(() => {
    setRenderRangeText();
CHAERIN KIM's avatar
CHAERIN KIM committed
74
    getReserve(props.room);
75
  }, [props.room])
Lee Jin Ju's avatar
Lee Jin Ju committed
76
77

  return (
78
79
80
81
82
83
84
85
86
87
88
89
90
    <div>
      <div id="menu">
        <span id="menu-navi" onClick={(e) => onClickNavi(e)}>
          <button type="button" className="btn btn-default btn-sm move-today" data-action="move-today">Today</button>
          <button type="button" className="btn btn-default btn-sm move-day" data-action="move-prev">
            <i class="calendar-icon ic-arrow-line-left" data-action="move-prev"></i>
          </button>
          <button type="button" className="btn btn-default btn-sm move-day" data-action="move-next">
            <i className="calendar-icon ic-arrow-line-right" data-action="move-next"></i>
          </button>
        </span>
        <span id="renderRange" className="render-range" style={{ height: "5em" }}>{period}</span>
      </div>
CHAERIN KIM's avatar
CHAERIN KIM committed
91
      <Calendar
92
        ref={calendarRef}
CHAERIN KIM's avatar
CHAERIN KIM committed
93
94
95
96
97
98
99
100
101
102
103
104
        height="100%"
        calendars={[
          {
            id: 'Subject',
            bgColor: '#a9a9a9',
            borderColor: '#a9a9a9',
            isReadOnly: 'true'
          }
        ]}
        view="week"
        disableDblClick={false}
        disableClick={true}
105
106
        isReadOnly={true}
        schedules={reserve}
CHAERIN KIM's avatar
CHAERIN KIM committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        scheduleView={['time']}
        taskView={false}
        theme={myTheme}
        timezones={[
          {
            timezoneOffset: 540,
            displayLabel: 'GMT+09:00',
            tooltip: 'Seoul'
          },
        ]}
        useDetailPopup
        useCreationPopup
        view={"week"}
        week={{
          workweek: true,
          hourStart: 8,
          hourEnd: 23
        }}
      />
    </div>
Lee Jin Ju's avatar
Lee Jin Ju committed
127
128
129
130
  )
}

export default Cal