Default is an actual category now

This commit is contained in:
Aria Moradi
2021-08-21 03:46:01 +04:30
parent c1f43157d9
commit 7a4bc28669
2 changed files with 17 additions and 34 deletions

View File

@@ -48,6 +48,7 @@ export default function CategorySelect(props: IProps) {
client.get('/api/v1/category/') client.get('/api/v1/category/')
.then((response) => response.data) .then((response) => response.data)
.then((data: ICategory[]) => { .then((data: ICategory[]) => {
if (data.length > 0 && data[0].name === 'Default') { data.shift(); }
tmpCategoryInfos = data.map((category) => ({ category, selected: false })); tmpCategoryInfos = data.map((category) => ({ category, selected: false }));
}) })
.then(() => { .then(() => {

View File

@@ -56,45 +56,23 @@ export default function Library() {
}; };
useEffect(() => { useEffect(() => {
Promise.all<IManga[], ICategory[]>([ client.get('/api/v1/category')
client.get('/api/v1/library').then((response) => response.data), .then((response) => response.data)
client.get('/api/v1/category').then((response) => response.data), .then((categories: ICategory[]) => {
])
.then(
([libraryMangas, categories]) => {
const categoryTabs = categories.map((category) => ({ const categoryTabs = categories.map((category) => ({
category, category,
mangas: [] as IManga[], mangas: [] as IManga[],
isFetched: false, isFetched: false,
})); }));
if (libraryMangas.length > 0 || categoryTabs.length === 0) {
const defaultCategoryTab = {
category: {
name: 'Default',
default: true,
order: 0,
id: -1,
},
mangas: libraryMangas,
isFetched: true,
};
setTabs(
[defaultCategoryTab, ...categoryTabs],
);
} else {
setTabs(categoryTabs); setTabs(categoryTabs);
setTabNum(1); });
}
},
);
}, []); }, []);
// console.log(client.defaults.baseURL);
// fetch the current tab // fetch the current tab
useEffect(() => { useEffect(() => {
tabs.forEach((tab, index) => { tabs.forEach((tab, index) => {
if (tab.category.order === tabNum && !tab.isFetched) { if (index === tabNum && !tab.isFetched) {
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
client.get(`/api/v1/category/${tab.category.id}`) client.get(`/api/v1/category/${tab.category.id}`)
.then((response) => response.data) .then((response) => response.data)
@@ -107,7 +85,11 @@ export default function Library() {
}); });
} }
}); });
}, [tabNum]); }, [tabs.length, tabNum]);
if (tabs.length === 0) {
return <h3>Library is empty</h3>;
}
let toRender; let toRender;
if (tabs.length > 1) { if (tabs.length > 1) {