ScheduleList.js 1.75 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
KU    
Kim, Subin committed
7
import styles from "./schedule.module.scss";
Kim, Subin's avatar
Kim, Subin committed
8
9

const ScheduleList = () => {
Kim, Subin's avatar
context    
Kim, Subin committed
10
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
41
42
43
44
45
46
47
48
49
    const [scheduleList, setScheduleList] = useState([])
    const [error, setError] = useState("")
    const { date } = useParams()
    const { user } = useAuth()

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

    useEffect(() => {
        getAll(date)
        return () => {
            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
50
    return (
Kim, Subin's avatar
Kim, Subin committed
51
        <div className={styles.list}>
Kim, Subin's avatar
Kim, Subin committed
52
            <div className="accordion accordion-flush" id="scheduleList">
Kim, Subin's avatar
context    
Kim, Subin committed
53
54
55
                {scheduleList.length !== 0 ?
                scheduleList.map((schedule, idx) => <Item key={idx} schedule={schedule} handleClick={delSchedule} />)
                : <div className="text-center text-secondary">오늘 등록된 일정이 없습니다.</div>}
Kim, Subin's avatar
Kim, Subin committed
56
            </div>
Kim, Subin's avatar
Kim, Subin committed
57
58
59
60
61
        </div>
    )
}

export default ScheduleList