fix(frontend): when there were no results in the list view, it would call fetch more infinitely

pull/175/head
sct 4 years ago
parent 366074c12a
commit c0ce87b6f6

@ -12,6 +12,7 @@ interface ListViewProps {
items?: (TvResult | MovieResult | PersonResult)[]; items?: (TvResult | MovieResult | PersonResult)[];
isEmpty?: boolean; isEmpty?: boolean;
isLoading?: boolean; isLoading?: boolean;
isReachingEnd?: boolean;
onScrollBottom: () => void; onScrollBottom: () => void;
} }
@ -20,8 +21,9 @@ const ListView: React.FC<ListViewProps> = ({
isEmpty, isEmpty,
isLoading, isLoading,
onScrollBottom, onScrollBottom,
isReachingEnd,
}) => { }) => {
useVerticalScroll(onScrollBottom, !isLoading); useVerticalScroll(onScrollBottom, !isLoading && !isEmpty && !isReachingEnd);
return ( return (
<> <>
{isEmpty && ( {isEmpty && (
@ -79,6 +81,7 @@ const ListView: React.FC<ListViewProps> = ({
); );
})} })}
{isLoading && {isLoading &&
!isReachingEnd &&
[...Array(10)].map((_item, i) => ( [...Array(10)].map((_item, i) => (
<li <li
key={`placeholder-${i}`} key={`placeholder-${i}`}

@ -49,6 +49,10 @@ const DiscoverMovies: React.FC = () => {
[] as MovieResult[] [] as MovieResult[]
); );
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -60,10 +64,11 @@ const DiscoverMovies: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }
isReachingEnd={isReachingEnd}
onScrollBottom={fetchMore} onScrollBottom={fetchMore}
/> />
</> </>

@ -46,6 +46,10 @@ const DiscoverTv: React.FC = () => {
const titles = data?.reduce((a, v) => [...a, ...v.results], [] as TvResult[]); const titles = data?.reduce((a, v) => [...a, ...v.results], [] as TvResult[]);
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -57,7 +61,8 @@ const DiscoverTv: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isReachingEnd={isReachingEnd}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }

@ -46,6 +46,10 @@ const MovieRecommendations: React.FC = () => {
[] as MovieResult[] [] as MovieResult[]
); );
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -57,7 +61,8 @@ const MovieRecommendations: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isReachingEnd={isReachingEnd}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }

@ -46,6 +46,10 @@ const MovieSimilar: React.FC = () => {
[] as MovieResult[] [] as MovieResult[]
); );
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -57,10 +61,11 @@ const MovieSimilar: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }
isReachingEnd={isReachingEnd}
onScrollBottom={fetchMore} onScrollBottom={fetchMore}
/> />
</> </>

@ -52,6 +52,10 @@ const Search: React.FC = () => {
[] as (MovieResult | TvResult | PersonResult)[] [] as (MovieResult | TvResult | PersonResult)[]
); );
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -79,10 +83,11 @@ const Search: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }
isReachingEnd={isReachingEnd}
onScrollBottom={fetchMore} onScrollBottom={fetchMore}
/> />
</> </>

@ -45,6 +45,10 @@ const TvRecommendations: React.FC = () => {
const titles = data?.reduce((a, v) => [...a, ...v.results], [] as TvResult[]); const titles = data?.reduce((a, v) => [...a, ...v.results], [] as TvResult[]);
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -56,7 +60,8 @@ const TvRecommendations: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isReachingEnd={isReachingEnd}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }

@ -48,6 +48,10 @@ const TvSimilar: React.FC = () => {
[] as MovieResult[] [] as MovieResult[]
); );
const isEmpty = !isLoadingInitialData && titles?.length === 0;
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.results.length < 20);
return ( return (
<> <>
<div className="md:flex md:items-center md:justify-between mb-8 mt-6"> <div className="md:flex md:items-center md:justify-between mb-8 mt-6">
@ -59,7 +63,8 @@ const TvSimilar: React.FC = () => {
</div> </div>
<ListView <ListView
items={titles} items={titles}
isEmpty={!isLoadingInitialData && titles?.length === 0} isEmpty={isEmpty}
isReachingEnd={isReachingEnd}
isLoading={ isLoading={
isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0) isLoadingInitialData || (isLoadingMore && (titles?.length ?? 0) > 0)
} }

Loading…
Cancel
Save