Weekly.js 4.44 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
import { useState, useEffect, useRef } from "react";
import { useHistory, useParams } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
import Date from "./DateSet";
Kim, Subin's avatar
Todo    
Kim, Subin committed
4
5
6
import todoApi from "../../apis/todo.api";
import { useAuth } from "../../utils/context";
import catchErrors from "../../utils/catchErrors";
Kim, Subin's avatar
Kim, Subin committed
7
import moment from 'moment';
Kim, Subin's avatar
Kim, Subin committed
8
import FullCalendar, { createPlugin } from '@fullcalendar/react';
Kim, Subin's avatar
Kim, Subin committed
9
10
import interactionPlugin from "@fullcalendar/interaction";
import bootstrapPlugin from '@fullcalendar/bootstrap';
Kim, Subin's avatar
Monthly    
Kim, Subin committed
11
12

const Weekly = () => {
Kim, Subin's avatar
Todo    
Kim, Subin committed
13
    const { user } = useAuth()
Kim, Subin's avatar
Kim, Subin committed
14
15
    const history = useHistory()
    const { date } = useParams()
Kim, Subin's avatar
Todo    
Kim, Subin committed
16
    const [initialDate, setInitialDate] = useState(moment().format("YYYY-MM-DD"))
Kim, Subin's avatar
Kim, Subin committed
17
18
    const [chooseDate, setChooseDate] = useState(moment(date).format("YYYY-MM-DD"))
    const [week, setWeek] = useState([
Kim, Subin's avatar
Kim, Subin committed
19
20
21
22
        { date: moment(date).day(0).format("YYYY-MM-DD"), rate: "" },
        { date: moment(date).day(1).format("YYYY-MM-DD"), rate: "" },
        { date: moment(date).day(2).format("YYYY-MM-DD"), rate: "" },
        { date: moment(date).day(3).format("YYYY-MM-DD"), rate: "" },
Kim, Subin's avatar
Kim, Subin committed
23
        { date: moment(date).day(4).format("YYYY-MM-DD"), rate: "" },
Kim, Subin's avatar
Kim, Subin committed
24
        { date: moment(date).day(5).format("YYYY-MM-DD"), rate: "" },
Kim, Subin's avatar
Kim, Subin committed
25
26
        { date: moment(date).day(6).format("YYYY-MM-DD"), rate: "" }
    ])
Kim, Subin's avatar
Todo    
Kim, Subin committed
27
    const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
28
29
30
31
32
33
34
35
36
37
    const calendarRef = useRef(null)
    let calendar = null

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

    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
38
39
        setChooseDate(date)
        getTodoList(date)
Kim, Subin's avatar
Kim, Subin committed
40
41
    }, [date])

Kim, Subin's avatar
Kim, Subin committed
42
    async function getTodoList(date) {
Kim, Subin's avatar
Todo    
Kim, Subin committed
43
44
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
45
            let weekArr = []
Kim, Subin's avatar
Todo    
Kim, Subin committed
46
            const result = await todoApi.getTodopercent(user.id, moment(date).day(0).format("YYYY-MM-DD"), moment(date).day(6).format("YYYY-MM-DD"))
Kim, Subin's avatar
Kim, Subin committed
47
48
49
50
51
52
53
            for (let i = 0; i < 7; i++) {
                const generateDate = moment(date).day(i).format("YYYY-MM-DD")
                const find = result.find(el => el.date === generateDate)
                if (find) weekArr[i] = find
                else weekArr[i] = { date: generateDate, rate: "" }
            }
            setWeek(weekArr)
Kim, Subin's avatar
Todo    
Kim, Subin committed
54
55
56
57
58
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
59
60
    function prev() {
        calendar.prev()
Kim, Subin's avatar
Kim, Subin committed
61
        const date = moment(calendar.getDate()).format('YYYY-MM-DD')
Kim, Subin's avatar
Kim, Subin committed
62
63
64
65
66
        history.push(`/todo/${date}`)
    }

    function next() {
        calendar.next()
Kim, Subin's avatar
Kim, Subin committed
67
        const date = moment(calendar.getDate()).format('YYYY-MM-DD')
Kim, Subin's avatar
Kim, Subin committed
68
69
70
71
72
73
74
75
76
77
        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")
Kim, Subin's avatar
Todo    
Kim, Subin committed
78

Kim, Subin's avatar
Kim, Subin committed
79
        return (
Kim, Subin's avatar
Kim, Subin committed
80
            <div className="fc-custom-view weekly-view d-flex row-cols-9" style={{ cursor: "pointer" }}>
Kim, Subin's avatar
Kim, Subin committed
81
82
83
84
85
86
87
88
89
90
91
92
93
                <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
94
95
    return (
        <FullCalendar
Kim, Subin's avatar
Kim, Subin committed
96
97
98
99
            ref={calendarRef}
            plugins={[customViewPlugin, interactionPlugin, bootstrapPlugin]}
            initialView="week"
            initialDate={chooseDate}
Kim, Subin's avatar
Monthly    
Kim, Subin committed
100
            headerToolbar={{
Kim, Subin's avatar
Kim, Subin committed
101
102
103
104
                start: 'title',
                center: '',
                end: ''
            }}
Kim, Subin's avatar
Kim, Subin committed
105
106
107
108
            titleFormat={() => moment(chooseDate).format("YYYY[년 ]MM[월]")}
            views={{
                week: {
                    validRange: {
Kim, Subin's avatar
Todo    
Kim, Subin committed
109
110
                        start: moment(initialDate).subtract(3, 'years').format('YYYY-MM[-01]'),
                        end: moment(initialDate).add(3, 'years').add(1, 'months').format('YYYY-MM[-01]')
Kim, Subin's avatar
Kim, Subin committed
111
112
113
114
115
                    },
                    dateIncrement: {
                        days: 7
                    }
                }
Kim, Subin's avatar
Monthly    
Kim, Subin committed
116
            }}
Kim, Subin's avatar
Kim, Subin committed
117
118
119
            timeZone="local"
            themeSystem='bootstrap'
            height="fit-content"
Kim, Subin's avatar
Monthly    
Kim, Subin committed
120
121
122
123
124
        />
    )
}

export default Weekly