2023-07-10 00:50:11 +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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-07-09 12:32:11 +08:00
|
|
|
const getInitialParams = () => {
|
|
|
|
|
const storedParams = localStorage.getItem('params');
|
|
|
|
|
if (storedParams) {
|
|
|
|
|
return JSON.parse(storedParams);
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
lastCategory: 0,
|
2023-07-10 00:50:11 +08:00
|
|
|
history: {
|
|
|
|
|
0: { lastPage: 1, scrollPos: 0 },
|
|
|
|
|
1: { lastPage: 1, scrollPos: 0 },
|
|
|
|
|
2: { lastPage: 1, scrollPos: 0 },
|
|
|
|
|
3: { lastPage: 1, scrollPos: 0 },
|
|
|
|
|
},
|
2023-07-09 12:32:11 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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: [],
|
2023-07-10 00:50:11 +08:00
|
|
|
page: params.history[params.lastCategory].lastPage,
|
2023-07-04 23:47:01 +08:00
|
|
|
total: 1,
|
|
|
|
|
length: 1,
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-09 12:32:11 +08:00
|
|
|
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),
|
|
|
|
|
});
|
|
|
|
|
}
|
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-10 00:50:11 +08:00
|
|
|
const currentCategory = params.lastCategory;
|
|
|
|
|
const currentPage = params.history[currentCategory].lastPage;
|
|
|
|
|
fetchMovies(currentCategory, currentPage);
|
|
|
|
|
}, [fetchMovies, params]);
|
2023-07-09 11:30:56 +08:00
|
|
|
|
2023-07-10 00:50:11 +08:00
|
|
|
const updateParams = useCallback((newParams) => {
|
2023-07-09 12:32:11 +08:00
|
|
|
setParams((prevParams) => {
|
2023-07-10 00:50:11 +08:00
|
|
|
const updatedParams = { ...prevParams, ...newParams };
|
|
|
|
|
localStorage.setItem('params', JSON.stringify(updatedParams));
|
|
|
|
|
return updatedParams;
|
2023-07-09 12:32:11 +08:00
|
|
|
});
|
2023-07-10 00:50:11 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handlePageChange = useCallback(
|
|
|
|
|
(event, page) => {
|
|
|
|
|
const currentCategory = params.lastCategory;
|
|
|
|
|
updateParams({
|
|
|
|
|
history: {
|
|
|
|
|
...params.history,
|
|
|
|
|
[currentCategory]: { ...params.history[currentCategory], lastPage: page },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
fetchMovies(currentCategory, page);
|
|
|
|
|
},
|
|
|
|
|
[fetchMovies, params, updateParams]
|
|
|
|
|
);
|
2023-07-09 12:32:11 +08:00
|
|
|
|
2023-07-10 00:50:11 +08:00
|
|
|
// 导致递归刷新
|
|
|
|
|
const handleCategoryChange = useCallback(
|
|
|
|
|
(category) => {
|
|
|
|
|
const currentPage = params.history[category].lastPage;
|
|
|
|
|
fetchMovies(category, currentPage);
|
2023-07-09 12:32:11 +08:00
|
|
|
|
2023-07-10 00:50:11 +08:00
|
|
|
const currentCategory = params.lastCategory;
|
|
|
|
|
updateParams({
|
2023-07-09 12:32:11 +08:00
|
|
|
lastCategory: category,
|
2023-07-10 00:50:11 +08:00
|
|
|
history: {
|
|
|
|
|
...params.history,
|
|
|
|
|
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[fetchMovies, params, updateParams]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleRenderComplete = useCallback(() => {
|
|
|
|
|
const { scrollPos } = params.history[params.lastCategory];
|
|
|
|
|
window.scrollTo(0, scrollPos);
|
|
|
|
|
}, [params]);
|
|
|
|
|
|
|
|
|
|
const handleMovieCardClick = () => {
|
|
|
|
|
const currentCategory = params.lastCategory;
|
|
|
|
|
updateParams({
|
|
|
|
|
history: {
|
|
|
|
|
...params.history,
|
|
|
|
|
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
|
|
|
|
|
},
|
2023-07-09 12:32:11 +08:00
|
|
|
});
|
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
|
|
|
|
|
categories={categories}
|
2023-07-09 12:32:11 +08:00
|
|
|
currentCategory={params.lastCategory}
|
2023-07-09 11:30:56 +08:00
|
|
|
onCategoryChange={handleCategoryChange}
|
|
|
|
|
/>
|
|
|
|
|
|
2023-07-09 12:32:11 +08:00
|
|
|
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
|
|
|
|
|
|
|
|
|
|
{loading ? (
|
|
|
|
|
<Loading />
|
|
|
|
|
) : (
|
2023-07-10 00:50:11 +08:00
|
|
|
<MoviesGrid
|
|
|
|
|
movies={pagination.movies}
|
|
|
|
|
config={config}
|
|
|
|
|
onRenderComplete={handleRenderComplete}
|
|
|
|
|
onMovieCardClick={handleMovieCardClick}
|
|
|
|
|
/>
|
2023-07-09 12:32:11 +08:00
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
|
2023-07-04 23:47:01 +08:00
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-09 12:32:11 +08:00
|
|
|
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>
|
2023-07-09 12:32:11 +08:00
|
|
|
);
|
2023-07-10 00:50:11 +08:00
|
|
|
|
2023-07-09 12:32:11 +08:00
|
|
|
|
2023-07-10 00:50:11 +08:00
|
|
|
const MoviesGrid = ({ movies, config, onRenderComplete, onMovieCardClick }) => {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onRenderComplete();
|
|
|
|
|
}, [movies, onRenderComplete]);
|
2023-07-09 12:32:11 +08:00
|
|
|
|
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-09 12:32:11 +08:00
|
|
|
>
|
2023-07-10 00:50:11 +08:00
|
|
|
<Link
|
|
|
|
|
to={`/res/${item.filename}`}
|
|
|
|
|
style={{ textDecoration: 'none', paddingBottom: 10 }}
|
|
|
|
|
onClick={onMovieCardClick} // 修改这里
|
|
|
|
|
>
|
|
|
|
|
<MovieCard movie={item} config={config} />
|
|
|
|
|
</Link>
|
|
|
|
|
</Grid>
|
|
|
|
|
))}
|
|
|
|
|
</Grid>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Loading = () => (
|
|
|
|
|
<Grid container justifyContent="center" style={{ marginTop: 20 }}>
|
|
|
|
|
<CircularProgress />
|
2023-07-09 12:32:11 +08:00
|
|
|
</Grid>
|
|
|
|
|
);
|
|
|
|
|
|
2023-07-04 23:47:01 +08:00
|
|
|
export default Main;
|