x-movie/src/Main.jsx

204 lines
5.7 KiB
React
Raw Normal View History

2024-03-28 03:40:13 +08:00
import React, { useState, useEffect, useContext, useCallback } 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
2024-03-28 03:40:13 +08:00
const Main = () => {
const config = useContext(ConfigContext);
const [loading, setLoading] = useState(false);
const categories = [
{ label: '15min', idx: 0 },
{ label: '30min', idx: 1 },
{ label: '60min', idx: 2 },
{ label: '大于60min', idx: 3 },
];
const limit = 20;
const getInitialParams = () => {
const storedParams = localStorage.getItem('params');
if (storedParams) {
return JSON.parse(storedParams);
} else {
return {
2024-03-28 03:40:13 +08:00
lastCategory: 0,
2023-07-10 00:50:11 +08:00
history: {
2024-03-28 03:40:13 +08:00
0: { lastPage: 1, scrollPos: 0 },
1: { lastPage: 1, scrollPos: 0 },
2: { lastPage: 1, scrollPos: 0 },
3: { lastPage: 1, scrollPos: 0 },
2023-07-10 00:50:11 +08:00
},
};
2024-03-28 03:40:13 +08:00
}
};
2023-07-10 11:41:17 +08:00
2024-03-28 03:40:13 +08:00
const [params, setParams] = useState(getInitialParams());
2023-07-10 11:41:17 +08:00
2024-03-28 03:40:13 +08:00
const [pagination, setPagination] = useState({
movies: [],
page: params.history[params.lastCategory].lastPage,
total: 1,
length: 1,
});
2023-07-04 23:47:01 +08:00
const fetchMovies = useCallback(async (category, page) => {
2024-03-28 03:40:13 +08:00
setLoading(true);
2023-07-04 23:47:01 +08:00
try {
const response = await axios.get(
2024-03-28 03:40:13 +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 {
2024-03-28 03:40:13 +08:00
setPagination({
movies: data.items,
page: page,
total: data.total,
length: Math.ceil(data.total / limit),
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 {
2024-03-28 03:40:13 +08:00
setLoading(false);
2023-07-04 23:47:01 +08:00
}
2024-03-28 03:40:13 +08:00
}, [config.Host, limit]);
2023-07-04 23:47:01 +08:00
useEffect(() => {
2024-03-28 03:40:13 +08:00
const currentCategory = params.lastCategory;
const currentPage = params.history[currentCategory].lastPage;
2023-07-10 00:50:11 +08:00
fetchMovies(currentCategory, currentPage);
2024-03-28 03:40:13 +08:00
}, [fetchMovies, params]);
const updateParams = useCallback((newParams) => {
setParams((prevParams) => {
const updatedParams = { ...prevParams, ...newParams };
localStorage.setItem('params', JSON.stringify(updatedParams));
return updatedParams;
});
}, []);
2023-07-10 00:50:11 +08:00
const handlePageChange = useCallback(
(event, page) => {
2024-03-28 03:40:13 +08:00
const currentCategory = params.lastCategory;
updateParams({
history: {
...params.history,
[currentCategory]: { ...params.history[currentCategory], lastPage: page },
},
});
2023-07-10 00:50:11 +08:00
fetchMovies(currentCategory, page);
},
2024-03-28 03:40:13 +08:00
[fetchMovies, params, updateParams]
2023-07-10 00:50:11 +08:00
);
2024-03-28 03:40:13 +08:00
// 导致递归刷新
2023-07-10 00:50:11 +08:00
const handleCategoryChange = useCallback(
(category) => {
2024-03-28 03:40:13 +08:00
const currentPage = params.history[category].lastPage;
2023-07-10 00:50:11 +08:00
fetchMovies(category, currentPage);
2024-03-28 03:40:13 +08:00
const currentCategory = params.lastCategory;
updateParams({
lastCategory: category,
history: {
...params.history,
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
},
});
2023-07-10 00:50:11 +08:00
},
2024-03-28 03:40:13 +08:00
[fetchMovies, params, updateParams]
2023-07-10 00:50:11 +08:00
);
const handleRenderComplete = useCallback(() => {
2024-03-28 03:40:13 +08:00
const { scrollPos } = params.history[params.lastCategory];
2023-07-10 00:50:11 +08:00
window.scrollTo(0, scrollPos);
2024-03-28 03:40:13 +08:00
}, [params]);
2023-07-10 00:50:11 +08:00
const handleMovieCardClick = () => {
2024-03-28 03:40:13 +08:00
const currentCategory = params.lastCategory;
updateParams({
history: {
...params.history,
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
},
});
2023-07-10 00:50:11 +08:00
};
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
2024-03-28 03:40:13 +08:00
categories={categories}
currentCategory={params.lastCategory}
2023-07-09 11:30:56 +08:00
onCategoryChange={handleCategoryChange}
/>
2024-03-28 03:40:13 +08:00
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
2024-03-28 03:40:13 +08:00
{loading ? (
<Loading />
) : (
2023-07-10 00:50:11 +08:00
<MoviesGrid
2024-03-28 03:40:13 +08:00
movies={pagination.movies}
2023-07-10 00:50:11 +08:00
config={config}
onRenderComplete={handleRenderComplete}
2024-03-28 03:40:13 +08:00
onMovieCardClick={handleMovieCardClick}
2023-07-10 00:50:11 +08:00
/>
)}
2024-03-28 03:40:13 +08:00
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
2023-07-04 23:47:01 +08:00
</Container>
);
};
const PaginationWrapper = ({ page, length, onPageChange }) => (
2023-07-10 00:50:11 +08:00
<Grid container justifyContent="center" style={{ marginTop: 20, marginBottom: 20 }}>
<Pagination count={length} page={page} onChange={onPageChange} />
</Grid>
);
2024-03-28 03:40:13 +08:00
2023-07-10 00:50:11 +08:00
const MoviesGrid = ({ movies, config, onRenderComplete, onMovieCardClick }) => {
useEffect(() => {
onRenderComplete();
}, [movies, onRenderComplete]);
2023-07-10 00:50:11 +08:00
return (
<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}
>
2023-07-10 00:50:11 +08:00
<Link
to={`/res/${item.filename}`}
style={{ textDecoration: 'none', paddingBottom: 10 }}
2024-03-28 03:40:13 +08:00
onClick={onMovieCardClick} // 修改这里
2023-07-10 00:50:11 +08:00
>
<MovieCard movie={item} config={config} />
</Link>
</Grid>
))}
</Grid>
);
};
const Loading = () => (
<Grid container justifyContent="center" style={{ marginTop: 20 }}>
<CircularProgress />
</Grid>
);
2023-07-04 23:47:01 +08:00
export default Main;