StudyPlanList.js 1.85 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import { useState, useEffect } from 'react';
Kim, Subin's avatar
Kim, Subin committed
2
import { Link, useHistory } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
4
5
6
7
import StudyPlanCard from "./StudyPlanCard";
import subjectApi from '../../apis/subject.api';
import catchErrors from "../../utils/catchErrors";
import { useAuth } from "../../utils/context";
import styles from "./studyplan.module.scss";
Choi Ga Young's avatar
Choi Ga Young committed
8
9

const StudyPlanList = () => {
Choi Ga Young's avatar
Choi Ga Young committed
10
  const { user } = useAuth();
Kim, Subin's avatar
Kim, Subin committed
11
  const [renList, setRenList] = useState([])
Kim, Subin's avatar
Kim, Subin committed
12
  const [success, setSuccess] = useState(false)
Kim, Subin's avatar
Kim, Subin committed
13
  const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
14
  const history = useHistory()
Choi Ga Young's avatar
Choi Ga Young committed
15

Choi Ga Young's avatar
Choi Ga Young committed
16
17
18
19
  useEffect(() => {
    getList(user.id);
  }, [])

Kim, Subin's avatar
Kim, Subin committed
20
21
22
23
24
25
26
27
  async function getList(userId) {
    try {
      setError("")
      const result = await subjectApi.allSubject(userId)
      setRenList(result)
    } catch (error) {
      catchErrors(error, setError)
    }
Choi Ga Young's avatar
Choi Ga Young committed
28
  }
Kim, Subin's avatar
Kim, Subin committed
29

Kim, Subin's avatar
Kim, Subin committed
30
31
  async function delSubject(e, subjectId) {
    e.preventDefault()
Kim, Subin's avatar
Kim, Subin committed
32
33
34
35
36
37
38
39
40
41
42
43
    try {
      setError("")
      await subjectApi.removeSubject(subjectId, user.id)
      alert("해당 과목 정보가 성공적으로 삭제되었습니다.")
      setSuccess(true)
    } catch (error) {
      catchErrors(error, setError)
    }
  }

  if (success) history.push("/home")

Choi Ga Young's avatar
Choi Ga Young committed
44
  return (
Choi Ga Young's avatar
Choi Ga Young committed
45
    <div className={`mt-4 ${styles.list}`}>
Kim, Subin's avatar
Kim, Subin committed
46
      <div className="d-flex flex-column align-items-center">
Kim, Subin's avatar
Kim, Subin committed
47
        {renList.length !== 0 ? renList.map((info, idx) => <StudyPlanCard key={idx} renList={info} handleClick={delSubject} />) : null}
Kim, Subin's avatar
Kim, Subin committed
48
49
        <Link className="card text-decoration-none link-dark" to="/subject/edit" style={{ width: "20rem" }}>
          <div className="card-body d-flex flex-column bg-secondary bg-opacity-25">
Kim, Subin's avatar
Kim, Subin committed
50
51
            <i className="bi bi-plus-lg d-flex justify-content-center fs-3"></i>
            <p className="card-text mt-2 text-center">새로운 과목 추가하기</p>
Choi Ga Young's avatar
Choi Ga Young committed
52
53
54
          </div>
        </Link>
      </div>
Choi Ga Young's avatar
Choi Ga Young committed
55
    </div>
Choi Ga Young's avatar
Choi Ga Young committed
56
57
58
59
  )
}

export default StudyPlanList;