StudyPlanList.js 2.21 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
import StudyPlanCard from "./StudyPlanCard";
import subjectApi from '../../apis/subject.api';
Choi Ga Young's avatar
Choi Ga Young committed
5
import planApi from '../../apis/plan.api';
Kim, Subin's avatar
Kim, Subin committed
6
7
8
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
9
10

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

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

Kim, Subin's avatar
Kim, Subin committed
21
22
23
24
25
26
27
28
  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
29
  }
Kim, Subin's avatar
Kim, Subin committed
30

Choi Ga Young's avatar
Choi Ga Young committed
31
32
33
34
35
36
37
38
39
40
41
42
43
  async function checkFn(e, planId, planCk) {
    try {
      setError("")
      e.preventDefault();
      const result = await planApi.saveCheck(planId, planCk)
      if (result === "success") {
        getList(user.id)
      }
    } catch (error) {
      catchErrors(error, setError)
    }
  }

Kim, Subin's avatar
Kim, Subin committed
44
45
  async function delSubject(e, subjectId) {
    e.preventDefault()
Kim, Subin's avatar
Kim, Subin committed
46
47
48
49
50
51
52
53
54
55
56
57
    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
58
  return (
Choi Ga Young's avatar
Choi Ga Young committed
59
    <div className={`mt-4 ${styles.list}`}>
Kim, Subin's avatar
Kim, Subin committed
60
      <div className="d-flex flex-column align-items-center">
Choi Ga Young's avatar
Choi Ga Young committed
61
        {renList.length !== 0 ? renList.map((info, idx) => <StudyPlanCard key={idx} renList={info} handleClick={delSubject} handleEdit={checkFn} />) : null}
Kim, Subin's avatar
Kim, Subin committed
62
63
        <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
64
65
            <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
66
67
68
          </div>
        </Link>
      </div>
Choi Ga Young's avatar
Choi Ga Young committed
69
    </div>
Choi Ga Young's avatar
Choi Ga Young committed
70
71
72
73
  )
}

export default StudyPlanList;