refactor, fix fetch loop

This commit is contained in:
Aria Moradi
2021-01-19 14:47:07 +03:30
parent 1e46a0c78c
commit f085f26d10
7 changed files with 100 additions and 81 deletions

View File

@@ -0,0 +1,21 @@
import React, { useEffect, useState } from 'react';
import ExtensionCard from '../components/ExtensionCard';
export default function Extensions() {
let mapped;
const [extensions, setExtensions] = useState<IExtension[]>([]);
useEffect(() => {
fetch('http://127.0.0.1:4567/api/v1/extension/list')
.then((response) => response.json())
.then((data) => setExtensions(data));
}, []);
if (extensions.length === 0) {
mapped = <h3>wait</h3>;
} else {
mapped = extensions.map((it) => <ExtensionCard extension={it} />);
}
return <h2>{mapped}</h2>;
}

View File

@@ -0,0 +1,9 @@
import React from 'react';
export default function Home() {
return (
<h1>
Home
</h1>
);
}

View File

@@ -0,0 +1,37 @@
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import MangaCard from '../components/MangaCard';
interface IManga {
title: string
thumbnailUrl: string
}
export default function MangaList(props: { popular: boolean }) {
const { sourceId } = useParams<{sourceId: string}>();
let mapped;
const [mangas, setMangas] = useState<IManga[]>([]);
useEffect(() => {
const sourceType = props.popular ? 'popular' : 'latest';
fetch(`http://127.0.0.1:4567/api/v1/source/${sourceId}/${sourceType}`)
.then((response) => response.json())
.then((data: { title: string, thumbnail_url: string }[]) => setMangas(
data.map((it) => ({ title: it.title, thumbnailUrl: it.thumbnail_url })),
));
}, []);
if (mangas.length === 0) {
mapped = <h3>wait</h3>;
} else {
mapped = (
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(5, auto)', gridGap: '1em' }}>
{mangas.map((it) => (
<MangaCard manga={it} />
))}
</div>
);
}
return mapped;
}

View File

@@ -0,0 +1,21 @@
import React, { useEffect, useState } from 'react';
import SourceCard from '../components/SourceCard';
export default function Sources() {
let mapped;
const [sources, setSources] = useState<ISource[]>([]);
useEffect(() => {
fetch('http://127.0.0.1:4567/api/v1/source/list')
.then((response) => response.json())
.then((data) => setSources(data));
}, []);
if (sources.length === 0) {
mapped = <h3>wait</h3>;
} else {
mapped = sources.map((it) => <SourceCard source={it} />);
}
return <h2>{mapped}</h2>;
}