refactor ChapterList (#125)

* Refactor to move all the Data fetch logic for chapters to seperate Hook called useChapterFetch

* Bug fix: No Chapters found Toast
This commit is contained in:
abhijeetChawla
2022-01-23 12:00:54 +05:30
committed by GitHub
parent 670508e085
commit f273b11a82
2 changed files with 47 additions and 29 deletions

View File

@@ -12,11 +12,11 @@ import Typography from '@mui/material/Typography';
import { CircularProgress, Fab } from '@mui/material';
import { Link } from 'react-router-dom';
import makeToast from 'components/util/Toast';
import client from 'util/client';
import PlayArrow from '@mui/icons-material/PlayArrow';
import ChapterOptions from 'components/chapter/ChapterOptions';
import ChapterCard from 'components/chapter/ChapterCard';
import useLocalStorage from 'util/useLocalStorage';
import useFetchChapters from './useFetchChapters';
const CustomVirtuoso = styled(Virtuoso)(({ theme }) => ({
listStyle: 'none',
@@ -85,11 +85,7 @@ function findFirstUnreadChapter(chapters: IChapter[]): IChapter | undefined {
export default function ChapterList(props: IProps) {
const { id } = props;
const [chapters, setChapters] = useState<IChapter[]>([]);
const [noChaptersFound, setNoChaptersFound] = useState(false);
const [chapterUpdateTriggerer, setChapterUpdateTriggerer] = useState(0);
const [fetchedOnline, setFetchedOnline] = useState(false);
const [fetchedOffline, setFetchedOffline] = useState(false);
const [chapters, triggerChaptersUpdate, noChaptersFound] = useFetchChapters(id);
const [firstUnreadChapter, setFirstUnreadChapter] = useState<IChapter>();
const [filteredChapters, setFilteredChapters] = useState<IChapter[]>([]);
const [options, setOptions] = useLocalStorage<ChapterListOptions>(
@@ -108,10 +104,6 @@ export default function ChapterList(props: IProps) {
const [, setWsClient] = useState<WebSocket>();
const [{ queue }, setQueueState] = useState<IQueue>(initialQueue);
function triggerChaptersUpdate() {
setChapterUpdateTriggerer(chapterUpdateTriggerer + 1);
}
useEffect(() => {
const wsc = new WebSocket(`${baseWebsocketUrl}/api/v1/downloads`);
wsc.onmessage = (e) => {
@@ -141,25 +133,6 @@ export default function ChapterList(props: IProps) {
return rtn;
};
useEffect(() => {
const shouldFetchOnline = fetchedOffline && !fetchedOnline;
client.get(`/api/v1/manga/${id}/chapters?onlineFetch=${shouldFetchOnline}`)
.then((response) => response.data)
.then((data) => {
if (data.length === 0 && fetchedOffline) {
makeToast('No chapters found', 'warning');
setNoChaptersFound(true);
}
setChapters(data);
})
.then(() => {
if (shouldFetchOnline) {
setFetchedOnline(true);
} else setFetchedOffline(true);
});
}, [fetchedOnline, fetchedOffline, chapterUpdateTriggerer]);
useEffect(() => {
const filtered = options.active
? chapters.filter((chp) => unreadFilter(options.unread, chp)
@@ -191,6 +164,12 @@ export default function ChapterList(props: IProps) {
</Fab>
));
useEffect(() => {
if (noChaptersFound) {
makeToast('No chapters found', 'warning');
}
}, [noChaptersFound]);
if (chapters.length === 0 || noChaptersFound) {
return (
<div style={{