[skip ci] Feat: Add switch Latest / Popular (#995)

* Add switch Latest / Popular

* Fix label
This commit is contained in:
Elfing
2024-06-20 10:35:43 +02:00
committed by GitHub
parent bf4c4e4f6c
commit 75c855b1ee
@@ -4,6 +4,9 @@ import {
Stack, Stack,
ToggleButton, ToggleButton,
ToggleButtonGroup, ToggleButtonGroup,
Switch,
FormControlLabel,
FormGroup,
} from '@mui/material'; } from '@mui/material';
import React, { useEffect, useState, ChangeEvent } from 'react'; import React, { useEffect, useState, ChangeEvent } from 'react';
import AccordionContainer from '../components/AccordionContainer'; import AccordionContainer from '../components/AccordionContainer';
@@ -19,12 +22,16 @@ export default function PopularNovels() {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [currentIndex, setCurrentIndex] = useState(0); const [currentIndex, setCurrentIndex] = useState(0);
const [maxIndex, setMaxIndex] = useState(0); const [maxIndex, setMaxIndex] = useState(0);
const [isLatest, setIsLatest] = useState(true);
const fetchNovelsByIndex = (index: number) => { const fetchNovelsByIndex = (index: number) => {
if (plugin && index) { if (plugin && index) {
setLoading(true); setLoading(true);
plugin plugin
.popularNovels(index, { filters: filterValues }) .popularNovels(index, {
filters: filterValues,
showLatestNovels: isLatest,
})
.then(res => { .then(res => {
if (res.length !== 0) { if (res.length !== 0) {
setCurrentIndex(index); setCurrentIndex(index);
@@ -38,6 +45,18 @@ export default function PopularNovels() {
} }
}; };
const handleSwitchLatestChange = (event: ChangeEvent<HTMLInputElement>) => {
setIsLatest(event.target.checked);
};
useEffect(() => {
if (plugin) {
setCurrentIndex(1);
setMaxIndex(1);
fetchNovelsByIndex(1);
}
}, [isLatest]);
useEffect(() => { useEffect(() => {
// Reset when changing plugins. // Reset when changing plugins.
setCurrentIndex(0); setCurrentIndex(0);
@@ -57,7 +76,19 @@ export default function PopularNovels() {
}, [plugin]); }, [plugin]);
return ( return (
<AccordionContainer title="Popular Novels" loading={loading}> <AccordionContainer title="Popular Novels" loading={loading}>
<Stack direction={'row'} spacing={2}> <Stack direction={'row'} spacing={2} alignItems={'center'}>
<FormGroup>
<FormControlLabel
control={
<Switch
checked={isLatest}
onChange={handleSwitchLatestChange}
color="secondary"
/>
}
label={<Box minWidth={70}>{isLatest ? 'Latest' : 'Popular'}</Box>}
/>
</FormGroup>
<Button <Button
disabled={plugin === undefined} disabled={plugin === undefined}
onClick={() => fetchNovelsByIndex(1)} onClick={() => fetchNovelsByIndex(1)}