2021-01-26 23:32:12 +03:30
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2021-01-22 21:11:00 +03:30
|
|
|
import React, { useEffect, useRef } from 'react';
|
2021-01-23 01:58:18 +03:30
|
|
|
import Grid from '@material-ui/core/Grid';
|
2021-01-20 15:26:52 +03:30
|
|
|
import MangaCard from './MangaCard';
|
|
|
|
|
|
|
|
|
|
interface IProps{
|
|
|
|
|
mangas: IManga[]
|
|
|
|
|
message?: string
|
2021-01-22 21:11:00 +03:30
|
|
|
hasNextPage: boolean
|
|
|
|
|
lastPageNum: number
|
|
|
|
|
setLastPageNum: (lastPageNum: number) => void
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function MangaGrid(props: IProps) {
|
2021-01-22 21:11:00 +03:30
|
|
|
const {
|
2021-01-29 14:25:18 +03:30
|
|
|
mangas, message, hasNextPage, lastPageNum, setLastPageNum,
|
2021-01-22 21:11:00 +03:30
|
|
|
} = props;
|
2021-01-20 15:26:52 +03:30
|
|
|
let mapped;
|
2021-01-22 21:11:00 +03:30
|
|
|
const lastManga = useRef<HTMLInputElement>();
|
|
|
|
|
|
|
|
|
|
const scrollHandler = () => {
|
|
|
|
|
if (lastManga.current) {
|
|
|
|
|
const rect = lastManga.current.getBoundingClientRect();
|
|
|
|
|
if (((rect.y + rect.height) / window.innerHeight < 2) && hasNextPage) {
|
|
|
|
|
setLastPageNum(lastPageNum + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('scroll', scrollHandler, true);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('scroll', scrollHandler, true);
|
|
|
|
|
};
|
|
|
|
|
}, [hasNextPage, mangas]);
|
2021-01-20 15:26:52 +03:30
|
|
|
|
|
|
|
|
if (mangas.length === 0) {
|
2021-01-22 21:11:00 +03:30
|
|
|
mapped = <h3>{message}</h3>;
|
2021-01-20 15:26:52 +03:30
|
|
|
} else {
|
2021-01-22 21:11:00 +03:30
|
|
|
mapped = mangas.map((it, idx) => {
|
|
|
|
|
if (idx === mangas.length - 1) {
|
2021-01-29 14:25:18 +03:30
|
|
|
return <MangaCard manga={it} ref={lastManga} />;
|
2021-01-22 21:11:00 +03:30
|
|
|
}
|
2021-01-29 14:25:18 +03:30
|
|
|
return <MangaCard manga={it} />;
|
2021-01-22 21:11:00 +03:30
|
|
|
});
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
|
|
|
|
|
2021-01-22 21:11:00 +03:30
|
|
|
return (
|
2021-03-16 23:42:51 +03:30
|
|
|
<Grid container spacing={1} style={{ margin: 0, padding: '5px' }}>
|
2021-01-22 21:11:00 +03:30
|
|
|
{mapped}
|
2021-01-23 01:58:18 +03:30
|
|
|
</Grid>
|
2021-01-22 21:11:00 +03:30
|
|
|
);
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
2021-01-22 21:11:00 +03:30
|
|
|
|
|
|
|
|
MangaGrid.defaultProps = {
|
|
|
|
|
message: 'loading...',
|
|
|
|
|
};
|