Feat: Add implementation of the 'Next' button. (#992)

* Implementation of the 'Next' button.

* fix name

* Fix Key + Fix Textarea

* Update src/components/accordions/PopularNovels/index.tsx

Co-authored-by: nyagami <hoangquan05112002@gmail.com>

* Fix index null

---------

Co-authored-by: nyagami <hoangquan05112002@gmail.com>
This commit is contained in:
Elfing
2024-06-17 05:47:00 +02:00
committed by GitHub
parent 1fd7480352
commit 379f511954
3 changed files with 50 additions and 10 deletions
@@ -42,7 +42,7 @@ export default function ParseChapter() {
}} }}
maxRows={20} maxRows={20}
disabled disabled
defaultValue={chapterText} value={chapterText}
/> />
) : null} ) : null}
</AccordionContainer> </AccordionContainer>
@@ -102,7 +102,7 @@ export default function SourceNovelCard({
<Textarea <Textarea
sx={{ width: '100%' }} sx={{ width: '100%' }}
maxRows={5} maxRows={5}
defaultValue={sourceNovel.summary} value={sourceNovel.summary}
disabled disabled
/> />
</CardContent> </CardContent>
@@ -1,5 +1,11 @@
import { Box, Button, Stack } from '@mui/material'; import {
import React, { useEffect, useState } from 'react'; Box,
Button,
Stack,
ToggleButton,
ToggleButtonGroup,
} from '@mui/material';
import React, { useEffect, useState, ChangeEvent } from 'react';
import AccordionContainer from '../components/AccordionContainer'; import AccordionContainer from '../components/AccordionContainer';
import { Plugin } from '@typings/plugin'; import { Plugin } from '@typings/plugin';
import NovelItemCard from '../components/NovelItemCard'; import NovelItemCard from '../components/NovelItemCard';
@@ -11,16 +17,33 @@ export default function PopularNovels() {
const [novels, setNovels] = useState<Plugin.NovelItem[]>([]); const [novels, setNovels] = useState<Plugin.NovelItem[]>([]);
const [filterValues, setFilterValues] = useState<Filters | undefined>(); const [filterValues, setFilterValues] = useState<Filters | undefined>();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const fetchNovels = () => { const [currentIndex, setCurrentIndex] = useState(0);
if (plugin) { const [maxIndex, setMaxIndex] = useState(0);
const fetchNovelsByIndex = (index: number) => {
if (plugin && index) {
setLoading(true); setLoading(true);
plugin plugin
.popularNovels(1, { filters: filterValues }) .popularNovels(index, { filters: filterValues })
.then(res => setNovels(res)) .then(res => {
if (res.length !== 0) {
setCurrentIndex(index);
if (index > maxIndex) {
setMaxIndex(index);
}
setNovels(res);
}
})
.finally(() => setLoading(false)); .finally(() => setLoading(false));
} }
}; };
useEffect(() => { useEffect(() => {
// Reset when changing plugins.
setCurrentIndex(0);
setMaxIndex(0);
setNovels([]);
if (plugin?.filters) { if (plugin?.filters) {
let filters = {}; let filters = {};
for (const fKey in plugin.filters) { for (const fKey in plugin.filters) {
@@ -37,14 +60,31 @@ export default function PopularNovels() {
<Stack direction={'row'} spacing={2}> <Stack direction={'row'} spacing={2}>
<Button <Button
disabled={plugin === undefined} disabled={plugin === undefined}
onClick={fetchNovels} onClick={() => fetchNovelsByIndex(1)}
variant="contained" variant="contained"
> >
Fetch Fetch
</Button> </Button>
<Button disabled={plugin === undefined} variant="outlined"> <Button
disabled={currentIndex === 0}
variant="outlined"
onClick={() => fetchNovelsByIndex(currentIndex + 1)}
>
Next Page Next Page
</Button> </Button>
<ToggleButtonGroup
value={currentIndex}
exclusive
onChange={(event, value) => fetchNovelsByIndex(value)}
>
{Array.from({ length: maxIndex }, (_, index) => index + 1).map(
number => (
<ToggleButton key={`novel_page_index_${number}`} value={number}>
{number}
</ToggleButton>
),
)}
</ToggleButtonGroup>
</Stack> </Stack>
<Box <Box
sx={{ sx={{