import React from 'react'; import { useSWRInfinite } from 'swr'; import { MovieResult } from '../../../server/models/Search'; import ListView from '../Common/ListView'; import { useRouter } from 'next/router'; interface SearchResult { page: number; totalResults: number; totalPages: number; results: MovieResult[]; } const MovieSimilar: React.FC = () => { const router = useRouter(); const { data, error, size, setSize } = useSWRInfinite( (pageIndex: number, previousPageData: SearchResult | null) => { if (previousPageData && pageIndex + 1 > previousPageData.totalPages) { return null; } return `/api/v1/movie/${router.query.movieId}/similar?page=${ pageIndex + 1 }`; }, { initialSize: 3, } ); const isLoadingInitialData = !data && !error; const isLoadingMore = isLoadingInitialData || (size > 0 && data && typeof data[size - 1] === 'undefined'); const fetchMore = () => { setSize(size + 1); }; if (error) { return
{error}
; } const titles = data?.reduce( (a, v) => [...a, ...v.results], [] as MovieResult[] ); return ( <>

Similar Titles

0) } onScrollBottom={fetchMore} /> ); }; export default MovieSimilar;