x-movie/src/Main.js

165 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-07-08 19:00:29 +00:00
// Main.js
2023-07-07 19:15:48 +00:00
import React, { useState, useEffect, useContext } from 'react';
2023-07-04 15:47:01 +00:00
import axios from 'axios';
import Container from '@mui/material/Container';
import Grid from '@mui/material/Grid';
import Card from '@mui/material/Card';
import CardMedia from '@mui/material/CardMedia';
import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import Pagination from '@mui/material/Pagination';
import { Link } from 'react-router-dom';
2023-07-04 17:33:55 +00:00
import CircularProgress from '@mui/material/CircularProgress';
2023-07-04 15:47:01 +00:00
2023-07-08 09:44:51 +00:00
import ConfigContext from './Config';
import MovieCard from './MovieCard';
import CategoryNav from './CategoryNav';
2023-07-07 19:15:48 +00:00
2023-07-04 15:47:01 +00:00
const Main = () => {
2023-07-07 19:15:48 +00:00
const config = useContext(ConfigContext);
2023-07-08 09:44:51 +00:00
const [currentCategory, setCurrentCategory] = useState(0);
const categories = [
{ label: '15min', idx: 0 },
{ label: '30min', idx: 1 },
{ label: '60min', idx: 2 },
{ label: '大于60min', idx: 3 },
];
2023-07-04 15:47:01 +00:00
const [pagination, setPagination] = useState({
movies: [],
page: 1,
total: 1,
length: 1,
});
2023-07-04 17:33:55 +00:00
const [loading, setLoading] = useState(false);
2023-07-08 09:44:51 +00:00
const limit = 20;
2023-07-04 15:47:01 +00:00
2023-07-08 09:44:51 +00:00
const handleCategoryChange = (category) => {
setCurrentCategory(category);
fetchMovies(category, 1);
};
const fetchMovies = async (category, page) => {
2023-07-04 17:33:55 +00:00
setLoading(true);
2023-07-04 15:47:01 +00:00
try {
const response = await axios.get(
2023-07-08 09:44:51 +00:00
`${config.Host}/movie/?page=${page}&limit=${limit}&category=${category}`
2023-07-04 15:47:01 +00:00
);
2023-07-08 09:44:51 +00:00
2023-07-04 15:47:01 +00:00
if (response.status === 200) {
const data = response.data.data;
2023-07-08 19:00:29 +00:00
2023-07-08 09:44:51 +00:00
if (data.items.length === 0 && page > 1) {
// 如果返回的数据为空且请求的页码大于1则尝试获取上一页的数据
fetchMovies(page - 1);
} else {
setPagination({
movies: data.items,
page: page,
total: data.total,
length: Math.ceil(data.total / limit),
});
localStorage.setItem('lastPage', page);
}
2023-07-04 15:47:01 +00:00
}
} catch (error) {
console.error('Error fetching movies:', error);
2023-07-04 17:33:55 +00:00
} finally {
setLoading(false);
2023-07-04 15:47:01 +00:00
}
};
useEffect(() => {
const lastPage = localStorage.getItem('lastPage') || 1;
2023-07-08 09:44:51 +00:00
fetchMovies(currentCategory, lastPage);
2023-07-04 15:47:01 +00:00
}, []);
const handlePageChange = (event, value) => {
2023-07-08 09:44:51 +00:00
fetchMovies(currentCategory, value);
2023-07-04 15:47:01 +00:00
};
2023-07-04 17:33:55 +00:00
const truncateFilename = (filename, maxLength) => {
return filename.length > maxLength
? filename.substring(0, maxLength - 3) + '...'
: filename;
};
2023-07-08 09:44:51 +00:00
2023-07-04 17:33:55 +00:00
2023-07-04 15:47:01 +00:00
return (
2023-07-07 19:15:48 +00:00
<Container style={{ marginTop: 20 }}>
2023-07-08 19:00:29 +00:00
<CategoryNav
2023-07-08 09:44:51 +00:00
categories={categories}
currentCategory={currentCategory}
onCategoryChange={handleCategoryChange}
/>
2023-07-08 19:00:29 +00:00
2023-07-07 19:15:48 +00:00
<div style={{ textAlign: 'center', marginBottom: 20 }}>
<Pagination
count={pagination.length}
page={pagination.page}
onChange={handlePageChange}
shape="rounded"
color="primary"
size="large"
/>
</div>
2023-07-08 09:44:51 +00:00
2023-07-04 15:47:01 +00:00
<div>
2023-07-04 17:33:55 +00:00
{loading ? (
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '50vh',
}}
>
<CircularProgress />
</div>
) : (
2023-07-08 09:44:51 +00:00
2023-07-07 19:15:48 +00:00
<Grid container spacing={2} style={{ marginTop: 3 }}>
2023-07-08 09:44:51 +00:00
{pagination.movies.map((item) => (
<Grid
item
xs={6}
sm={4}
md={3}
lg={2}
style={{ display: 'flex', justifyContent: 'space-between' }}
key={item.filename}
>
<Link
to={`/res/${item.filename}`}
style={{ textDecoration: 'none', paddingBottom: 10 }}
2023-07-04 17:33:55 +00:00
>
2023-07-08 09:44:51 +00:00
<MovieCard movie={item} config={config} />
</Link>
</Grid>
))}
</Grid>
2023-07-04 17:33:55 +00:00
)}
2023-07-04 15:47:01 +00:00
</div>
2023-07-08 19:00:29 +00:00
2023-07-07 19:15:48 +00:00
<div style={{ textAlign: 'center', marginTop: 20 }}>
2023-07-04 15:47:01 +00:00
<Pagination
count={pagination.length}
page={pagination.page}
onChange={handlePageChange}
shape="rounded"
2023-07-04 17:33:55 +00:00
color="primary"
size="large"
2023-07-04 15:47:01 +00:00
/>
</div>
</Container>
);
};
export default Main;