SubjectForm.js 3.2 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
63
        //등록함수 실행
        await subjectApi.addsubject(subject, user.id)
64
65
66
      }
    } catch (error) {
      catchErrors(error, setError)
67
68
69
70
71
      setSubject({
        lectureName: "",
        prof: "",
        classRoom: ""
      })
72
73
74
    }

  }
75
76
77
78
79
  
  if (success) {
    return <Redirect to="/studyplan" />
  }

80
81
82
83
84
  return (
      <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>
85
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.lectureName} name="lectureName" onChange={handleChange} />
86
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
87
          <div className="mb-5 pt-2 d-flex flex-row">
88
            <label className="form-label fs-4" style={{ width: "100px" }}>교수명</label>
89
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.prof} name="prof" onChange={handleChange} />
90
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
91
          <div className="mb-5 pt-2 d-flex flex-row">
Choi Ga Young's avatar
Choi Ga Young committed
92
            <label className="form-label fs-4 " style={{ width: "100px", letterSpacing: "15px" }}>장소</label>
93
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} value={subject.classRoom} name="classRoom" onChange={handleChange} />
94
95
          </div>
        </div>
Choi Ga Young's avatar
Choi Ga Young committed
96
        <div className="pt-2">
Kim, Subin's avatar
Kim, Subin committed
97
          <BtnGroup text={subjectId} disabled={disabled} handleSubmit={handleSubmit} />
Choi Ga Young's avatar
Choi Ga Young committed
98
        </div>
99
100
101
102
103
      </div>
  )
}

export default SubjectForm;