MovieTable.js 3.6 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
34
35
36
37
38
    return (
        <table className={`table text-center ${styles.tableForm}`}>
            <thead className={`table-dark ${styles.dNone}`}>
                <tr>
                    <th>제목</th>
                    <th>감독</th>
39
                    <th>상영일</th>
Kim, Subin's avatar
Kim, Subin committed
40
41
42
43
44
45
46
                    <th>줄거리</th>
                    <th>포스터</th>
                    <th>스틸컷</th>
                    <th>예고편</th>
                </tr>
            </thead>
            <tbody>
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
                {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>
                            <td data-label="- " className={`d-inline-block d-md-table-cell ${styles.data}`}>케이트 쇼트랜드</td>
                            <td data-label="/ " className={`d-inline-block d-md-table-cell ${styles.data}`}>{movie.release_date}</td>
                            <td className="d-none d-md-table-cell">{movie.overview !== '' ? 'O' : 'X'}</td>
                            <td className="d-none d-md-table-cell">{movie.poster_path !== '' ? 'O' : 'X'}</td>
                            <td className="d-none d-md-table-cell">{movie.backdrop_path !== '' ? 'O' : 'X'}</td>
                            <td className="d-none d-md-table-cell">{movie.video !== false ? 'O' : 'X'}</td>
                        </tr>
                        <tr className={styles.Row}>
                            <td colSpan="7" className="collapse" id={"movie" + movie.id}>
                                <div className={`d-inline-block d-md-none ${styles.word} mb-2`}>
                                    줄거리 - {movie.overview !== '' ? 'O' : 'X'} /
                                    포스터 - {movie.poster_path !== '' ? 'O' : 'X'} /
                                    스틸컷 - {movie.backdrop_path !== '' ? 'O' : 'X'} /
                                    예고편 - {movie.video !== false ? 'O' : 'X'}
                                </div>
                                <div className="d-flex justify-content-end">
Kim, Subin's avatar
Kim, Subin committed
67
68
                                    {movie.existed ? <button type="button" className="btn btn-danger" onClick={(e) => handleDelete(e, movie.id)}>삭제</button>
                                    : <button type="button" className="btn btn-primary" onClick={(e) => handleSubmit(e, movie.id)}>등록</button>}
69
70
71
72
                                </div>
                            </td>
                        </tr>
                    </>)}
Kim, Subin's avatar
Kim, Subin committed
73
74
75
76
77
78
            </tbody>
        </table>
    )
}

export default MovieTable