StudyPlanEditForm.js 7.33 KB
Newer Older
1
import { useState, useEffect } from 'react';
Kim, Subin's avatar
Kim, Subin committed
2
import { useParams, Redirect } from 'react-router-dom';
Kim, Subin's avatar
Kim, Subin committed
3
import { useAuth } from "../../utils/context";
4
import BtnGroup from "../Buttons/BtnGroup";
Kim, Subin's avatar
Kim, Subin committed
5
6
7
import subjectApi from '../../apis/subject.api';
import planApi from '../../apis/plan.api';
import catchErrors from '../../utils/catchErrors';
8
9
10
import styles from "./form.module.scss";

const StudyPlanEditForm = () => {
Choi Ga Young's avatar
Choi Ga Young committed
11
12
13
14
15
    const { user } = useAuth();
    const params = useParams();
    const [disabled, setDisabled] = useState(true)
    const [selected, setSelected] = useState("");
    const [error, setError] = useState("");
Kim, Subin's avatar
Kim, Subin committed
16
    const [success, setSuccess] = useState(false)
Kim, Subin's avatar
plan    
Kim, Subin committed
17
    const [subjectName, setSubjectName] = useState("");
18
19
20
    const [studyplan, setStudyplan] = useState({
        studyplanTitle: "",
        endDate: "",
Choi Ga Young's avatar
Choi Ga Young committed
21
22
23
        memo: "",
        deadline: "",
        selected: ""
24
    })
Kim, Subin's avatar
Kim, Subin committed
25
    const [subjectList, setList] = useState([])
26
27
28

    useEffect(() => {
        let isMounted = true;
Kim, Subin's avatar
plan    
Kim, Subin committed
29
        const checkInfo = { studyplanTitle: studyplan.studyplanTitle, endDate: studyplan.endDate, selected: studyplan.selected }
Choi Ga Young's avatar
Choi Ga Young committed
30
        if (studyplan.deadline === "on") {
31
32
33
34
35
36
37
38
39
40
41
42
43
            checkInfo.endTime = studyplan.endTime
        } else {
            delete checkInfo.endTime
        }
        if (isMounted) {
            const isStudyPlan = Object.values(checkInfo).every((el) => Boolean(el));
            isStudyPlan ? setDisabled(false) : setDisabled(true);
        }
        return () => {
            isMounted = false;
        }
    }, [studyplan])

Choi Ga Young's avatar
Choi Ga Young committed
44
    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
45
        getSubject(user.id)
Kim, Subin's avatar
plan    
Kim, Subin committed
46
47
48
49
50
51
52
        console.log("useEffect params 확인", params)
        if (params.subjectId) setStudyplan({...studyplan, selected: params.subjectId })
        else if (params.planId) getInfo(params.planId);
        // if (params.hasOwnProperty('planId')) {
        //     console.log('planId params확인');
        //     getInfo(params.planId);
        // }
Choi Ga Young's avatar
Choi Ga Young committed
53
54
    }, [])

Kim, Subin's avatar
plan    
Kim, Subin committed
55
    async function getSubject(id) {
Kim, Subin's avatar
Kim, Subin committed
56
57
        try {
            setError("")
Kim, Subin's avatar
plan    
Kim, Subin committed
58
59
            const result = await subjectApi.subjectTitle(id)
            setList(result)
Kim, Subin's avatar
Kim, Subin committed
60
61
        } catch (error) {
            catchErrors(error, setError)
Choi Ga Young's avatar
Choi Ga Young committed
62
        }
Choi Ga Young's avatar
Choi Ga Young committed
63
64
    }

Kim, Subin's avatar
plan    
Kim, Subin committed
65
    async function getInfo(planId) {
Kim, Subin's avatar
Kim, Subin committed
66
67
        try {
            setError("")
Kim, Subin's avatar
plan    
Kim, Subin committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
            const result = await planApi.getDetail(planId)
            console.log('수정 getInfo result', result)
            setStudyplan({ ...studyplan, ...result })
            // setSubjectName(result.subjectName)
            // if (result.endTime) {
            //     setStudyplan({
            //         studyplanTitle: result.title,
            //         endDate: result.endDate,
            //         endTime: result.endTime,
            //         memo: result.memo,
            //         deadline: result.deadline,
            //         selected: result.subjectId
            //     })
            //     setSubjectName(result.subjectName)
            // } else {
            //     setStudyplan({
            //         studyplanTitle: result.title,
            //         endDate: result.endDate,
            //         memo: result.memo,
            //         deadline: result.deadline,
            //         selected: result.subjectId
            //     })
            //     setSubjectName(result.subjectName)
            // }
Kim, Subin's avatar
Kim, Subin committed
92
93
94
        } catch (error) {
            catchErrors(error, setError)
        }
Choi Ga Young's avatar
Choi Ga Young committed
95
96
    }

97
98
99
100
101
102
103
104
105
    function handleChange(e) {
        const { name, value } = e.target
        if (name === "deadline") {
            studyplan.deadline !== "on" ? setStudyplan({ ...studyplan, [name]: value }) : setStudyplan({ ...studyplan, [name]: "off" })
        } else {
            setStudyplan({ ...studyplan, [name]: value })
        }
    }

Choi Ga Young's avatar
Choi Ga Young committed
106
107
108
109
    async function handleSubmit(e) {
        e.preventDefault();
        try {
            setError("")
Kim, Subin's avatar
plan    
Kim, Subin committed
110
            if (params.subjectId) {
Choi Ga Young's avatar
Choi Ga Young committed
111
                //등록함수 실행
Kim, Subin's avatar
plan    
Kim, Subin committed
112
                await planApi.submit(studyplan, params.subjectId)
Kim, Subin's avatar
Kim, Subin committed
113
                alert("해당 학업계획이 성공적으로 등록되었습니다.")
Choi Ga Young's avatar
Choi Ga Young committed
114
115
            } else {
                //수정함수 실행
Kim, Subin's avatar
plan    
Kim, Subin committed
116
                await planApi.edit(studyplan, params.planId)
Kim, Subin's avatar
Kim, Subin committed
117
                alert("해당 학업계획이 성공적으로 수정되었습니다.")
Choi Ga Young's avatar
Choi Ga Young committed
118
            }
Kim, Subin's avatar
Kim, Subin committed
119
            setSuccess(true)
Choi Ga Young's avatar
Choi Ga Young committed
120
121
122
123
124
125
        } catch (error) {
            catchErrors(error, setError)
            setStudyplan({
                studyplanTitle: "",
                endDate: "",
                deadline: "",
Kim, Subin's avatar
plan    
Kim, Subin committed
126
127
                memo: "",
                selected: ""
Choi Ga Young's avatar
Choi Ga Young committed
128
129
130
131
            })
        }
    }

Kim, Subin's avatar
Kim, Subin committed
132
133
134
135
    if (success) {
        return <Redirect to="/studyplan" />
    }

136
    return (
Kim, Subin's avatar
Kim, Subin committed
137
        <div className="pt-5">
Kim, Subin's avatar
plan    
Kim, Subin committed
138
139
140
141
142
143
144
145
146
147
            {console.log("studyplan---",studyplan)}
            <select className={`form-select mb-4 ${styles.selectInput}`} name="selected" aria-label="Choose subject" onChange={handleChange}>
                {subjectList.length !== 0 ? subjectList.map((subject, idx) => {
                    if (idx === 0) return <>
                        <option selected={studyplan.selected === "" ? true : false}>관련 과목을 선택해주세요.</option>
                        <option value={subject.id} selected={studyplan.selected === subject.id ? true : false}>{subject.name}</option>
                    </>
                    else return <option value={subject.id} selected={studyplan.selected === subject.id ? true : false}>{subject.name}</option>
                })
                : <option selected>새로운 과목을 만들어주세요.</option>}
Kim, Subin's avatar
Kim, Subin committed
148
149
150
            </select>
            <input type="text" name="studyplanTitle"
                className={`form-control shadow-none rounded-0 mb-5 ${styles.textInput}`}
Choi Ga Young's avatar
Choi Ga Young committed
151
                placeholder="제목" value={studyplan.studyplanTitle} onChange={handleChange} />
Kim, Subin's avatar
Kim, Subin committed
152
153
            <div className="d-flex mb-3">
                <label className="col col-form-label align-self-center py-0">마감일</label>
Choi Ga Young's avatar
Choi Ga Young committed
154
155
                <div className={studyplan.deadline === "on" ? "col-5" : "col-7"}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="date" name="endDate" aria-label="endDate" value={studyplan.endDate} onChange={handleChange} />
156
                </div>
Choi Ga Young's avatar
Choi Ga Young committed
157
158
                <div className={"col-4 " + (studyplan.deadline === "on" ? "d-block" : "d-none")}>
                    <input className={`form-control shadow-none ${styles.dateInput}`} type="time" name="endTime" aria-label="endTime" value={studyplan.endTime} onChange={handleChange} />
159
                </div>
Kim, Subin's avatar
Kim, Subin committed
160
161
            </div>
            <div className="d-flex justify-content-end form-check mb-4">
Choi Ga Young's avatar
Choi Ga Young committed
162
                <input className={`form-check-input shadow-none ${styles.checkBox} me-2`} type="checkbox" id="deadline" name="deadline" onChange={handleChange} checked={studyplan.deadline === "on" ? true : false} />
Kim, Subin's avatar
Kim, Subin committed
163
164
165
166
167
                <label className="form-check-label" htmlFor="deadline">시간 </label>
            </div>
            <div className="d-flex justify-content-between mb-5">
                <i className="col bi bi-journal-text fs-3"></i>
                <div className="col-10">
Choi Ga Young's avatar
Choi Ga Young committed
168
                    <textarea className={`form-control shadow-none ${styles.textArea}`} name="memo" rows="5" value={studyplan.memo} onChange={handleChange}></textarea>
169
170
                </div>
            </div>
Choi Ga Young's avatar
Choi Ga Young committed
171
            <BtnGroup disabled={disabled} handleSubmit={handleSubmit} />
Kim, Subin's avatar
Kim, Subin committed
172
        </div>
173
174
175
176
    )
}

export default StudyPlanEditForm