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

const SubjectForm = () => {
10
  const { user } = useAuth();
11
  const { subjectId } = useParams();
12
  const [success, setSuccess] = useState(false)
13
  const [error, setError] = useState("");
Kim, Subin's avatar
Kim, Subin committed
14
  const [disabled, setDisabled] = useState(true)
15
16
17
18
19
  const [subject, setSubject] = useState({
    lectureName: "",
    prof: "",
    classRoom: ""
  })
20
21
22
23
24

  useEffect(() => {
    getInfo(subjectId);
  }, [])

25
26
  useEffect(() => {
    let isMounted = true;
Choi Ga Young's avatar
Choi Ga Young committed
27
    const checkInfo = { lectureName: subject.lectureName }
28
29
30
31
32
33
34
35
36
37
38
39
40
41

    if (isMounted) {
      const isSubject = Object.values(checkInfo).every((el) => Boolean(el));
      isSubject ? setDisabled(false) : setDisabled(true);
    }
    return () => {
      isMounted = false;
    }
  }, [subject])

  function handleChange(e) {
    const { name, value } = e.target
    setSubject({ ...subject, [name]: value })
  }
42

43
44
45
46
47
48
49
50
  async function getInfo(id) {
    const result = await subjectApi.getSubInfo(id)
    setSubject({
      lectureName: result.name,
      prof: result.prof,
      classRoom: result.room
    })
  }
51

Choi Ga Young's avatar
Choi Ga Young committed
52
  async function handleSubmit(e) {
53
54
    e.preventDefault();
    try {
55
      setError("")
56
57
      if (subjectId) {
        //수정함수 실행
58
59
60
        await subjectApi.editSubject(subject, subjectId)
        alert("과목정보가 수정되었습니다.")
        setSuccess(true)
61
      } else {
62
        //등록함수 실행
Choi Ga Young's avatar
Choi Ga Young committed
63
64
65
66
67
68
69
70
71
        const result = await subjectApi.addsubject(subject, user.id)
        if (result) {
          alert("등록되었습니다")
          setSubject({
            lectureName: "",
            prof: "",
            classRoom: ""
          })
        } else alert("등록에 실패하였습니다.")
72
73
74
      }
    } catch (error) {
      catchErrors(error, setError)
75
76
77
78
79
      setSubject({
        lectureName: "",
        prof: "",
        classRoom: ""
      })
80
81
82
    }

  }
Choi Ga Young's avatar
Choi Ga Young committed
83

84
85
86
87
  if (success) {
    return <Redirect to="/studyplan" />
  }

88
  return (
Choi Ga Young's avatar
Choi Ga Young committed
89
90
91
92
93
94
95
96
97
    <div className="position-absolute top-50 start-50 translate-middle" style={{ width: "80%" }}>
      <div>
        <div className="mb-5 d-flex flex-row">
          <label className="form-label fs-4" style={{ width: "100px" }}>강의명</label>
          <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.lectureName} name="lectureName" onChange={handleChange} />
        </div>
        <div className="mb-5 pt-2 d-flex flex-row">
          <label className="form-label fs-4" style={{ width: "100px" }}>교수명</label>
          <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.prof} name="prof" onChange={handleChange} />
98
        </div>
Choi Ga Young's avatar
Choi Ga Young committed
99
100
101
        <div className="mb-5 pt-2 d-flex flex-row">
          <label className="form-label fs-4 " style={{ width: "100px", letterSpacing: "15px" }}>장소</label>
          <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.classRoom} name="classRoom" onChange={handleChange} />
Choi Ga Young's avatar
Choi Ga Young committed
102
        </div>
103
      </div>
Choi Ga Young's avatar
Choi Ga Young committed
104
105
106
107
      <div className="pt-2">
        <BtnGroup text={subjectId} disabled={disabled} handleSubmit={handleSubmit} />
      </div>
    </div>
108
109
110
111
  )
}

export default SubjectForm;