Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
Newspaper Frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lap nguyen
Newspaper Frontend
Commits
d2ad01e1
Commit
d2ad01e1
authored
2 years ago
by
Ken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enhance avoid increasing pages when no data
parent
49667e35
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
20 deletions
+34
-20
Newspaper.scss
src/components/Newspaper/Newspaper.scss
+1
-1
homePageSlice.ts
src/pages/home/homePageSlice.ts
+12
-4
index.tsx
src/pages/home/index.tsx
+21
-15
No files found.
src/components/Newspaper/Newspaper.scss
View file @
d2ad01e1
...
...
@@ -40,7 +40,7 @@
&
__desc
{
line-height
:
22px
;
font-size
:
1
4
px
;
font-size
:
1
8
px
;
color
:
$neutral-900
;
display
:
block
;
}
...
...
This diff is collapsed.
Click to expand it.
src/pages/home/homePageSlice.ts
View file @
d2ad01e1
...
...
@@ -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
;
This diff is collapsed.
Click to expand it.
src/pages/home/index.tsx
View file @
d2ad01e1
...
...
@@ -10,7 +10,7 @@ import { CircularProgress } from "@mui/material";
const
HomePage
=
()
=>
{
const
dispatch
=
useAppDispatch
();
const
{
newsData
}
=
useAppSelector
(
homeSelector
);
const
{
newsData
,
currentPage
}
=
useAppSelector
(
homeSelector
);
const
{
ref
,
inView
}
=
useInView
({
threshold
:
0.7
,
initialInView
:
false
,
...
...
@@ -21,6 +21,7 @@ const HomePage = () => {
useEffect
(()
=>
{
try
{
if
(
page
>
currentPage
)
{
setIsLocalLoading
(
true
);
setTimeout
(()
=>
{
const
newsParams
:
IPagination
=
{
...
...
@@ -30,12 +31,17 @@ const HomePage = () => {
PageSize
:
10
,
};
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
();
setIsLocalLoading
(
false
);
},
1000
);
}
}
catch
(
err
)
{
console
.
error
(
"ERROR: "
,
err
);
setIsLocalLoading
(
false
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment