Commit d2ad01e1 authored by Ken's avatar Ken

enhance avoid increasing pages when no data

parent 49667e35
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
&__desc { &__desc {
line-height: 22px; line-height: 22px;
font-size: 14px; font-size: 18px;
color: $neutral-900; color: $neutral-900;
display: block; display: block;
} }
......
...@@ -2,8 +2,9 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"; ...@@ -2,8 +2,9 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import newsApi from "api/newsApi"; import newsApi from "api/newsApi";
import { INewspaper, IPagination } from "pages/interface"; import { INewspaper, IPagination } from "pages/interface";
const initialState: { newsData: INewspaper[] } = { const initialState: { newsData: INewspaper[]; currentPage: number } = {
newsData: [], newsData: [],
currentPage: 0,
}; };
export const getNews = createAsyncThunk( export const getNews = createAsyncThunk(
...@@ -19,17 +20,24 @@ const home = createSlice({ ...@@ -19,17 +20,24 @@ const home = createSlice({
initialState, initialState,
reducers: { reducers: {
handleResetNews: (state) => { handleResetNews: (state) => {
state.newsData = []; return initialState;
},
loadPages: (state) => {
state.currentPage++;
}, },
}, },
extraReducers: (builder) => { extraReducers: (builder) => {
builder.addCase(getNews.fulfilled, (state, action: PayloadAction<any>) => { builder.addCase(getNews.fulfilled, (state, action: PayloadAction<any>) => {
const { collection } = action.payload.data; const { collection } = action.payload.data;
if (collection.length > 0) {
state.currentPage++;
state.newsData = [...state.newsData, ...collection]; state.newsData = [...state.newsData, ...collection];
}
}); });
}, },
}); });
const { reducer, actions } = home; const { reducer, actions } = home;
export const { handleResetNews } = actions; export const { handleResetNews, loadPages } = actions;
export default reducer; export default reducer;
...@@ -10,7 +10,7 @@ import { CircularProgress } from "@mui/material"; ...@@ -10,7 +10,7 @@ import { CircularProgress } from "@mui/material";
const HomePage = () => { const HomePage = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { newsData } = useAppSelector(homeSelector); const { newsData, currentPage } = useAppSelector(homeSelector);
const { ref, inView } = useInView({ const { ref, inView } = useInView({
threshold: 0.7, threshold: 0.7,
initialInView: false, initialInView: false,
...@@ -21,6 +21,7 @@ const HomePage = () => { ...@@ -21,6 +21,7 @@ const HomePage = () => {
useEffect(() => { useEffect(() => {
try { try {
if (page > currentPage) {
setIsLocalLoading(true); setIsLocalLoading(true);
setTimeout(() => { setTimeout(() => {
const newsParams: IPagination = { const newsParams: IPagination = {
...@@ -30,12 +31,17 @@ const HomePage = () => { ...@@ -30,12 +31,17 @@ const HomePage = () => {
PageSize: 10, PageSize: 10,
}; };
const fetchData = async () => { const fetchData = async () => {
await dispatch(getNews(newsParams)); const res: any = await dispatch(getNews(newsParams)).unwrap();
if (res.data.collection.length === 0) {
setPage((page) => page - 1);
}
}; };
fetchData(); fetchData();
setIsLocalLoading(false); setIsLocalLoading(false);
}, 1000); }, 1000);
}
} catch (err) { } catch (err) {
console.error("ERROR: ", err); console.error("ERROR: ", err);
setIsLocalLoading(false); setIsLocalLoading(false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment