From aafd26d853cc7cb525f1810bc646dada0b62b4bc Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 9 Jul 2023 11:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.jsx | 106 ++++++++++++++++++----------------- src/components/MovieCard.jsx | 6 +- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/Main.jsx b/src/Main.jsx index ac70a9f..01a5a6a 100644 --- a/src/Main.jsx +++ b/src/Main.jsx @@ -10,17 +10,10 @@ import CircularProgress from '@mui/material/CircularProgress'; import ConfigContext from './Config'; import MovieCard from './components/MovieCard'; import CategoryNav from './components/CategoryNav'; - -import Select from '@mui/material/Select'; -import MenuItem from '@mui/material/MenuItem'; -import FormControl from '@mui/material/FormControl'; -import InputLabel from '@mui/material/InputLabel'; const Main = () => { const config = useContext(ConfigContext); - - const [currentCategory, setCurrentCategory] = useState(0); - + const [loading, setLoading] = useState(false); const categories = [ { label: '15min', idx: 0 }, { label: '30min', idx: 1 }, @@ -28,6 +21,12 @@ const Main = () => { { label: '大于60min', idx: 3 }, ]; + + const limit = 20; + + const [currentCategory, setCurrentCategory] = useState(parseInt(localStorage.getItem('lastCategory'), 10) || 0); + + const [pagination, setPagination] = useState({ movies: [], page: 1, @@ -35,26 +34,18 @@ const Main = () => { length: 1, }); - const [loading, setLoading] = useState(false); - - const limit = 20; - - const handleCategoryChange = (category) => { - setCurrentCategory(category); - fetchMovies(category, 1); - }; - const fetchMovies = useCallback(async (category, page) => { + setLoading(true); try { const response = await axios.get( `${config.Host}/movie/?page=${page}&limit=${limit}&category=${category}` ); - + if (response.status === 200) { const data = response.data.data; - + if (data.items.length === 0 && page > 1) { // 如果返回的数据为空且请求的页码大于1,则尝试获取上一页的数据 fetchMovies(category, page - 1); @@ -76,24 +67,33 @@ const Main = () => { }, [config.Host, limit]); useEffect(() => { - const lastPage = localStorage.getItem('lastPage') || 1; - fetchMovies(currentCategory, lastPage); - }, [ currentCategory, fetchMovies]); + const lastPage = parseInt(localStorage.getItem('lastPage'), 10) || 1; + const lastCategory = localStorage.getItem('lastCategory') || 0; + fetchMovies(lastCategory, lastPage); + }, [fetchMovies]); + + const handlePageChange = useCallback(async (event, page) => { + // console.log("handlePageChange"); + fetchMovies(currentCategory, page); + }, [fetchMovies, currentCategory]); + + const handleCategoryChange = useCallback(async (category) => { + // console.log("handleCategoryChange"); + setCurrentCategory(category); + localStorage.setItem('lastCategory', category); + fetchMovies(category, 1); + }, [fetchMovies]); - const handlePageChange = (event, value) => { - fetchMovies(currentCategory, value); - }; - - return ( - - + +
+ { color="primary" size="large" /> +
- +
{loading ? (
) : ( - + - {pagination.movies.map((item) => ( - - ( + - - - - ))} - + + + + + ))} + )}
- -
+ +
+ { color="primary" size="large" /> +
); diff --git a/src/components/MovieCard.jsx b/src/components/MovieCard.jsx index 4c213d2..c31242e 100644 --- a/src/components/MovieCard.jsx +++ b/src/components/MovieCard.jsx @@ -9,11 +9,7 @@ const MovieCard = ({ movie, config }) => { ? filename.substring(0, maxLength - 3) + '...' : filename; }; - - const theme = useTheme(); - console.log(theme.shadows) - - + const StyledCard = styled(Card)({ '&:hover': { transform: 'scale(1.05)',