File size: 3,606 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { useState, useEffect, useCallback } from 'react'
import './SearchContent.css'

import axios from 'baseAxios'
import VideoCard from 'components/Video/VideoCard/VideoCard'
import { debounce } from 'lodash'
import { buildVideoMetadata, buildVideoModal } from 'utils/transformations'
import { sortVideosByPopularity } from 'utils/sorting'
import useVideoInfoHandlers from 'hooks/useVideoInfoHandlers'
import ErrorPage from 'components/StaticPages/ErrorPage/ErrorPage'

const SearchContent = props => {
    const [searchedVideoList, setSearchedVideoList] = useState([])
    const [searchedError, setSearchedError] = useState(null)
    const [loading, setLoading] = useState(true)
    const { searchParam } = props
    const [
        videoInfo, videoInfoError, detailModal, cardClickHandler,
        cardHoverHandler, closeModalHandler
    ] = useVideoInfoHandlers()

    const getSearchMovies = async (searchItem) => {
        const movieUrl = `search/movie?api_key=${process.env.REACT_APP_MOVIEDB_API_KEY}&language=en-US&query=${searchItem}&page=1&include_adult=false`
        const tvUrl = `search/tv?api_key=${process.env.REACT_APP_MOVIEDB_API_KEY}&language=en-US&page=1&query=${searchItem}&include_adult=false`

        try {
            const responses = await Promise.all(
                [
                    axios.get(movieUrl).then(response => response.data.results),
                    axios.get(tvUrl).then(response => response.data.results)
                ]
            )

            setSearchedVideoList([...responses[0], ...responses[1]])
            setLoading(false)
        } catch (error) {
            setSearchedError(error)
            setLoading(false)
        }
    }

    const delayedAPICall = useCallback(debounce(getSearchMovies, 1000), [])

    useEffect(() => {
        delayedAPICall(searchParam)
        return () => {
            delayedAPICall.cancel()
        }
    }, [delayedAPICall, searchParam])

    const detailModalComponent = buildVideoModal(detailModal, videoInfo, { closeModalHandler })

    // we check if the video has a poster or a mediaType because these properties are missing in 
    // some tiles, and, generally, a missing mediaType means there is no video overview or 
    // information. It's an easy fix to skip these little known movies, as the API itself 
    // doesn't provide information. 
    let movieCards
    if (!loading) {
        searchedVideoList.sort(sortVideosByPopularity)
        movieCards = searchedVideoList.map(video => {
            const { mediaType, extraInfo } = buildVideoMetadata(video, videoInfo)
            return video.poster_path && mediaType && (
                <div className="GridItem"
                    key={video.id}
                    onClick={
                        () => cardClickHandler(video.id, mediaType)}
                    onMouseEnter={
                        () => cardHoverHandler(video.id, mediaType)} >
                    <VideoCard name={video.name || video.title}
                        vote_average={video.vote_average}
                        poster_path={video.poster_path}
                        netflixOriginalCard={false} {...extraInfo} />
                </div>
            )
        })
    }

    return (
        (!videoInfoError && !searchedError) ? (
            <div className="SearchContent">
                <div className="SearchGrid">
                    {movieCards}
                </div>
                {detailModalComponent}
            </div>) :
            <ErrorPage errors={videoInfoError || searchedError} />
    )
}

export default SearchContent