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";
Kim, Subin's avatar
Kim, Subin committed
6
import moment from 'moment';
Kim, Subin's avatar
Kim, Subin committed
7
import axios from 'axios';
Kim, Subin's avatar
Kim, Subin committed
8
9
10

function Cal(props) {
  const calendarRef = useRef(null);
Kim, Subin's avatar
Kim, Subin committed
11
12
  const [reserve, setReserve] = useState([]);
  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
  });

Kim, Subin's avatar
Kim, Subin 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)
      });
  }

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

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

Kim, Subin's avatar
Kim, Subin committed
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

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

Kim, Subin's avatar
Kim, Subin committed
63
64
65
66
67
68
  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'));
Kim, Subin's avatar
Kim, Subin committed
69
    setPeriod(html.join(''))
Kim, Subin's avatar
Kim, Subin committed
70
  }
Kim, Subin's avatar
Kim, Subin committed
71

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

  return (
Kim, Subin's avatar
Kim, Subin committed
78
    <div>
Kim, Subin's avatar
Kim, Subin committed
79
      <div id="menu">
Kim, Subin's avatar
Kim, Subin committed
80
81
82
83
84
85
86
87
88
        <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>
Kim, Subin's avatar
Kim, Subin committed
89
        <span id="renderRange" className="render-range" style={{ height: "5em" }}>{period}</span>
Kim, Subin's avatar
Kim, Subin committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
      </div>
      <Calendar
        ref={calendarRef}
        height="100%"
        calendars={[
          {
            id: 'Subject',
            bgColor: '#a9a9a9',
            borderColor: '#a9a9a9',
            isReadOnly: 'true'
          }
        ]}
        view="week"
        disableDblClick={false}
        disableClick={true}
        isReadOnly={true}
Kim, Subin's avatar
Kim, Subin committed
106
        schedules={reserve}
Kim, Subin's avatar
Kim, Subin 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