x-movie/src/Main.jsx

181 lines
4.7 KiB
React
Raw Normal View History

import React, { useState, useEffect, useContext, useCallback, useRef} 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 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-04 23:47:01 +08:00
2023-07-08 17:44:51 +08:00
import ConfigContext from './Config';
2023-07-09 05:58:33 +08:00
import MovieCard from './components/MovieCard';
import CategoryNav from './components/CategoryNav';
2023-07-04 23:47:01 +08:00
const Main = () => {
2023-07-08 03:15:48 +08:00
const config = useContext(ConfigContext);
2023-07-09 11:30:56 +08:00
const [loading, setLoading] = useState(false);
2023-07-08 17:44:51 +08:00
const categories = [
{ label: '15min', idx: 0 },
{ label: '30min', idx: 1 },
{ label: '60min', idx: 2 },
{ label: '大于60min', idx: 3 },
];
2023-07-09 11:30:56 +08:00
const limit = 20;
const getInitialParams = () => {
const storedParams = localStorage.getItem('params');
if (storedParams) {
return JSON.parse(storedParams);
} else {
return {
lastPage: {
0: 1,
1: 1,
2: 1,
3: 1,
},
lastCategory: 0,
};
}
};
const [params, setParams] = useState(getInitialParams());
2023-07-09 11:30:56 +08:00
2023-07-04 23:47:01 +08:00
const [pagination, setPagination] = useState({
movies: [],
page: params.lastPage[params.lastCategory],
2023-07-04 23:47:01 +08:00
total: 1,
length: 1,
});
2023-07-09 11:30:56 +08:00
const paramsRef = useRef(params);
paramsRef.current = params;
const fetchMovies = useCallback(async (category, 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(
2023-07-08 17:44:51 +08:00
`${config.Host}/movie/?page=${page}&limit=${limit}&category=${category}`
2023-07-04 23:47:01 +08:00
);
2023-07-09 11:30:56 +08:00
2023-07-04 23:47:01 +08:00
if (response.status === 200) {
const data = response.data.data;
2023-07-09 11:30:56 +08:00
2023-07-08 17:44:51 +08:00
if (data.items.length === 0 && page > 1) {
2023-07-09 05:58:33 +08:00
fetchMovies(category, page - 1);
2023-07-08 17:44:51 +08:00
} else {
setPagination({
movies: data.items,
page: page,
total: data.total,
length: Math.ceil(data.total / limit),
});
updateParams(category, page);
2023-07-08 17:44:51 +08:00
}
2023-07-04 23:47:01 +08:00
}
} 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
}
2023-07-09 05:58:33 +08:00
}, [config.Host, limit]);
2023-07-04 23:47:01 +08:00
useEffect(() => {
2023-07-09 11:30:56 +08:00
fetchMovies(paramsRef.current.lastCategory, paramsRef.current.lastPage[paramsRef.current.lastCategory]);
}, [fetchMovies]);
2023-07-09 11:30:56 +08:00
const updateParams = (category, page) => {
setParams((prevParams) => {
const newParams = {
...prevParams,
lastPage: {
...prevParams.lastPage,
[category]: page,
},
};
localStorage.setItem('params', JSON.stringify(newParams));
return newParams;
});
};
const handlePageChange = useCallback((event, page) => {
fetchMovies(params.lastCategory, page);
updateParams(params.lastCategory, page);
}, [fetchMovies, params]);
const handleCategoryChange = useCallback((category) => {
setParams((prevParams) => {
const newParams = {
...prevParams,
lastCategory: category,
};
localStorage.setItem('params', JSON.stringify(newParams));
return newParams;
});
// 从 paramsRef.current 获取当前 category 对应的页面
fetchMovies(category, paramsRef.current.lastPage[category]);
2023-07-09 11:30:56 +08:00
}, [fetchMovies]);
2023-07-04 23:47:01 +08:00
return (
2023-07-08 03:15:48 +08:00
<Container style={{ marginTop: 20 }}>
2023-07-09 11:30:56 +08:00
<CategoryNav
categories={categories}
currentCategory={params.lastCategory}
2023-07-09 11:30:56 +08:00
onCategoryChange={handleCategoryChange}
/>
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
{loading ? (
<Loading />
) : (
<MoviesGrid movies={pagination.movies} config={config} />
)}
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
2023-07-04 23:47:01 +08:00
</Container>
);
};
const PaginationWrapper = ({ page, length, onPageChange }) => (
<div style={{ textAlign: 'center', marginBottom: 20 }}>
<Pagination
count={length}
page={page}
onChange={onPageChange}
shape="rounded"
color="primary"
size="large"
/>
</div>
);
const Loading = () => (
<div
style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '50vh' }}
>
<CircularProgress />
</div>
);
const MoviesGrid = ({ movies, config }) => (
<Grid container spacing={2} style={{ marginTop: 3 }}>
{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 }}
>
<MovieCard movie={item} config={config} />
</Link>
</Grid>
))}
</Grid>
);
2023-07-04 23:47:01 +08:00
export default Main;