46 lines
1.5 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, { useEffect, useState, useContext } from 'react';
2021-01-19 20:20:28 +03:30
import { useParams } from 'react-router-dom';
import ChapterCard from '../components/ChapterCard';
import MangaDetails from '../components/MangaDetails';
2021-01-22 17:00:33 +03:30
import NavBarTitle from '../context/NavbarTitle';
2021-01-19 20:20:28 +03:30
export default function Manga() {
const { id } = useParams<{id: string}>();
2021-01-22 17:00:33 +03:30
const { setTitle } = useContext(NavBarTitle);
2021-01-19 20:20:28 +03:30
2021-01-19 21:02:57 +03:30
const [manga, setManga] = useState<IManga>();
const [chapters, setChapters] = useState<IChapter[]>([]);
useEffect(() => {
fetch(`http://127.0.0.1:4567/api/v1/manga/${id}/`)
.then((response) => response.json())
2021-01-22 17:00:33 +03:30
.then((data: IManga) => {
setManga(data);
setTitle(data.title);
});
2021-01-19 21:02:57 +03:30
}, []);
useEffect(() => {
2021-01-20 00:04:12 +03:30
fetch(`http://127.0.0.1:4567/api/v1/manga/${id}/chapters`)
2021-01-19 21:02:57 +03:30
.then((response) => response.json())
.then((data) => setChapters(data));
}, []);
const chapterCards = chapters.map((chapter) => (
<ol style={{ listStyle: 'none', padding: 0 }}>
<ChapterCard chapter={chapter} />
</ol>
));
2021-01-19 20:20:28 +03:30
return (
<>
2021-02-13 21:12:18 +03:30
{manga && <MangaDetails manga={manga} />}
2021-01-19 21:02:57 +03:30
{chapterCards}
2021-01-19 20:20:28 +03:30
</>
);
}