Schedule.js 3.69 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
import { Image, Button } from 'react-bootstrap';
import leftArrow from '../caret-left-fill.svg';
import rightArrow from '../caret-right-fill.svg';
Kim, Subin's avatar
Kim, Subin committed
11
12
13

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

Kim, Subin's avatar
Kim, Subin committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  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
41
42
43
  function getDataAction(target) {
    return target.dataset ? target.dataset.action : target.getAttribute('data-action');
  }
Kim, Subin's avatar
Kim, Subin committed
44

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

Kim, Subin's avatar
Kim, Subin committed
49
50
51
52
53
54
55
56
57
58
59
60
61
    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
62

Kim, Subin's avatar
Kim, Subin committed
63
64
    setRenderRangeText();
  }
Kim, Subin's avatar
Kim, Subin committed
65

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

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

  return (
Kim, Subin's avatar
Kim, Subin committed
81
    <div>
Kim, Subin's avatar
Kim, Subin committed
82
      <div id="menu" className="p-2">
Kim, Subin's avatar
Kim, Subin committed
83
        <span id="menu-navi" onClick={(e) => onClickNavi(e)}>
Kim, Subin's avatar
Kim, Subin committed
84
85
86
87
88
89
90
          <Button variant="default" size="sm" className="move-today" data-action="move-today">Today</Button>
          <Button variant="default" size="sm" className="move-day" data-action="move-prev">
            <Image class="calendar-icon" src={leftArrow} data-action="move-prev"></Image>
          </Button>
          <Button variant="default" size="sm" className="move-day" data-action="move-next">
            <Image className="calendar-icon" src={rightArrow} data-action="move-next"></Image>
          </Button>
Kim, Subin's avatar
Kim, Subin committed
91
        </span>
Kim, Subin's avatar
Kim, Subin committed
92
        <span id="renderRange" className="render-range ml-2" style={{ height: "5em" }}>{period}</span>
Kim, Subin's avatar
Kim, Subin committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      </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
109
        schedules={reserve}
Kim, Subin's avatar
Kim, Subin committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        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
130
131
132
133
  )
}

export default Cal