x-movie/src/Components.js

153 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-07-08 03:15:48 +08:00
import React, { useState, useEffect, useContext } from 'react';
2023-07-04 23:47:01 +08: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-05 01:33:55 +08:00
import CircularProgress from '@mui/material/CircularProgress';
2023-07-08 03:15:48 +08:00
import ConfigContext from './Config';
2023-07-04 23:47:01 +08:00
2023-07-08 03:15:48 +08:00
2023-07-04 23:47:01 +08:00
const Main = () => {
2023-07-08 03:15:48 +08:00
const config = useContext(ConfigContext);
2023-07-04 23:47:01 +08:00
const [pagination, setPagination] = useState({
movies: [],
page: 1,
total: 1,
length: 1,
});
2023-07-05 01:33:55 +08:00
const [loading, setLoading] = useState(false);
2023-07-08 03:15:48 +08:00
const limit = 12;
2023-07-04 23:47:01 +08:00
const fetchMovies = async (page) => {
2023-07-05 01:33:55 +08:00
setLoading(true);
2023-07-04 23:47:01 +08:00
try {
const response = await axios.get(
`${config.Host}/movie/?page=${page}&limit=${limit}`
);
if (response.status === 200) {
const data = response.data.data;
setPagination({
movies: data.items,
page: page,
total: data.total,
length: Math.ceil(data.total / limit),
});
localStorage.setItem('lastPage', page);
}
} catch (error) {
console.error('Error fetching movies:', error);
2023-07-05 01:33:55 +08:00
} finally {
setLoading(false);
2023-07-04 23:47:01 +08:00
}
};
useEffect(() => {
const lastPage = localStorage.getItem('lastPage') || 1;
fetchMovies(lastPage);
}, []);
const handlePageChange = (event, value) => {
fetchMovies(value);
};
2023-07-05 01:33:55 +08:00
const truncateFilename = (filename, maxLength) => {
return filename.length > maxLength
? filename.substring(0, maxLength - 3) + '...'
: filename;
};
2023-07-04 23:47:01 +08:00
return (
2023-07-08 03:15:48 +08:00
<Container style={{ marginTop: 20 }}>
<div style={{ textAlign: 'center', marginBottom: 20 }}>
<Pagination
count={pagination.length}
page={pagination.page}
onChange={handlePageChange}
shape="rounded"
color="primary"
size="large"
/>
</div>
2023-07-04 23:47:01 +08:00
<div>
2023-07-05 01:33:55 +08:00
{loading ? (
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '50vh',
}}
>
<CircularProgress />
</div>
) : (
2023-07-08 03:15:48 +08:00
<Grid container spacing={2} style={{ marginTop: 3 }}>
2023-07-05 01:33:55 +08:00
{pagination.movies.map((item) => (
<Grid
item
xs={6}
sm={4}
md={3}
lg={2}
style={{ display: 'flex', justifyContent: 'space-between' }}
key={item.filename}
>
2023-07-08 03:15:48 +08:00
<Link
to={`/res/${item.filename}`}
style={{ textDecoration: 'none', paddingBottom: 10 }}
>
2023-07-05 01:33:55 +08:00
<Card
style={{
width: '100%',
height: 200,
boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)',
transition: 'box-shadow 0.3s ease-in-out',
'&:hover': {
boxShadow: '0 8px 12px rgba(0, 0, 0, 0.2)',
},
}}
>
<CardMedia
component="img"
height="120"
image={`${config.Host}/res/${item.image}`}
/>
<CardContent>
<Typography>
2023-07-08 03:15:48 +08:00
{truncateFilename(item.filename, 15)}
2023-07-05 01:33:55 +08:00
</Typography>
</CardContent>
</Card>
</Link>
</Grid>
))}
</Grid>
)}
2023-07-04 23:47:01 +08:00
</div>
2023-07-08 03:15:48 +08:00
<div style={{ textAlign: 'center', marginTop: 20 }}>
2023-07-04 23:47:01 +08:00
<Pagination
count={pagination.length}
page={pagination.page}
onChange={handlePageChange}
shape="rounded"
2023-07-05 01:33:55 +08:00
color="primary"
size="large"
2023-07-04 23:47:01 +08:00
/>
</div>
</Container>
);
};
export default Main;