Skip to content

Commit c5c8de1

Browse files
author
Adam Silverstein
committed
Add a loadMore pagination type that displays a load more button
Based on Automattic#442 * add preventDefault to prevent admin form submission * add a style to center the button
1 parent c0c455b commit c5c8de1

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

src/react/actions/apiActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export const getEntriesPaginated = (page, scrollTo) => ({
1212
scrollTo,
1313
});
1414

15-
export const getEntriesSuccess = (payload, renderNewEntries) => ({
15+
export const getEntriesSuccess = (payload, renderNewEntries, paginationType) => ({
1616
type: types.GET_ENTRIES_SUCCESS,
1717
payload,
1818
renderNewEntries,
19+
paginationType,
1920
});
2021

2122
export const getEntriesFailed = () => ({

src/react/containers/AppContainer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ class AppContainer extends Component {
3434

3535
render() {
3636
const { page, loading, entries, polling, mergePolling, config } = this.props;
37+
const paginationTypeLoadMore = config.paginationType === 'loadMore';
3738
const canEdit = config.is_liveblog_editable === '1';
3839
const frontEndEditing = config.backend_liveblogging !== '1';
3940
const isAdmin = config.is_admin;
40-
const showEditor = isAdmin || ((page === 1 && canEdit && frontEndEditing));
41+
const showEditor = isAdmin ||
42+
(
43+
(page === 1 || paginationTypeLoadMore) &&
44+
canEdit &&
45+
frontEndEditing
46+
);
4147

4248
return (
4349
<div style={{ position: 'relative' }}>
@@ -75,8 +81,7 @@ const mapStateToProps = state => ({
7581
page: state.pagination.page,
7682
loading: state.api.loading,
7783
entries: Object.keys(state.api.entries)
78-
.map(key => state.api.entries[key])
79-
.slice(0, state.config.entries_per_page),
84+
.map(key => state.api.entries[key]),
8085
polling: Object.keys(state.polling.entries),
8186
config: state.config,
8287
});

src/react/containers/PaginationContainer.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ import * as userActions from '../actions/userActions';
77

88
class PaginationContainer extends Component {
99
render() {
10-
const { page, pages, getEntriesPaginated } = this.props;
10+
const { page, pages, getEntriesPaginated, paginationType } = this.props;
11+
12+
if (paginationType === 'loadMore') {
13+
return (
14+
<div className="liveblog-pagination">
15+
{page !== pages &&
16+
<button
17+
className="button button-large liveblog-btn liveblog-pagination-btn liveblog-pagination-loadmore"
18+
onClick={(e) => {
19+
e.preventDefault();
20+
getEntriesPaginated(page + 1);
21+
}}
22+
>
23+
Load More
24+
</button>}
25+
</div>
26+
);
27+
}
1128

1229
// Don't diplsay pagination if we only have a single page.
1330
if (pages === 1) {
@@ -100,11 +117,13 @@ PaginationContainer.propTypes = {
100117
page: PropTypes.number,
101118
pages: PropTypes.number,
102119
getEntriesPaginated: PropTypes.func,
120+
paginationType: PropTypes.string,
103121
};
104122

105123
const mapStateToProps = state => ({
106124
page: state.pagination.page,
107125
pages: state.pagination.pages,
126+
paginationType: state.config.paginationType,
108127
});
109128

110129
const mapDispatchToProps = dispatch =>

src/react/epics/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const getPaginatedEntriesEpic = (action$, store) =>
8080
store.getState().api.entries,
8181
store.getState().polling.entries,
8282
),
83+
store.getState().config.paginationType,
8384
)),
8485
of(scrollToEntry(getScrollToId(res.response.entries, scrollTo))),
8586
),

src/react/epics/polling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const mergePollingEpic = (action$, store) =>
5454
const entries = Object.keys(polling.entries).map(key => polling.entries[key]);
5555
const pages = Math.max(pagination.pages, polling.pages);
5656

57-
if (pagination.page === 1) {
57+
if (pagination.page === 1 || config.paginationType === 'loadMore') {
5858
return concat(
5959
of(mergePollingIntoEntries(entries, pages)),
6060
of(scrollToEntry(`id_${entries[entries.length - 1].id}`)),

src/react/reducers/api.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export const api = (state = initialState, action) => {
3030
...state,
3131
error: false,
3232
loading: false,
33-
entries: applyUpdate({}, action.payload.entries),
33+
entries: applyUpdate(
34+
action.paginationType === 'loadMore' ? state.entries : {},
35+
action.payload.entries,
36+
),
3437
newestEntry: getNewestEntry(
3538
state.newestEntry,
3639
action.payload.entries[0],

src/styles/core/app/_buttons.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@
6464
background: darken($color-warning, 10%);
6565
}
6666

67+
.liveblog-pagination-btn.liveblog-pagination-loadmore {
68+
margin: 10px auto;
69+
}

0 commit comments

Comments
 (0)