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;
state.newsData = [...state.newsData, ...collection];
if (collection.length > 0) {
state.currentPage++;
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,21 +21,27 @@ const HomePage = () => { ...@@ -21,21 +21,27 @@ const HomePage = () => {
useEffect(() => { useEffect(() => {
try { try {
setIsLocalLoading(true); if (page > currentPage) {
setTimeout(() => { setIsLocalLoading(true);
const newsParams: IPagination = { setTimeout(() => {
Filters: "", const newsParams: IPagination = {
Sorts: "", Filters: "",
Page: page, Sorts: "",
PageSize: 10, Page: page,
}; PageSize: 10,
const fetchData = async () => { };
await dispatch(getNews(newsParams)); const fetchData = async () => {
}; const res: any = await dispatch(getNews(newsParams)).unwrap();
fetchData(); if (res.data.collection.length === 0) {
setIsLocalLoading(false); setPage((page) => page - 1);
}, 1000); }
};
fetchData();
setIsLocalLoading(false);
}, 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