DatePickerModal.js 4.2 KB
Newer Older
Kim, Subin's avatar
Monthly    
Kim, Subin committed
1
2
import { useState, useEffect } from "react";
import moment from 'moment';
Kim, Subin's avatar
Kim, Subin committed
3
import styles from "./modal.module.scss";
Kim, Subin's avatar
Monthly    
Kim, Subin committed
4

Kim, Subin's avatar
Kim, Subin committed
5
const DatePickerModal = ({ initialDate, changeDate, setChangeDate, show, setShow }) => {
Kim, Subin's avatar
Monthly    
Kim, Subin committed
6
7
8
9
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
    const [date, setDate] = useState({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 })
    const [year, setYear] = useState({ start: moment(initialDate).get('year') - 3, end: moment(initialDate).get('year') + 3 })
    const month = moment(initialDate).get('month') + 1

    useEffect(() => {
        setDate({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 })
    }, [changeDate])

    function up(e) {
        const { id } = e.target
        if (id === "year") {
            if (date[id] < year.end) setDate({ ...date, [id]: date[id] + 1 })
        } else {
            if (date[id] === 12) setDate({ ...date, [id]: 1 })
            else setDate({ ...date, [id]: date[id] + 1 })
        }
    }

    function down(e) {
        const { id } = e.target
        if (id === "year") {
            if (date[id] > year.start) setDate({ ...date, [id]: date[id] - 1 })
        } else {
            if (date[id] === 1) setDate({ ...date, [id]: 12 })
            else setDate({ ...date, [id]: date[id] - 1 })
        }
    }

    function cancel() {
        setDate({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 })
Kim, Subin's avatar
Kim, Subin committed
36
        setShow(false)
Kim, Subin's avatar
Monthly    
Kim, Subin committed
37
38
39
40
41
42
43
44
    }

    function handleClick() {
        if (date.year === year.start) {
            if (month > date.month) alert("선택하신 날짜는 유효하지 않습니다. 다시 선택해주세요.")
            else {
                let dateStr = date.year + "-" + date.month + "-01"
                setChangeDate(moment(dateStr).format("YYYY-MM-DD"))
Kim, Subin's avatar
Kim, Subin committed
45
                setShow(false)
Kim, Subin's avatar
Monthly    
Kim, Subin committed
46
47
48
49
50
51
            }
        } else if (date.year === year.end) {
            if (month < date.month) alert("선택하신 날짜는 유효하지 않습니다. 다시 선택해주세요.")
            else {
                let dateStr = date.year + "-" + date.month + "-01"
                setChangeDate(moment(dateStr).format("YYYY-MM-DD"))
Kim, Subin's avatar
Kim, Subin committed
52
                setShow(false)
Kim, Subin's avatar
Monthly    
Kim, Subin committed
53
54
55
56
            }
        } else {
            let dateStr = date.year + "-" + date.month + "-01"
            setChangeDate(moment(dateStr).format("YYYY-MM-DD"))
Kim, Subin's avatar
Kim, Subin committed
57
            setShow(false)
Kim, Subin's avatar
Monthly    
Kim, Subin committed
58
59
60
61
62
        }
    }

    return (
        <>
Kim, Subin's avatar
Kim, Subin committed
63
64
            {show ? <div className="offcanvas-backdrop fade show"></div> : null}
            <div className={"offcanvas offcanvas-bottom " + (show ? "visible show" : "invisiblel")} tabIndex="-1" id="datePicker" aria-labelledby="datePicker">
Kim, Subin's avatar
Kim, Subin committed
65
                <div className="offcanvas-body small user-select-none py-2">
Kim, Subin's avatar
Monthly    
Kim, Subin committed
66
67
                    <div className="d-flex my-3">
                        <div className="col-6 d-flex flex-column justify-content-between align-items-center fs-4">
Kim, Subin's avatar
Kim, Subin committed
68
                            <i className={`bi bi-caret-up-fill ${styles.cursor}`} id="year" onClick={up}></i>
Kim, Subin's avatar
Monthly    
Kim, Subin committed
69
                            {date.year}
Kim, Subin's avatar
Kim, Subin committed
70
                            <i className={`bi bi-caret-down-fill ${styles.cursor}`} id="year" onClick={down}></i>
Kim, Subin's avatar
Monthly    
Kim, Subin committed
71
72
                        </div>
                        <div className="col-6 d-flex flex-column justify-content-between align-items-center fs-4">
Kim, Subin's avatar
Kim, Subin committed
73
                            <i className={`bi bi-caret-up-fill ${styles.cursor}`} id="month" onClick={up}></i>
Kim, Subin's avatar
Monthly    
Kim, Subin committed
74
                            {date.month < 10 ? "0" + date.month : date.month}
Kim, Subin's avatar
Kim, Subin committed
75
                            <i className={`bi bi-caret-down-fill ${styles.cursor}`} id="month" onClick={down}></i>
Kim, Subin's avatar
Monthly    
Kim, Subin committed
76
77
                        </div>
                    </div>
Kim, Subin's avatar
Kim, Subin committed
78
                    <div className={`d-flex pt-2 border-top border-dark ${styles.cursor}`}>
Kim, Subin's avatar
Monthly    
Kim, Subin committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
                        <div className="col-6" data-bs-dismiss="offcanvas">
                            <p className="text-center fs-6 py-1 mb-0" onClick={cancel}>취소</p>
                        </div>
                        <div className="col-6" data-bs-dismiss="offcanvas" onClick={handleClick}>
                            <p className="text-center fs-6 py-1 mb-0">완료</p>
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}

export default DatePickerModal