SubjectForm.js 2.41 KB
Newer Older
1
2
3
import { useState, useEffect } from 'react';
import BtnGroup from "../Buttons/BtnGroup";
import styles from "./form.module.scss";
4
5
6
import { useParams } from 'react-router-dom';
import subjectApi from '../../apis/subject.api';
import catchErrors from '../../utils/catchErrors.js';
7
8

const SubjectForm = () => {
9
10
  const { subjectId } = useParams();
  const [error, setError] = useState("");
11
12
13
14
15
16
17
18
19
  const [subject, setSubject] = useState({
    lectureName: "",
    prof: "",
    classRoom: ""
  })
  const [disabled, setDisabled] = useState(true)

  useEffect(() => {
    let isMounted = true;
Choi Ga Young's avatar
Choi Ga Young committed
20
    const checkInfo = { lectureName: subject.lectureName }
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

    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 })

  }
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


  async function handleSubmit(e) {
    e.preventDefault();
    try {
      if (subjectId) {
        //수정함수 실행
      } else {
        await subjectApi.addsubject(subject)
      }
    } catch (error) {
      catchErrors(error, setError)
    }

  }

52
53
54
55
56
57
  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>
Choi Ga Young's avatar
Choi Ga Young committed
58
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="lectureName" onChange={handleChange} />
59
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
60
          <div className="mb-5 pt-2 d-flex flex-row">
61
            <label className="form-label fs-4" style={{ width: "100px" }}>교수명</label>
Choi Ga Young's avatar
Choi Ga Young committed
62
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="prof" onChange={handleChange} />
63
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
64
          <div className="mb-5 pt-2 d-flex flex-row">
Choi Ga Young's avatar
Choi Ga Young committed
65
66
            <label className="form-label fs-4 " style={{ width: "100px", letterSpacing: "15px" }}>장소</label>
            <input className={`form-control shadow-none rounded-0 ${styles.textInput}`} name="classRoom" onChange={handleChange} />
67
68
          </div>
        </div>
Choi Ga Young's avatar
Choi Ga Young committed
69
        <div className="pt-2">
70
          <BtnGroup disabled={disabled} handleSubmit={handleSubmit} />
Choi Ga Young's avatar
Choi Ga Young committed
71
        </div>
72
73
74
75
76
77
      </div>
    </>
  )
}

export default SubjectForm;