Weekly.js 4.04 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
3
import { useState, useEffect, useRef } from "react";
import { useHistory, useParams } from "react-router-dom";
import Date from "./DateSet.js";
Kim, Subin's avatar
Kim, Subin committed
4
import moment from 'moment';
Kim, Subin's avatar
Kim, Subin committed
5
import FullCalendar, { createPlugin } from '@fullcalendar/react';
Kim, Subin's avatar
Kim, Subin committed
6
7
import interactionPlugin from "@fullcalendar/interaction";
import bootstrapPlugin from '@fullcalendar/bootstrap';
Kim, Subin's avatar
Monthly    
Kim, Subin committed
8
9

const Weekly = () => {
Kim, Subin's avatar
Kim, Subin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    const history = useHistory()
    const { date } = useParams()
    const [chooseDate, setChooseDate] = useState(moment(date).format("YYYY-MM-DD"))
    const [week, setWeek] = useState([
        { date: moment(date).day(0).format("YYYY-MM-DD"), rate: "75" },
        { date: moment(date).day(1).format("YYYY-MM-DD"), rate: "85" },
        { date: moment(date).day(2).format("YYYY-MM-DD"), rate: "40" },
        { date: moment(date).day(3).format("YYYY-MM-DD"), rate: "100" },
        { date: moment(date).day(4).format("YYYY-MM-DD"), rate: "" },
        { date: moment(date).day(5).format("YYYY-MM-DD"), rate: "0" },
        { date: moment(date).day(6).format("YYYY-MM-DD"), rate: "" }
    ])
    const calendarRef = useRef(null)
    let calendar = null

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

    useEffect(() => {
        setChooseDate(moment(date).format("YYYY-MM-DD"))
        setWeek([
            { date: moment(date).day(0).format("YYYY-MM-DD"), rate: "7" },
            { date: moment(date).day(1).format("YYYY-MM-DD"), rate: "8" },
            { date: moment(date).day(2).format("YYYY-MM-DD"), rate: "4" },
            { date: moment(date).day(3).format("YYYY-MM-DD"), rate: "100" },
            { date: moment(date).day(4).format("YYYY-MM-DD"), rate: "" },
            { date: moment(date).day(5).format("YYYY-MM-DD"), rate: "0" },
            { date: moment(date).day(6).format("YYYY-MM-DD"), rate: "" }
        ])
    }, [date])

    function prev() {
        calendar.prev()
        let date = moment(calendar.getDate()).format('YYYY-MM-DD')
        history.push(`/todo/${date}`)
    }

    function next() {
        calendar.next()
        let date = moment(calendar.getDate()).format('YYYY-MM-DD')
        history.push(`/todo/${date}`)
    }

    function gotoDate(date) {
        calendar.gotoDate(date)
        history.push(`/todo/${date}`)
    }

    const CustomeWeeklyView = ({ dateProfile }) => {
        let current = moment(dateProfile.currentRange.start).format("YYYY-MM-DD")
        
        return (
Kim, Subin's avatar
Kim, Subin committed
65
            <div className="fc-custom-view weekly-view d-flex row-cols-9" style={{ cursor: "pointer" }}>
Kim, Subin's avatar
Kim, Subin committed
66
67
68
69
70
71
72
73
74
75
76
77
78
                <i className="col bi bi-chevron-left align-self-center" onClick={prev} style={{ fontSize: "2em" }} />
                {week.map((info, idx) => <Date index={idx} info={info} today={moment(info.date).isSame(current) ? true : false} handleClick={gotoDate} />)}
                <i className="col bi bi-chevron-right align-self-center" onClick={next} style={{ fontSize: "2em" }} />
            </div>
        )
    }

    const customViewPlugin = createPlugin({
        views: {
            week: CustomeWeeklyView
        }
    })

Kim, Subin's avatar
Monthly    
Kim, Subin committed
79
80
    return (
        <FullCalendar
Kim, Subin's avatar
Kim, Subin committed
81
82
83
84
            ref={calendarRef}
            plugins={[customViewPlugin, interactionPlugin, bootstrapPlugin]}
            initialView="week"
            initialDate={chooseDate}
Kim, Subin's avatar
Monthly    
Kim, Subin committed
85
            headerToolbar={{
Kim, Subin's avatar
Kim, Subin committed
86
87
88
89
                start: 'title',
                center: '',
                end: ''
            }}
Kim, Subin's avatar
Kim, Subin committed
90
91
92
93
94
95
96
97
98
99
100
            titleFormat={() => moment(chooseDate).format("YYYY[년 ]MM[월]")}
            views={{
                week: {
                    validRange: {
                        start: moment(chooseDate).subtract(3, 'years').format('YYYY-MM[-01]'),
                        end: moment(chooseDate).add(3, 'years').add(1, 'months').format('YYYY-MM[-01]')
                    },
                    dateIncrement: {
                        days: 7
                    }
                }
Kim, Subin's avatar
Monthly    
Kim, Subin committed
101
            }}
Kim, Subin's avatar
Kim, Subin committed
102
103
104
            timeZone="local"
            themeSystem='bootstrap'
            height="fit-content"
Kim, Subin's avatar
Monthly    
Kim, Subin committed
105
106
107
108
109
        />
    )
}

export default Weekly