-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reader: enable pagination controls for Recent Feed #97674
Changes from 4 commits
3cdde6c
03303df
e4c60f3
41b2ba6
2fae39b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,16 +184,6 @@ body.is-reader-full-post { | |
padding: $recent-feed-spacing; | ||
} | ||
} | ||
|
||
// This is a temporary fix to disable the pagination page selector because of this issue: Automattic/loop#247 | ||
// Once that issue is fixed, this code should be removed. | ||
.dataviews-pagination__page-select { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
display: none; | ||
|
||
& + div { | ||
margin-left: auto; | ||
} | ||
} | ||
} | ||
|
||
&__post-column { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,16 +36,55 @@ export const items = ( state = [], action ) => { | |
let gap; | ||
let newState; | ||
let newXPosts; | ||
let perPage; | ||
let page; | ||
|
||
switch ( action.type ) { | ||
case READER_STREAMS_PAGE_RECEIVE: | ||
gap = action.payload.gap; | ||
streamItems = action.payload.streamItems; | ||
perPage = action.payload.perPage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need optional chaining here like on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it would make a difference because I do notice now that I broke from this "destructuring" pattern when accessing |
||
page = action.payload.page; | ||
|
||
if ( ! Array.isArray( streamItems ) ) { | ||
return state; | ||
} | ||
|
||
// For the Recent feeds, we need to pad the stream with empty items | ||
// for the DataViews pagination to work correctly | ||
// see Automattic/loop#238 | ||
if ( | ||
action.payload?.streamKey?.startsWith( 'recent' ) && | ||
streamItems.length > 0 && | ||
perPage && | ||
page > 1 | ||
) { | ||
// Calculate where new items should start | ||
const startIndex = ( page - 1 ) * perPage; | ||
const existingLength = state.length; | ||
const paddingNeeded = startIndex - existingLength; | ||
|
||
// Case 1: Need to add padding before new items | ||
if ( paddingNeeded > 0 ) { | ||
const paddingItems = Array( paddingNeeded ) | ||
.fill( undefined ) | ||
.map( ( _, index ) => ( { | ||
isPadding: true, | ||
postId: `padding-${ index }`, | ||
} ) ); | ||
|
||
return combineXPosts( [ ...state, ...paddingItems, ...streamItems ] ); | ||
} | ||
|
||
// Case 2: Replace existing items at correct index | ||
const updatedState = [ ...state ]; | ||
streamItems.forEach( ( item, index ) => { | ||
updatedState[ startIndex + index ] = item; | ||
} ); | ||
|
||
return combineXPosts( updatedState ); | ||
} | ||
|
||
if ( gap ) { | ||
const beforeGap = takeWhile( state, ( postKey ) => ! keysAreEqual( postKey, gap ) ); | ||
const afterGap = takeRightWhile( state, ( postKey ) => ! keysAreEqual( postKey, gap ) ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about this pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really useful, especially when using Array.filter(). If you search around for "type guard using type predicate" you can learn more. Here's the section from the TS guide.