PlanItem.js 3.21 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import { useState } from "react";
Kim, Subin's avatar
Kim, Subin committed
2
3
4
import { Link, useHistory } from "react-router-dom";
import planApi from "../../apis/plan.api";
import { useAuth } from "../../utils/context";
Kim, Subin's avatar
Kim, Subin committed
5
import catchErrors from "../../utils/catchErrors";
Choi Ga Young's avatar
Choi Ga Young committed
6
7
import styles from "../Schedule/schedule.module.scss";
import styles2 from "./studyplan.module.scss";
Kim, Subin's avatar
Kim, Subin committed
8
import moment from 'moment';
Choi Ga Young's avatar
Choi Ga Young committed
9

Choi Ga Young's avatar
Choi Ga Young committed
10
const PlanItem = ({ planList = [], subjectId, getPlanList }) => {
Kim, Subin's avatar
Kim, Subin committed
11
  const { user } = useAuth()
Choi Ga Young's avatar
Choi Ga Young committed
12
  const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
13
  const history = useHistory()
Choi Ga Young's avatar
Choi Ga Young committed
14

Kim, Subin's avatar
Kim, Subin committed
15
  async function delPlan(planId) {
Choi Ga Young's avatar
Choi Ga Young committed
16
17
    try {
      setError("")
Kim, Subin's avatar
Kim, Subin committed
18
19
      await planApi.remove(planId, user.id)
      alert("해당 계획을 성공적으로 삭제했습니다.")
Choi Ga Young's avatar
Choi Ga Young committed
20
      history.push("/studyplan")
Choi Ga Young's avatar
Choi Ga Young committed
21
22
23
24
    } catch (error) {
      catchErrors(error, setError)
    }
  }
Choi Ga Young's avatar
Choi Ga Young committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  async function checkFn(e, planId, planCk) {
    try {
      setError("")
      e.preventDefault();
      const result = await planApi.saveCheck(planId, planCk)
      console.log('check save result', result)
      if (result === "success") {
        getPlanList()
      }
    } catch (error) {
      catchErrors(error, setError)
    }
  }

Choi Ga Young's avatar
Choi Ga Young committed
39
40

  return (
Kim, Subin's avatar
Kim, Subin committed
41
42
    <>
      {planList.length !== 0 ? planList.map((plan, idx) => <div className="d-flex">
Choi Ga Young's avatar
Choi Ga Young committed
43
        <input className={`form-check-input rounded-0 shadow-none mt-1 ${styles2.checkBox}`} style={{ width: "5%" }} type="checkbox" checked={plan.checked} onClick={(e) => checkFn(e, plan.id, plan.checked)} />
Kim, Subin's avatar
Kim, Subin committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        <div className="accordion-item border-0 col" style={{ width: "95%" }}>
          <button className={`d-flex flex-column align-items-start accordion-button collapsed bg-white shadow-none px-0 pt-0 ps-3 ${styles.activeBtn}`} type="button" data-bs-toggle="collapse" data-bs-target={"#plan" + idx} aria-expanded="false" aria-controls={"plan" + idx}>
            <h5 className={`accordion-header ${styles.title}`} id={"plan-heading" + idx}>{plan.title}</h5>
            <p className={`text-secondary mb-0 ${styles.time}`}>
              ~ {plan.timeChecked ? moment(plan.deadline).format("YY.MM.DD HH:mm") : moment(plan.deadline).format("YY.MM.DD")}
            </p>
          </button>
          <div id={"plan" + idx} className="accordion-collapse collapse" aria-labelledby={"plan-heading" + idx} data-bs-parent="S#addplanlist">
            <div className={`accordion-body px-0 pt-2 pb-0 mb-3 ${styles.textBox}`}>
              <div className="d-flex align-items-start fw-bold">
                <i className="bi bi-clock-history fs-5"></i>
                <div className="col-11 ms-2 align-self-center">
                  ~ {plan.timeChecked ? moment(plan.deadline).format("YY.MM.DD HH:mm") : moment(plan.deadline).format("YY.MM.DD")}
                </div>
              </div>
              {plan.memo}
              <div className="d-flex justify-content-end mt-3">
                <Link className="btn btn-light btn-sm border-dark" to={`/studyplan/edit/${plan.id}`}>수정</Link>
Kim, Subin's avatar
Kim, Subin committed
62
                <button type="button" className="btn btn-crimson btn-sm ms-2" onClick={() => delPlan(plan.id)}>삭제</button>
Choi Ga Young's avatar
Choi Ga Young committed
63
              </div>
Choi Ga Young's avatar
Choi Ga Young committed
64
            </div>
Choi Ga Young's avatar
Choi Ga Young committed
65
66
          </div>
        </div>
Kim, Subin's avatar
Kim, Subin committed
67
68
69
      </div>)
        : <p className="text-center">등록된 학업 계획이 없습니다. </p>}
    </>
Choi Ga Young's avatar
Choi Ga Young committed
70
71
72
73
  )
}

export default PlanItem;