完成改造

This commit is contained in:
eson 2023-07-09 11:30:56 +08:00
parent c5159c3eab
commit aafd26d853
2 changed files with 56 additions and 56 deletions

View File

@ -10,17 +10,10 @@ import CircularProgress from '@mui/material/CircularProgress';
import ConfigContext from './Config'; import ConfigContext from './Config';
import MovieCard from './components/MovieCard'; import MovieCard from './components/MovieCard';
import CategoryNav from './components/CategoryNav'; import CategoryNav from './components/CategoryNav';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
const Main = () => { const Main = () => {
const config = useContext(ConfigContext); const config = useContext(ConfigContext);
const [loading, setLoading] = useState(false);
const [currentCategory, setCurrentCategory] = useState(0);
const categories = [ const categories = [
{ label: '15min', idx: 0 }, { label: '15min', idx: 0 },
{ label: '30min', idx: 1 }, { label: '30min', idx: 1 },
@ -28,6 +21,12 @@ const Main = () => {
{ label: '大于60min', idx: 3 }, { label: '大于60min', idx: 3 },
]; ];
const limit = 20;
const [currentCategory, setCurrentCategory] = useState(parseInt(localStorage.getItem('lastCategory'), 10) || 0);
const [pagination, setPagination] = useState({ const [pagination, setPagination] = useState({
movies: [], movies: [],
page: 1, page: 1,
@ -35,26 +34,18 @@ const Main = () => {
length: 1, length: 1,
}); });
const [loading, setLoading] = useState(false);
const limit = 20;
const handleCategoryChange = (category) => {
setCurrentCategory(category);
fetchMovies(category, 1);
};
const fetchMovies = useCallback(async (category, page) => { const fetchMovies = useCallback(async (category, page) => {
setLoading(true); setLoading(true);
try { try {
const response = await axios.get( const response = await axios.get(
`${config.Host}/movie/?page=${page}&limit=${limit}&category=${category}` `${config.Host}/movie/?page=${page}&limit=${limit}&category=${category}`
); );
if (response.status === 200) { if (response.status === 200) {
const data = response.data.data; const data = response.data.data;
if (data.items.length === 0 && page > 1) { if (data.items.length === 0 && page > 1) {
// 1 // 1
fetchMovies(category, page - 1); fetchMovies(category, page - 1);
@ -76,24 +67,33 @@ const Main = () => {
}, [config.Host, limit]); }, [config.Host, limit]);
useEffect(() => { useEffect(() => {
const lastPage = localStorage.getItem('lastPage') || 1; const lastPage = parseInt(localStorage.getItem('lastPage'), 10) || 1;
fetchMovies(currentCategory, lastPage); const lastCategory = localStorage.getItem('lastCategory') || 0;
}, [ currentCategory, fetchMovies]); fetchMovies(lastCategory, lastPage);
}, [fetchMovies]);
const handlePageChange = useCallback(async (event, page) => {
// console.log("handlePageChange");
fetchMovies(currentCategory, page);
}, [fetchMovies, currentCategory]);
const handleCategoryChange = useCallback(async (category) => {
// console.log("handleCategoryChange");
setCurrentCategory(category);
localStorage.setItem('lastCategory', category);
fetchMovies(category, 1);
}, [fetchMovies]);
const handlePageChange = (event, value) => {
fetchMovies(currentCategory, value);
};
return ( return (
<Container style={{ marginTop: 20 }}> <Container style={{ marginTop: 20 }}>
<CategoryNav <CategoryNav
categories={categories} categories={categories}
currentCategory={currentCategory} currentCategory={currentCategory}
onCategoryChange={handleCategoryChange} onCategoryChange={handleCategoryChange}
/> />
<div style={{ textAlign: 'center', marginBottom: 20 }}> <div style={{ textAlign: 'center', marginBottom: 20 }}>
<Pagination <Pagination
count={pagination.length} count={pagination.length}
page={pagination.page} page={pagination.page}
@ -102,37 +102,40 @@ const Main = () => {
color="primary" color="primary"
size="large" size="large"
/> />
</div> </div>
<div> <div>
{loading ? ( {loading ? (
<div <div
style={{display: 'flex',justifyContent: 'center',alignItems: 'center',height: '50vh'}} style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '50vh' }}
> >
<CircularProgress /> <CircularProgress />
</div> </div>
) : ( ) : (
<Grid container spacing={2} style={{ marginTop: 3 }}> <Grid container spacing={2} style={{ marginTop: 3 }}>
{pagination.movies.map((item) => ( {pagination.movies.map((item) => (
<Grid item xs={6} sm={4} md={3} lg={2} <Grid item xs={6} sm={4} md={3} lg={2}
style={{ display: 'flex', justifyContent: 'space-between' }} style={{ display: 'flex', justifyContent: 'space-between' }}
key={item.filename} key={item.filename}
>
<Link
to={`/res/${item.filename}`}
style={{ textDecoration: 'none', paddingBottom: 10 }}
> >
<MovieCard movie={item} config={config} /> <Link
</Link> to={`/res/${item.filename}`}
</Grid> style={{ textDecoration: 'none', paddingBottom: 10 }}
))} >
</Grid> <MovieCard movie={item} config={config} />
</Link>
</Grid>
))}
</Grid>
)} )}
</div> </div>
<div style={{ textAlign: 'center', marginTop: 20 }}> <div style={{ textAlign: 'center', marginBottom: 20 }}>
<Pagination <Pagination
count={pagination.length} count={pagination.length}
page={pagination.page} page={pagination.page}
onChange={handlePageChange} onChange={handlePageChange}
@ -140,6 +143,7 @@ const Main = () => {
color="primary" color="primary"
size="large" size="large"
/> />
</div> </div>
</Container> </Container>
); );

View File

@ -9,11 +9,7 @@ const MovieCard = ({ movie, config }) => {
? filename.substring(0, maxLength - 3) + '...' ? filename.substring(0, maxLength - 3) + '...'
: filename; : filename;
}; };
const theme = useTheme();
console.log(theme.shadows)
const StyledCard = styled(Card)({ const StyledCard = styled(Card)({
'&:hover': { '&:hover': {
transform: 'scale(1.05)', transform: 'scale(1.05)',