PlanItem.js 2.96 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
5
import { Link, useHistory } from "react-router-dom";
import AlertModal from "../Modal/PlanAlertModal";
import planApi from "../../apis/plan.api";
import { useAuth } from "../../utils/context";
Kim, Subin's avatar
Kim, Subin committed
6
import catchErrors from "../../utils/catchErrors";
Choi Ga Young's avatar
Choi Ga Young committed
7
8
import styles from "../Schedule/schedule.module.scss";
import styles2 from "./studyplan.module.scss";
Kim, Subin's avatar
Kim, Subin committed
9
import moment from 'moment';
Choi Ga Young's avatar
Choi Ga Young committed
10

Kim, Subin's avatar
Kim, Subin committed
11
12
const PlanItem = ({ planList = [], subjectId }) => {
  const { user } = useAuth()
Choi Ga Young's avatar
Choi Ga Young 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

Kim, Subin's avatar
Kim, Subin committed
16
  async function delPlan(planId) {
Choi Ga Young's avatar
Choi Ga Young committed
17
18
    try {
      setError("")
Kim, Subin's avatar
Kim, Subin committed
19
20
21
      await planApi.remove(planId, user.id)
      alert("해당 계획을 성공적으로 삭제했습니다.")
      history.push(`/studyplan/${subjectId}`)
Choi Ga Young's avatar
Choi Ga Young committed
22
23
24
25
26
27
    } catch (error) {
      catchErrors(error, setError)
    }
  }

  return (
Kim, Subin's avatar
Kim, Subin committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    <>
      {planList.length !== 0 ? planList.map((plan, idx) => <div className="d-flex">
        <input className={`form-check-input rounded-0 shadow-none mt-1 ${styles2.checkBox}`} style={{ width: "5%" }} type="checkbox" checked={plan.checked} />
        <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>
                <button type="button" className="btn btn-crimson btn-sm ms-2" data-bs-toggle="modal" data-bs-target="#planmodal">삭제</button>
                <AlertModal planId={plan.id} handleClick={delPlan} />
Choi Ga Young's avatar
Choi Ga Young committed
51
              </div>
Choi Ga Young's avatar
Choi Ga Young committed
52
            </div>
Choi Ga Young's avatar
Choi Ga Young committed
53
54
          </div>
        </div>
Kim, Subin's avatar
Kim, Subin committed
55
56
57
      </div>)
        : <p className="text-center">등록된 학업 계획이 없습니다. </p>}
    </>
Choi Ga Young's avatar
Choi Ga Young committed
58
59
60
61
  )
}

export default PlanItem;