MovieEdit.js 1.83 KB
Newer Older
1
import { useState, useEffect } from "react";
Kim, Subin's avatar
Kim, Subin committed
2
import Search from "../Search";
Kim, Subin's avatar
Kim, Subin committed
3
4
import MovieTable from "../MovieTable";
import Pagination from "../Pagination";
5
6
import movieApi from "../../apis/movie.api.js";
import catchErrors from "../../utils/catchErrors.js";
Kim, Subin's avatar
Kim, Subin committed
7
8

const MovieEdit = () => {
9
    const [search, setSearch] = useState({ type: "admin", keyword: "" })
10
    const [movieList, setMovieList] = useState([])
Kim, Subin's avatar
Kim, Subin committed
11
12
    const [totalPages, setTotalPages] = useState(0)
    const [activePage, setActivePage] = useState(1)
13
14
15
    const [error, setError] = useState("")

    useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
16
        paginate(activePage)
17
18
    }, [])

Kim, Subin's avatar
Kim, Subin committed
19
    async function paginate(pageNum) {
20
        try {
Kim, Subin's avatar
Kim, Subin committed
21
22
23
24
            const { TMDBmovies, totalPage } = (search.keyword !== '') ? await movieApi.search(search, pageNum) : await movieApi.getAllfromTM(pageNum)
            setActivePage(pageNum)
            setTotalPages(totalPage)
            setMovieList(TMDBmovies)
25
        } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
26
            catchErrors(error, setError);
27
28
29
        }
    }

Kim, Subin's avatar
Kim, Subin committed
30
31
32
33
34
    const prevPage = () => {
        if (activePage > 1) {
            paginate(activePage - 1)
        } else {
            paginate(1);
Kim, Subin's avatar
수빈    
Kim, Subin committed
35
        }
Kim, Subin's avatar
Kim, Subin committed
36
37
38
39
40
41
42
43
44
    };

    const nextPage = () => {
        if (activePage < totalPages) {
            paginate(activePage + 1)
        } else {
            paginate(totalPages);
        }
    };
Kim, Subin's avatar
수빈    
Kim, Subin committed
45

Kim, Subin's avatar
Kim, Subin committed
46
    return (
Kim, Subin's avatar
Kim, Subin committed
47
        <>
Kim, Subin's avatar
수빈    
Kim, Subin committed
48
            <div className="d-flex justify-content-md-end justify-content-center mb-3">
Kim, Subin's avatar
Kim, Subin committed
49
                <Search search={search} setSearch={setSearch} handleClick={paginate} />
Kim, Subin's avatar
Kim, Subin committed
50
            </div>
51
            <MovieTable movieList={movieList} />
Kim, Subin's avatar
Kim, Subin committed
52
53
54
55
56
57
            <Pagination
                totalPages={totalPages}
                activePage={activePage}
                prevPage={prevPage}
                nextPage={nextPage}
                paginate={paginate} />
Kim, Subin's avatar
Kim, Subin committed
58
        </>
Kim, Subin's avatar
Kim, Subin committed
59
60
61
    )
}
export default MovieEdit