From f085f26d10441b9131c4651d82ea91e179ea7920 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Tue, 19 Jan 2021 14:47:07 +0330 Subject: [PATCH] refactor, fix fetch loop --- webUI/react/.gitignore | 1 + webUI/react/src/App.tsx | 90 +++-------------------- webUI/react/src/components/SourceCard.tsx | 2 +- webUI/react/src/screens/Extensions.tsx | 21 ++++++ webUI/react/src/screens/Home.tsx | 9 +++ webUI/react/src/screens/PopularManga.tsx | 37 ++++++++++ webUI/react/src/screens/Sources.tsx | 21 ++++++ 7 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 webUI/react/src/screens/Extensions.tsx create mode 100644 webUI/react/src/screens/Home.tsx create mode 100644 webUI/react/src/screens/PopularManga.tsx create mode 100644 webUI/react/src/screens/Sources.tsx diff --git a/webUI/react/.gitignore b/webUI/react/.gitignore index 6b02b55..24daa58 100644 --- a/webUI/react/.gitignore +++ b/webUI/react/.gitignore @@ -1,2 +1,3 @@ node_modules/ .eslintcache +.vscode diff --git a/webUI/react/src/App.tsx b/webUI/react/src/App.tsx index 39e780e..87d98b2 100644 --- a/webUI/react/src/App.tsx +++ b/webUI/react/src/App.tsx @@ -1,97 +1,27 @@ -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { - BrowserRouter as Router, - Switch, - Route, - useParams, + BrowserRouter as Router, Route, Switch, } from 'react-router-dom'; -import Button from '@material-ui/core/Button'; import NavBar from './components/NavBar'; -import ExtensionCard from './components/ExtensionCard'; -import SourceCard from './components/SourceCard'; -import MangaCard from './components/MangaCard'; - -function MangaPage() { - const { sourceId } = useParams<{sourceId: string}>(); - let mapped; - const [mangas, setMangas] = useState([]); - - useEffect(() => { - fetch(`http://127.0.0.1:4567/api/v1/source/${sourceId}/popular`) - .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 =

wait

; - } else { - mapped = ( -
- {mangas.map((it) => ( - - ))} -
- ); - } - - return mapped; -} - -function Extensions() { - let mapped; - const [extensions, setExtensions] = useState([]); - - if (extensions.length === 0) { - mapped =

wait

; - fetch('http://127.0.0.1:4567/api/v1/extension/list') - .then((response) => response.json()) - .then((data) => setExtensions(data)); - } else { - mapped = extensions.map((it) => ); - } - - return

{mapped}

; -} - -function Sources() { - let mapped; - const [sources, setSources] = useState([]); - - if (sources.length === 0) { - mapped =

wait

; - fetch('http://127.0.0.1:4567/api/v1/source/list') - .then((response) => response.json()) - .then((data) => setSources(data)); - } else { - mapped = sources.map((it) => ); - } - - return

{mapped}

; -} - -function Home() { - return ( - - ); -} +import Home from './screens/Home'; +import Sources from './screens/Sources'; +import Extensions from './screens/Extensions'; +import MangaList from './screens/PopularManga'; export default function App() { return ( - {/* */} - {/* eslint-disable-next-line react/no-children-prop */} - + + + + diff --git a/webUI/react/src/components/SourceCard.tsx b/webUI/react/src/components/SourceCard.tsx index 530d1e2..60aacb6 100644 --- a/webUI/react/src/components/SourceCard.tsx +++ b/webUI/react/src/components/SourceCard.tsx @@ -65,7 +65,7 @@ export default function SourceCard(props: IProps) {
- {supportsLatest && } + {supportsLatest && }
diff --git a/webUI/react/src/screens/Extensions.tsx b/webUI/react/src/screens/Extensions.tsx new file mode 100644 index 0000000..51622fd --- /dev/null +++ b/webUI/react/src/screens/Extensions.tsx @@ -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([]); + + 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 =

wait

; + } else { + mapped = extensions.map((it) => ); + } + + return

{mapped}

; +} diff --git a/webUI/react/src/screens/Home.tsx b/webUI/react/src/screens/Home.tsx new file mode 100644 index 0000000..08edfc2 --- /dev/null +++ b/webUI/react/src/screens/Home.tsx @@ -0,0 +1,9 @@ +import React from 'react'; + +export default function Home() { + return ( +

+ Home +

+ ); +} diff --git a/webUI/react/src/screens/PopularManga.tsx b/webUI/react/src/screens/PopularManga.tsx new file mode 100644 index 0000000..fa311e9 --- /dev/null +++ b/webUI/react/src/screens/PopularManga.tsx @@ -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([]); + + 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 =

wait

; + } else { + mapped = ( +
+ {mangas.map((it) => ( + + ))} +
+ ); + } + + return mapped; +} diff --git a/webUI/react/src/screens/Sources.tsx b/webUI/react/src/screens/Sources.tsx new file mode 100644 index 0000000..96d2cd5 --- /dev/null +++ b/webUI/react/src/screens/Sources.tsx @@ -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([]); + + 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 =

wait

; + } else { + mapped = sources.map((it) => ); + } + + return

{mapped}

; +}