-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathdemo.js
179 lines (163 loc) · 4.88 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { Component } from 'react';
import { createRoot } from 'react-dom/client';
import PropTypes from 'prop-types';
import ReactPaginate from 'react-paginate';
import styled from 'styled-components';
import $ from 'jquery';
// You can style your pagination component
// thanks to styled-components.
// Use inner class names to style the controls.
const MyPaginate = styled(ReactPaginate).attrs({
// You can redefine classes here, if you want.
activeClassName: 'active', // default to "selected"
})`
margin-bottom: 2rem;
display: flex;
flex-direction: row;
justify-content: space-between;
list-style-type: none;
padding: 0 5rem;
li a {
border-radius: 7px;
padding: 0.1rem 1rem;
border: gray 1px solid;
cursor: pointer;
}
li.previous a,
li.next a,
li.break a {
border-color: transparent;
}
li.active a {
background-color: #0366d6;
border-color: transparent;
color: white;
min-width: 32px;
}
li.disabled a {
color: grey;
}
li.disable,
li.disabled a {
cursor: default;
}
`;
export class CommentList extends Component {
static propTypes = {
data: PropTypes.array.isRequired,
};
render() {
let commentNodes = this.props.data.map(function (comment, index) {
return (
<li key={index} className="list-group-item">
<div className="d-flex w-100 justify-content-between">
<h5 className="mb-1">{comment.comment}</h5>
</div>
<small>From {comment.username}.</small>
</li>
);
});
return (
<div id="project-comments" className="commentList">
<ul className="list-group">{commentNodes}</ul>
</div>
);
}
}
export class App extends Component {
static propTypes = {
url: PropTypes.string.isRequired,
author: PropTypes.string.isRequired,
perPage: PropTypes.number.isRequired,
};
constructor(props) {
super(props);
this.state = {
data: [],
offset: 0,
};
}
loadCommentsFromServer() {
$.ajax({
url: this.props.url,
data: { limit: this.props.perPage, offset: this.state.offset },
dataType: 'json',
type: 'GET',
success: (data) => {
this.setState({
data: data.comments,
pageCount: Math.ceil(data.meta.total_count / data.meta.limit),
});
},
error: (xhr, status, err) => {
console.error(this.props.url, status, err.toString()); // eslint-disable-line
},
});
}
componentDidMount() {
this.loadCommentsFromServer();
}
handlePageClick = (data) => {
console.log('onPageChange', data);
let selected = data.selected;
let offset = Math.ceil(selected * this.props.perPage);
this.setState({ offset: offset }, () => {
this.loadCommentsFromServer();
});
};
render() {
const currentPage = Math.round(this.state.offset / this.props.perPage);
return (
<div className="commentBox">
<MyPaginate
pageCount={20}
onPageChange={this.handlePageClick}
forcePage={currentPage}
/>
<CommentList data={this.state.data} />
{/* Here the pagination component is styled thanks to Bootstrap
classes. See https://getbootstrap.com/docs/5.1/components/pagination */}
<nav aria-label="Page navigation comments" className="mt-4">
<ReactPaginate
previousLabel="previous"
nextLabel="next"
breakLabel="..."
breakClassName="page-item"
breakLinkClassName="page-link"
pageCount={20}
pageRangeDisplayed={4}
marginPagesDisplayed={2}
onPageChange={this.handlePageClick}
containerClassName="pagination justify-content-center"
pageClassName="page-item"
pageLinkClassName="page-link"
previousClassName="page-item"
previousLinkClassName="page-link"
nextClassName="page-item"
nextLinkClassName="page-link"
activeClassName="active"
// eslint-disable-next-line no-unused-vars
hrefBuilder={(page, pageCount, selected) =>
page >= 1 && page <= pageCount ? `/page/${page}` : '#'
}
hrefAllControls
forcePage={currentPage}
onClick={(clickEvent) => {
console.log('onClick', clickEvent);
// Return false to prevent standard page change,
// return false; // --> Will do nothing.
// return a number to choose the next page,
// return 4; --> Will go to page 5 (index 4)
// return nothing (undefined) to let standard behavior take place.
}}
/>
</nav>
</div>
);
}
}
const container = document.getElementById('react-paginate');
const root = createRoot(container);
root.render(
<App url={'http://localhost:3000/comments'} author={'adele'} perPage={6} />
);