Commit d2ad01e1 authored by Ken's avatar Ken

enhance avoid increasing pages when no data

parent 49667e35
......@@ -40,7 +40,7 @@
&__desc {
line-height: 22px;
font-size: 14px;
font-size: 18px;
color: $neutral-900;
display: block;
}
......
......@@ -2,8 +2,9 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import newsApi from "api/newsApi";
import { INewspaper, IPagination } from "pages/interface";
const initialState: { newsData: INewspaper[] } = {
const initialState: { newsData: INewspaper[]; currentPage: number } = {
newsData: [],
currentPage: 0,
};
export const getNews = createAsyncThunk(
......@@ -19,17 +20,24 @@ const home = createSlice({
initialState,
reducers: {
handleResetNews: (state) => {
state.newsData = [];
return initialState;
},
loadPages: (state) => {
state.currentPage++;
},
},
extraReducers: (builder) => {
builder.addCase(getNews.fulfilled, (state, action: PayloadAction<any>) => {
const { collection } = action.payload.data;
if (collection.length > 0) {
state.currentPage++;
state.newsData = [...state.newsData, ...collection];
}
});
},
});
const { reducer, actions } = home;
export const { handleResetNews } = actions;
export const { handleResetNews, loadPages } = actions;
export default reducer;