Video.js 1.1 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import { useEffect, useState } from 'react'
import movieApi from '../apis/movie.api.js'
Kim, Subin's avatar
Kim, Subin committed
3
4
5
import catchErrors from "../utils/catchErrors.js"

const Video = ({ movieId }) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
6
    const [videoUrls, setVideoUrls] = useState([])
Kim, Subin's avatar
Kim, Subin committed
7
8
    const [error, setError] = useState("")

Jiwon Yoon's avatar
Jiwon Yoon committed
9
10
11
12
13
14
    useEffect(() => {
        getVideos()
    }, [])

    async function getVideos() {
        try {
Kim, Subin's avatar
Kim, Subin committed
15
            const data = await movieApi.getVideosfromTM(movieId)
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
            setVideoUrls(data)
        } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
18
            catchErrors(error, setError)
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
        }
    }
Kim, Subin's avatar
Kim, Subin committed
21

Jiwon Yoon's avatar
Jiwon Yoon committed
22
23
24
25
    return (
        <div>
            {videoUrls.length > 0
                ? videoUrls.map(el => (
Jiwon Yoon's avatar
Jiwon Yoon committed
26
                    <div className="mt-5 pb-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
                        <p>{el.name}</p>
                        <div class="ratio ratio-16x9">
Jiwon Yoon's avatar
Jiwon Yoon committed
29
                            <iframe src={`https://www.youtube.com/embed/${el.key}`} title="YouTube video" allowFullScreen></iframe>
Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
33
34
35
36
37
38
                        </div>
                    </div>
                ))
                : <div>예고편 정보가 존재하지 않습니다.</div>}
        </div>
    )
}

export default Video