MovieTable.js 2.7 KB
Newer Older
1
2
3
import { useState } from "react";
import movieApi from "../../apis/movie.api.js";
import catchErrors from "../../utils/catchErrors.js";
Kim, Subin's avatar
Kim, Subin committed
4
5
import styles from "./movie-table.module.scss";

6
7
8
const MovieTable = ({ movieList }) => {
    const [error, setError] = useState("")

Kim, Subin's avatar
Kim, Subin committed
9
    async function handleSubmit(e, movieId) {
10
11
        e.preventDefault();
        try {
Kim, Subin's avatar
Kim, Subin committed
12
            setError("")
13
14
            await movieApi.submit(movieId)
            alert("서버 등록이 완료되었습니다.")
Kim, Subin's avatar
Kim, Subin committed
15
16
17
18
19
20
21
22
23
24
25
26
27
            window.location.reload()
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function handleDelete(e, movieId) {
        e.preventDefault()
        try {
            setError("")
            await movieApi.remove(movieId)
            alert("해당 영화 정보가 서버에서 삭제되었습니다.")
            window.location.reload()
28
        } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
29
            catchErrors(error, setError)
30
31
32
        }
    }

Kim, Subin's avatar
Kim, Subin committed
33
    return (
Jiwon Yoon's avatar
Jiwon Yoon committed
34
        <table style={{ color:"white" }} className={`table text-center align-middle ${styles.tableForm}`}>
Kim, Subin's avatar
Kim, Subin committed
35
36
            <thead className={`table-dark ${styles.dNone}`}>
                <tr>
37
38
39
                    <th className="col-md-5">제목</th>
                    <th className="col-md-4">감독</th>
                    <th className="col-md-3">상영일</th>
Kim, Subin's avatar
Kim, Subin committed
40
41
42
                </tr>
            </thead>
            <tbody>
43
44
45
46
                {movieList?.map(movie =>
                    <>
                        <tr className={styles.Row} data-bs-toggle="collapse" data-bs-target={"#movie" + movie.id}>
                            <td className="d-inline-block d-md-table-cell">{movie.title}</td>
47
48
                            <td className="d-none d-md-table-cell">{movie.director}</td>
                            <td className="d-none d-md-table-cell">{movie.release_date}</td>
49
50
                        </tr>
                        <tr className={styles.Row}>
51
52
                            <td colSpan="3" className="collapse" id={"movie" + movie.id}>
                                <div className={`d-inline-block d-md-none ${styles.word} mb-2`}>{movie.director} / {movie.release_date}</div>
53
                                <div className="d-flex justify-content-end">
Kim, Subin's avatar
Kim, Subin committed
54
                                    {movie.existed ? <button type="button" className="btn btn-danger" onClick={(e) => handleDelete(e, movie.id)}>삭제</button>
55
                                        : <button type="button" className="btn btn-primary" onClick={(e) => handleSubmit(e, movie.id)}>등록</button>}
56
57
58
59
                                </div>
                            </td>
                        </tr>
                    </>)}
Kim, Subin's avatar
Kim, Subin committed
60
61
62
63
64
65
            </tbody>
        </table>
    )
}

export default MovieTable