ScheduleList.js 1.67 KB
Newer Older
Kim, Subin's avatar
context    
Kim, Subin committed
1
2
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
import Item from "./ScheduleItem";
Kim, Subin's avatar
context    
Kim, Subin committed
4
import scheduleApi from "../../apis/schedule.api";
Kim, Subin's avatar
Kim, Subin committed
5
6
import { useAuth } from "../../utils/context";
import catchErrors from "../../utils/catchErrors";
Kim, Subin's avatar
Kim, Subin committed
7
import moment from 'moment';
Kim, Subin's avatar
KU    
Kim, Subin committed
8
import styles from "./schedule.module.scss";
Kim, Subin's avatar
Kim, Subin committed
9
10

const ScheduleList = () => {
Kim, Subin's avatar
context    
Kim, Subin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    const [scheduleList, setScheduleList] = useState([])
    const [error, setError] = useState("")
    const { date } = useParams()
    const { user } = useAuth()

    useEffect(() => {
        getAll(date)
    }, [date])

    async function getAll(date) {
        try {
            setError("")
            const resList = await scheduleApi.getbyDate(date, user.id)
            setScheduleList(resList)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function delSchedule(id) {
        try {
            setError("")
            await scheduleApi.remove(id, user.id)
            alert("해당 일정을 성공적으로 삭제하였습니다.")
            getAll(date)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
41
    return (
Kim, Subin's avatar
Kim, Subin committed
42
        <div className={styles.list}>
Kim, Subin's avatar
Kim, Subin committed
43
            <div className="accordion accordion-flush" id="scheduleList">
Kim, Subin's avatar
context    
Kim, Subin committed
44
                {scheduleList.length !== 0 ?
Kim, Subin's avatar
Kim, Subin committed
45
46
                scheduleList.map((schedule, idx) => <Item key={idx} index={idx} curDate={moment(date).format("YY.MM.DD")} schedule={schedule} handleClick={delSchedule} />)
                : <div className="text-center text-secondary mt-2">오늘 등록된 일정이 없습니다.</div>}
Kim, Subin's avatar
Kim, Subin committed
47
            </div>
Kim, Subin's avatar
Kim, Subin committed
48
49
50
51
52
        </div>
    )
}

export default ScheduleList