2021-02-13 21:12:18 +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-02-20 01:23:52 +03:30
|
|
|
import { Tab, Tabs } from '@material-ui/core';
|
2021-02-13 21:12:18 +03:30
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
|
import MangaGrid from '../components/MangaGrid';
|
|
|
|
|
import NavBarTitle from '../context/NavbarTitle';
|
|
|
|
|
|
2021-02-20 01:23:52 +03:30
|
|
|
interface IMangaCategory {
|
|
|
|
|
category: ICategory
|
|
|
|
|
mangas: IManga[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface TabPanelProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
index: any;
|
|
|
|
|
value: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function TabPanel(props: TabPanelProps) {
|
|
|
|
|
const {
|
2021-02-21 04:27:41 +03:30
|
|
|
children, value, index,
|
2021-02-20 01:23:52 +03:30
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
role="tabpanel"
|
|
|
|
|
hidden={value !== index}
|
|
|
|
|
id={`simple-tabpanel-${index}`}
|
|
|
|
|
>
|
|
|
|
|
{value === index && children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Library() {
|
2021-02-13 21:12:18 +03:30
|
|
|
const { setTitle } = useContext(NavBarTitle);
|
2021-02-20 01:23:52 +03:30
|
|
|
const [tabs, setTabs] = useState<IMangaCategory[]>([]);
|
|
|
|
|
const [tabNum, setTabNum] = useState<number>(0);
|
2021-02-13 21:12:18 +03:30
|
|
|
|
2021-02-21 04:27:41 +03:30
|
|
|
// a hack so MangaGrid doesn't stop working. I won't change it in case
|
|
|
|
|
// if I do manga pagination for library..
|
|
|
|
|
const [lastPageNum, setLastPageNum] = useState<number>(1);
|
2021-02-13 21:12:18 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
setTitle('Library');
|
|
|
|
|
}, []);
|
2021-02-21 04:27:41 +03:30
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
|
|
|
const fetchAndSetMangas = (tabs: IMangaCategory[], tab: IMangaCategory, index: number) => {
|
|
|
|
|
fetch(`http://127.0.0.1:4567/api/v1/category/${tab.category.id}`)
|
|
|
|
|
.then((response) => response.json())
|
|
|
|
|
.then((data: IManga[]) => {
|
|
|
|
|
const tabsClone = JSON.parse(JSON.stringify(tabs));
|
|
|
|
|
tabsClone[index].mangas = data;
|
|
|
|
|
setTabs(tabsClone); // clone the object
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleTabChange = (newTab: number) => {
|
|
|
|
|
setTabNum(newTab);
|
|
|
|
|
tabs.forEach((tab, index) => {
|
|
|
|
|
if (tab.category.order === newTab && tab.mangas.length === 0) {
|
|
|
|
|
// mangas are empty, fetch the mangas
|
|
|
|
|
fetchAndSetMangas(tabs, tab, index);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-13 21:12:18 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
fetch('http://127.0.0.1:4567/api/v1/library')
|
|
|
|
|
.then((response) => response.json())
|
|
|
|
|
.then((data: IManga[]) => {
|
2021-02-20 02:28:55 +03:30
|
|
|
// if some manga with no category exist, they will be added under a virtual category
|
2021-02-20 01:23:52 +03:30
|
|
|
if (data.length > 0) {
|
2021-02-21 04:27:41 +03:30
|
|
|
return [
|
2021-02-20 01:23:52 +03:30
|
|
|
{
|
|
|
|
|
category: {
|
2021-02-21 04:27:41 +03:30
|
|
|
name: 'Default', isLanding: true, order: 0, id: -1,
|
2021-02-20 01:23:52 +03:30
|
|
|
},
|
|
|
|
|
mangas: data,
|
|
|
|
|
},
|
2021-02-20 02:28:55 +03:30
|
|
|
]; // will set state on the next fetch
|
2021-02-20 01:23:52 +03:30
|
|
|
}
|
2021-02-21 04:27:41 +03:30
|
|
|
|
|
|
|
|
// no default category so the first tab is 1
|
|
|
|
|
setTabNum(1);
|
|
|
|
|
return [];
|
2021-02-20 02:28:55 +03:30
|
|
|
})
|
|
|
|
|
.then(
|
2021-02-21 04:27:41 +03:30
|
|
|
(newTabs: IMangaCategory[]) => {
|
2021-02-20 02:28:55 +03:30
|
|
|
fetch('http://127.0.0.1:4567/api/v1/category')
|
|
|
|
|
.then((response) => response.json())
|
|
|
|
|
.then((data: ICategory[]) => {
|
|
|
|
|
const mangaCategories = data.map((category) => ({
|
|
|
|
|
category,
|
|
|
|
|
mangas: [] as IManga[],
|
|
|
|
|
}));
|
2021-02-21 04:27:41 +03:30
|
|
|
const newNewTabs = [...newTabs, ...mangaCategories];
|
|
|
|
|
setTabs(newNewTabs);
|
|
|
|
|
|
|
|
|
|
// if no default category, we must fetch the first tab now...
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
if (newTabs.length === 0) { fetchAndSetMangas(newNewTabs, newNewTabs[0], 0); }
|
2021-02-20 02:28:55 +03:30
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
2021-02-20 01:23:52 +03:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
let toRender;
|
|
|
|
|
if (tabs.length > 1) {
|
2021-02-21 04:27:41 +03:30
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
const tabDefines = tabs.map((tab) => (<Tab label={tab.category.name} value={tab.category.order} />));
|
2021-02-20 01:23:52 +03:30
|
|
|
|
2021-02-21 04:27:41 +03:30
|
|
|
const tabBodies = tabs.map((tab) => (
|
|
|
|
|
<TabPanel value={tabNum} index={tab.category.order}>
|
2021-02-20 01:23:52 +03:30
|
|
|
<MangaGrid
|
|
|
|
|
mangas={tab.mangas}
|
|
|
|
|
hasNextPage={false}
|
|
|
|
|
lastPageNum={lastPageNum}
|
|
|
|
|
setLastPageNum={setLastPageNum}
|
|
|
|
|
/>
|
|
|
|
|
</TabPanel>
|
|
|
|
|
));
|
2021-02-20 02:28:55 +03:30
|
|
|
|
|
|
|
|
// 160px is min-width for viewport width of >600
|
|
|
|
|
const scrollableTabs = window.innerWidth < tabs.length * 160;
|
2021-02-20 01:23:52 +03:30
|
|
|
toRender = (
|
|
|
|
|
<>
|
|
|
|
|
<Tabs
|
|
|
|
|
value={tabNum}
|
2021-02-21 04:27:41 +03:30
|
|
|
onChange={(e, newTab) => handleTabChange(newTab)}
|
2021-02-20 01:23:52 +03:30
|
|
|
indicatorColor="primary"
|
|
|
|
|
textColor="primary"
|
2021-02-20 02:28:55 +03:30
|
|
|
centered={!scrollableTabs}
|
|
|
|
|
variant={scrollableTabs ? 'scrollable' : 'fullWidth'}
|
|
|
|
|
scrollButtons="on"
|
2021-02-20 01:23:52 +03:30
|
|
|
>
|
|
|
|
|
{tabDefines}
|
|
|
|
|
</Tabs>
|
|
|
|
|
{tabBodies}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const mangas = tabs.length === 1 ? tabs[0].mangas : [];
|
|
|
|
|
toRender = (
|
|
|
|
|
<MangaGrid
|
|
|
|
|
mangas={mangas}
|
|
|
|
|
hasNextPage={false}
|
|
|
|
|
lastPageNum={lastPageNum}
|
|
|
|
|
setLastPageNum={setLastPageNum}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toRender;
|
2021-02-13 21:12:18 +03:30
|
|
|
}
|