tachiyomi-extensions-inspector/webUI/react/src/screens/SourceMangas.tsx

46 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-01-26 21:02:12 +01:00
/* 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 14:30:33 +01:00
import React, { useContext, useEffect, useState } from 'react';
2021-01-19 12:17:07 +01:00
import { useParams } from 'react-router-dom';
2021-01-20 12:56:52 +01:00
import MangaGrid from '../components/MangaGrid';
2021-01-22 14:30:33 +01:00
import NavBarTitle from '../context/NavbarTitle';
2021-01-19 12:17:07 +01:00
2021-02-19 22:53:52 +01:00
export default function SourceMangas(props: { popular: boolean }) {
2021-01-19 12:17:07 +01:00
const { sourceId } = useParams<{sourceId: string}>();
2021-01-22 14:30:33 +01:00
const { setTitle } = useContext(NavBarTitle);
2021-01-19 12:17:07 +01:00
const [mangas, setMangas] = useState<IManga[]>([]);
2021-01-22 18:41:00 +01:00
const [hasNextPage, setHasNextPage] = useState<boolean>(false);
const [lastPageNum, setLastPageNum] = useState<number>(1);
2021-01-19 12:17:07 +01:00
2021-01-22 14:30:33 +01:00
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 12:17:07 +01:00
useEffect(() => {
const sourceType = props.popular ? 'popular' : 'latest';
2021-01-19 12:37:20 +01:00
fetch(`http://127.0.0.1:4567/api/v1/source/${sourceId}/${sourceType}/${lastPageNum}`)
2021-01-19 12:17:07 +01:00
.then((response) => response.json())
2021-01-22 18:41:00 +01:00
.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 12:17:07 +01:00
2021-01-22 18:41:00 +01:00
return (
<MangaGrid
mangas={mangas}
hasNextPage={hasNextPage}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
/>
);
2021-01-19 12:17:07 +01:00
}