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

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

21
22
23
24
25

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

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

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

  }
44

45
46
47
48
49
50
51
52
53
  async function getInfo(id) {
    const result = await subjectApi.getSubInfo(id)
    console.log('과목수정 result확인', result)
    setSubject({
      lectureName: result.name,
      prof: result.prof,
      classRoom: result.room
    })
  }
54
55
56
57

  async function handleSubmit(e) {
    e.preventDefault();
    try {
58
      setError("")
59
60
      if (subjectId) {
        //수정함수 실행
61
62
63
        await subjectApi.editSubject(subject, subjectId)
        alert("과목정보가 수정되었습니다.")
        setSuccess(true)
64
      } else {
65
66
        //등록함수 실행
        await subjectApi.addsubject(subject, user.id)
67
68
69
      }
    } catch (error) {
      catchErrors(error, setError)
70
71
72
73
74
      setSubject({
        lectureName: "",
        prof: "",
        classRoom: ""
      })
75
76
77
78
    }

  }

79
80
81
82
83
  
  if (success) {
    return <Redirect to="/studyplan" />
  }

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

export default SubjectForm;