ScheduleForm.js 5.34 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import { useState, useEffect } from "react";
2
import { Redirect, useLocation } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
import BtnGroup from "../Buttons/BtnGroup.js";
Kim, Subin's avatar
Kim, Subin committed
4
import scheduleApi from "../../apis/schedule.api.js";
Kim, Subin's avatar
Kim, Subin committed
5
import catchErrors from "../../utils/catchErrors.js";
Kim, Subin's avatar
Kim, Subin committed
6
7
8
import styles from "./form.module.scss";

const ScheduleForm = () => {
9
    const location = useLocation()
Kim, Subin's avatar
Kim, Subin committed
10
11
12
13
14
15
16
17
18
19
20
21
    const [schedule, setSchedule] = useState({
        title: "",
        startDate: "",
        endDate: "",
        startTime: "",
        endTime: "",
        allDay: "",
        location: "",
        memo: ""
    })
    const [disabled, setDisabled] = useState(true)
    const [success, setSuccess] = useState(false)
Kim, Subin's avatar
Kim, Subin committed
22
23
24
    const [error, setError] = useState("")

    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
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
        let isMounted = true;
        const checkInfo = { title: schedule.title, startDate: schedule.startDate, endDate: schedule.endDate }
        if (schedule.allDay !== "on") {
            checkInfo.startTime = schedule.startTime
            checkInfo.endTime = schedule.endTime
        } else {
            delete checkInfo.startTime
            delete checkInfo.endTime
        }
        if (isMounted) {
            const isSchedule = Object.values(checkInfo).every((el) => Boolean(el));
            isSchedule ? setDisabled(false) : setDisabled(true);
        }
        return () => {
            isMounted = false;
        };
    }, [schedule])

    function handleChange(e) {
        const { name, value } = e.target
        if (name === "allDay") {
            schedule.allDay !== "on" ? setSchedule({ ...schedule, [name]: value }) : setSchedule({ ...schedule, [name]: "off" })
        } else if (name === "startDate") {
            setSchedule({ ...schedule, [name]: value, endDate: value })
        } else if (name === "startTime") {
            setSchedule({ ...schedule, [name]: value, endTime: value })
        } else setSchedule({ ...schedule, [name]: value })
    }
Kim, Subin's avatar
Kim, Subin committed
53
54
55
56
57

    async function handleSubmit(e) {
        e.preventDefault()
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
58
            if (schedule.allDay === "on") setSchedule({ ...schedule, startTime: '00:00', endTime: '23:59' })
Kim, Subin's avatar
Kim, Subin committed
59
            await scheduleApi.submit()
Kim, Subin's avatar
Kim, Subin committed
60
            setSuccess(true)
Kim, Subin's avatar
Kim, Subin committed
61
62
63
        } catch (error) {
            catchErrors(error, setError)
        }
Kim, Subin's avatar
Kim, Subin committed
64
65
66
67
68
69
    }

    if (success) {
        alert('해당 일정이 성공적으로 등록되었습니다.')
        return <Redirect to="/home" />
    }
Kim, Subin's avatar
Kim, Subin committed
70
71

    return (
Kim, Subin's avatar
Kim, Subin committed
72
        <form className="pt-5" onSubmit={handleSubmit}>
Kim, Subin's avatar
Kim, Subin committed
73
            {console.log("data==",)}
74
            <div>
Kim, Subin's avatar
Kim, Subin committed
75
                <input className={`form-control form-control-lg shadow-none rounded-0 px-1 mb-5 ${styles.textInput}`} type="text" name="title" placeholder="제목" aria-label="title" onChange={handleChange} autoFocus />
76
            </div>
Kim, Subin's avatar
Kim, Subin committed
77
            <div className="d-flex mb-4">
Kim, Subin's avatar
Kim, Subin committed
78
                <label className="col col-form-label align-self-center py-0">시작</label>
Kim, Subin's avatar
Kim, Subin committed
79
80
                <div className={schedule.allDay === "on" ? "col-7" : "col-5"}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="date" name="startDate" aria-label="startDate" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
81
                </div>
Kim, Subin's avatar
Kim, Subin committed
82
83
                <div className={"col-5 " + (schedule.allDay === "on" ? "d-none" : "d-block")}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="time" name="startTime" aria-label="startTime" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
84
85
86
                </div>
            </div>
            <div className="d-flex mb-3">
Kim, Subin's avatar
Kim, Subin committed
87
                <label className="col col-form-label align-self-center py-0">종료</label>
Kim, Subin's avatar
Kim, Subin committed
88
89
                <div className={schedule.allDay === "on" ? "col-7" : "col-5"}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="date" name="endDate" value={schedule.endDate} aria-label="endDate" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
90
                </div>
Kim, Subin's avatar
Kim, Subin committed
91
92
                <div className={"col-5 " + (schedule.allDay === "on" ? "d-none" : "d-block")}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="time" name="endTime" value={schedule.endTime} aria-label="endTime" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
93
94
                </div>
            </div>
Kim, Subin's avatar
Kim, Subin committed
95
            <div className="d-flex justify-content-end form-check mb-4">
Kim, Subin's avatar
Kim, Subin committed
96
                <input className={`form-check-input shadow-none ${styles.checkBox}`} type="checkbox" id="allDay" name="allDay" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
97
98
99
100
101
                <label className="form-check-label ms-2" htmlFor="allDay">하루 종일</label>
            </div>
            <div className="d-flex justify-content-between align-items-center mb-4">
                <i className="col bi bi-geo-alt fs-3"></i>
                <div className="col-10">
Kim, Subin's avatar
Kim, Subin committed
102
                    <input className={`form-control shadow-none rounded-0 px-1 ${styles.textInput}`} type="text" name="location" placeholder="장소" aria-label="location" onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
103
104
105
106
107
                </div>
            </div>
            <div className="d-flex justify-content-between mb-5">
                <i className="col bi bi-journal-text fs-3"></i>
                <div className="col-10">
Kim, Subin's avatar
Kim, Subin committed
108
                    <textarea className={`form-control shadow-none ${styles.textArea}`} name="memo" rows="5" onChange={handleChange}></textarea>
Kim, Subin's avatar
Kim, Subin committed
109
                </div>
Kim, Subin's avatar
Kim, Subin committed
110
            </div>
Kim, Subin's avatar
Kim, Subin committed
111
            <BtnGroup disabled={disabled} />
Kim, Subin's avatar
Kim, Subin committed
112
113
114
115
116
        </form>
    )
}

export default ScheduleForm