x-movie/src/Main.jsx

198 lines
5.9 KiB
React
Raw Normal View History

2023-07-10 11:41:17 +08:00
import React, { useState, useEffect, useContext, useCallback, useReducer } 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
2023-07-10 11:41:17 +08:00
const initialState = {
lastCategory: 0,
history: {
0: { lastPage: 1, scrollPos: 0 },
1: { lastPage: 1, scrollPos: 0 },
2: { lastPage: 1, scrollPos: 0 },
3: { lastPage: 1, scrollPos: 0 },
},
pagination: {
movies: [],
page: 1,
total: 1,
length: 1,
},
loading: false,
};
function reducer(state, action) {
switch (action.type) {
case 'UPDATE_LAST_CATEGORY':
return { ...state, lastCategory: action.payload.lastCategory };
case 'UPDATE_HISTORY':
return {
2023-07-10 11:41:17 +08:00
...state,
2023-07-10 00:50:11 +08:00
history: {
2023-07-10 11:41:17 +08:00
...state.history,
[action.payload.category]: action.payload.newData,
2023-07-10 00:50:11 +08:00
},
};
2023-07-10 11:41:17 +08:00
case 'SET_PAGINATION':
return { ...state, pagination: action.payload.pagination };
case 'SET_LOADING':
return { ...state, loading: action.payload.loading };
default:
return state;
}
}
2023-07-10 11:41:17 +08:00
const Main = () => {
const config = useContext(ConfigContext);
2023-07-09 11:30:56 +08:00
2023-07-10 11:41:17 +08:00
const [state, dispatch] = useReducer(reducer, initialState);
const updateLastCategory = useCallback((newCategory) => {
dispatch({ type: 'UPDATE_LAST_CATEGORY', payload: { lastCategory: newCategory } });
}, []);
const updateHistory = useCallback((category, newData) => {
dispatch({ type: 'UPDATE_HISTORY', payload: { category, newData } });
}, []);
2023-07-04 23:47:01 +08:00
const fetchMovies = useCallback(async (category, page) => {
2023-07-10 11:41:17 +08:00
dispatch({ type: 'SET_LOADING', payload: { loading: true } });
2023-07-04 23:47:01 +08:00
try {
const response = await axios.get(
2023-07-10 11:41:17 +08:00
`${config.Host}/movie/?page=${page}&limit=${config.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 {
2023-07-10 11:41:17 +08:00
dispatch({
type: 'SET_PAGINATION',
payload: {
pagination: {
movies: data.items,
page: page,
total: data.total,
length: Math.ceil(data.total / config.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 {
2023-07-10 11:41:17 +08:00
dispatch({ type: 'SET_LOADING', payload: { loading: false } });
2023-07-04 23:47:01 +08:00
}
2023-07-10 11:41:17 +08:00
}, [config.Host, config.limit]); // 修改这里
2023-07-04 23:47:01 +08:00
useEffect(() => {
2023-07-10 11:41:17 +08:00
const currentCategory = state.lastCategory;
const currentPage = state.history[currentCategory].lastPage;
2023-07-10 00:50:11 +08:00
fetchMovies(currentCategory, currentPage);
2023-07-10 11:41:17 +08:00
}, [fetchMovies, state]);
2023-07-10 00:50:11 +08:00
const handlePageChange = useCallback(
(event, page) => {
2023-07-10 11:41:17 +08:00
const currentCategory = state.lastCategory;
updateHistory(currentCategory, { ...state.history[currentCategory], lastPage: page });
2023-07-10 00:50:11 +08:00
fetchMovies(currentCategory, page);
},
2023-07-10 11:41:17 +08:00
[fetchMovies, state, updateHistory]
2023-07-10 00:50:11 +08:00
);
2023-07-10 00:50:11 +08:00
const handleCategoryChange = useCallback(
(category) => {
2023-07-10 11:41:17 +08:00
const currentPage = state.history[category].lastPage;
2023-07-10 00:50:11 +08:00
fetchMovies(category, currentPage);
2023-07-10 11:41:17 +08:00
const currentCategory = state.lastCategory;
updateHistory(currentCategory, { ...state.history[currentCategory], scrollPos: window.scrollY });
updateLastCategory(category);
2023-07-10 00:50:11 +08:00
},
2023-07-10 11:41:17 +08:00
[fetchMovies, state, updateHistory, updateLastCategory]
2023-07-10 00:50:11 +08:00
);
const handleRenderComplete = useCallback(() => {
2023-07-10 11:41:17 +08:00
const { scrollPos } = state.history[state.lastCategory];
2023-07-10 00:50:11 +08:00
window.scrollTo(0, scrollPos);
2023-07-10 11:41:17 +08:00
}, [state]);
2023-07-10 00:50:11 +08:00
const handleMovieCardClick = () => {
2023-07-10 11:41:17 +08:00
const currentCategory = state.lastCategory;
updateHistory(currentCategory, { ...state.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
2023-07-10 11:41:17 +08:00
categories={config.categories} // 修改这里
currentCategory={state.lastCategory} // 修改这里
2023-07-09 11:30:56 +08:00
onCategoryChange={handleCategoryChange}
/>
2023-07-10 11:41:17 +08:00
<PaginationWrapper page={state.pagination.page} length={state.pagination.length} onPageChange={handlePageChange} />
2023-07-10 11:41:17 +08:00
{state.loading ? (
<Loading />
) : (
2023-07-10 00:50:11 +08:00
<MoviesGrid
2023-07-10 11:41:17 +08:00
movies={state.pagination.movies}
2023-07-10 00:50:11 +08:00
config={config}
onRenderComplete={handleRenderComplete}
2023-07-10 11:41:17 +08:00
onMovieCardClick={handleMovieCardClick}
2023-07-10 00:50:11 +08:00
/>
)}
2023-07-10 11:41:17 +08:00
<PaginationWrapper page={state.pagination.page} length={state.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>
);
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 }}
2023-07-10 11:41:17 +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;