46 lines
1.8 KiB
TypeScript
Raw Normal View History

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 17:00:33 +03:30
import React, { useContext, useEffect, useState } from 'react';
2021-01-19 14:47:07 +03:30
import { useParams } from 'react-router-dom';
2021-01-20 15:26:52 +03:30
import MangaGrid from '../components/MangaGrid';
2021-01-22 17:00:33 +03:30
import NavBarTitle from '../context/NavbarTitle';
2021-01-19 14:47:07 +03:30
export default function MangaList(props: { popular: boolean }) {
const { sourceId } = useParams<{sourceId: string}>();
2021-01-22 17:00:33 +03:30
const { setTitle } = useContext(NavBarTitle);
2021-01-19 14:47:07 +03:30
const [mangas, setMangas] = useState<IManga[]>([]);
2021-01-22 21:11:00 +03:30
const [hasNextPage, setHasNextPage] = useState<boolean>(false);
const [lastPageNum, setLastPageNum] = useState<number>(1);
2021-01-19 14:47:07 +03:30
2021-01-22 17:00:33 +03:30
useEffect(() => {
fetch(`http://127.0.0.1:4567/api/v1/source/${sourceId}`)
.then((response) => response.json())
.then((data: { name: string }) => setTitle(data.name));
}, []);
2021-01-19 14:47:07 +03:30
useEffect(() => {
const sourceType = props.popular ? 'popular' : 'latest';
2021-01-19 15:07:20 +03:30
fetch(`http://127.0.0.1:4567/api/v1/source/${sourceId}/${sourceType}/${lastPageNum}`)
2021-01-19 14:47:07 +03:30
.then((response) => response.json())
2021-01-22 21:11:00 +03:30
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
setMangas([
...mangas,
...data.mangaList.map((it) => ({
title: it.title, thumbnailUrl: it.thumbnailUrl, id: it.id,
}))]);
setHasNextPage(data.hasNextPage);
});
}, [lastPageNum]);
2021-01-19 14:47:07 +03:30
2021-01-22 21:11:00 +03:30
return (
<MangaGrid
mangas={mangas}
hasNextPage={hasNextPage}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
/>
);
2021-01-19 14:47:07 +03:30
}