SearchResult.js 1.33 KB
Newer Older
Kim, Subin's avatar
Search    
Kim, Subin committed
1
2
3
4
5
6
import { useState, useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import queryString from 'query-string'
import MovieCard from "./MovieCard/index.js"
import movieApi from '../apis/movie.api.js'
import catchErrors from '../utils/catchErrors.js'
Kim, Subin's avatar
수빈    
Kim, Subin committed
7
8

const SearchResult = () => {
Kim, Subin's avatar
Search    
Kim, Subin committed
9
10
    const [result, setResult] = useState([])
    const [error, setError] = useState("")
Kim, Subin's avatar
수빈    
Kim, Subin committed
11
12
13
    const { search } = useLocation()
    const { title } = queryString.parse(search)

Kim, Subin's avatar
Search    
Kim, Subin committed
14
15
16
17
18
19
20
    useEffect(() => {
        findforKeyword()
    }, [search])

    async function findforKeyword() {
        try {
            setError("")
Kim, Subin's avatar
Kim, Subin committed
21
22
            const { count, results } = await movieApi.search(title)
            setResult([...results])
Kim, Subin's avatar
Search    
Kim, Subin committed
23
24
25
26
27
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
수빈    
Kim, Subin committed
28
29
    return (
        <>
Kim, Subin's avatar
Search    
Kim, Subin committed
30
31
32
            {result.length !== 0 ? (
                <>
                    <h3 className="text-white text-center my-5">'{title}'  관한 검색 결과입니다.</h3>
Kim, Subin's avatar
Kim, Subin committed
33
34
35
                    <div className="row row-cols-1 row-cols-md-4 g-4">
                        <MovieCard list={result} />
                    </div>
Kim, Subin's avatar
Search    
Kim, Subin committed
36
                </>
Kim, Subin's avatar
Kim, Subin committed
37
38
            ) : <h3 className="text-white text-center my-5">'{title}'  관한 검색 결과가 존재하지 않습니다.</h3>
            }
Kim, Subin's avatar
수빈    
Kim, Subin committed
39
40
41
42
43
        </>
    )
}

export default SearchResult