Skip to content

Commit 8041f5d

Browse files
Pagination enabled.
1 parent a9da25a commit 8041f5d

File tree

11 files changed

+418
-176
lines changed

11 files changed

+418
-176
lines changed

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import HeaderNavigationBar from "./components/navigation/HeaderNavigationBar";
99
import {connect} from "react-redux";
1010
import {ConnectionsOverview} from "./components/connections/ConnectionsOverview";
1111
import AddConnectionForm from "./components/connections/AddConnectionForm";
12+
import {FormGenOverview} from "./components/form/FormGenOverview";
1213

1314

1415
class App extends React.Component {
@@ -28,7 +29,7 @@ class App extends React.Component {
2829
<Route exact path="/">
2930
<WelcomePage/>
3031
</Route>
31-
<Route exact path="/browse/forms/:connectionName" component={ContextOverview}>
32+
<Route exact path="/browse/forms/:connectionName" component={FormGenOverview}>
3233
</Route>
3334
<Route exact path="/browse/contexts/:connectionName" component={ContextOverview}>
3435
</Route>

src/App.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
2-
import { render } from '@testing-library/react';
2+
import {render} from '@testing-library/react';
33
import App from './App';
44

55
test('renders learn react link', () => {
6-
const { getByText } = render(<App />);
7-
const linkElement = getByText(/learn react/i);
8-
expect(linkElement).toBeInTheDocument();
6+
const {getByText} = render(<App/>);
7+
const linkElement = getByText(/learn react/i);
8+
expect(linkElement).toBeInTheDocument();
99
});

src/api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ if (process.env.REACT_APP_MOCK_REST_API == "true") {
99
}
1010

1111
export default axios.create({
12-
baseURL: apiUrl
12+
baseURL: apiUrl
1313
});

src/components/Paginator.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import Pagination from "react-bootstrap/Pagination";
3+
import Alert from "react-bootstrap/Alert";
4+
5+
export const ITEMS_PER_PAGE = 30;
6+
7+
export class Paginator extends React.Component {
8+
9+
constructor(props) {
10+
super(props);
11+
this.state = {
12+
activePage: Math.round(this.props.offset / ITEMS_PER_PAGE) + 1,
13+
numberOfPages: Math.round(this.props.totalItems / ITEMS_PER_PAGE),
14+
}
15+
}
16+
17+
handleSelectPage(selectedPage) {
18+
if (selectedPage > 0 && selectedPage <= this.state.numberOfPages) {
19+
this.setState({activePage: selectedPage, loading: true})
20+
21+
this.props.requestItemsHandler((selectedPage - 1) * ITEMS_PER_PAGE, ITEMS_PER_PAGE)
22+
}
23+
}
24+
25+
render() {
26+
if (!this.props.totalItems) {
27+
return <div>Paginator not initialized correctly.</div>
28+
}
29+
let loadingMessage;
30+
if (this.props.loading) {
31+
loadingMessage = <Alert style={{margin: '0.0'}} variant={"light"} className={"h-10"}>
32+
Reloading...
33+
</Alert>
34+
}
35+
36+
let leftEllipses;
37+
if (this.state.activePage > 5) {
38+
leftEllipses = <Pagination.Ellipsis/>;
39+
}
40+
let rightEllipses;
41+
if (this.state.activePage < this.state.numberOfPages - 5) {
42+
rightEllipses = <Pagination.Ellipsis/>;
43+
}
44+
45+
let items = [];
46+
let firstDisplayedPage = this.state.activePage > 5 ? this.state.activePage - 4 : 1;
47+
let lastDisplayedPage = this.state.activePage < this.state.numberOfPages - 5 ? this.state.activePage + 4 : this.state.numberOfPages;
48+
for (let pageNumber = firstDisplayedPage; pageNumber <= lastDisplayedPage; pageNumber++) {
49+
items.push(
50+
<Pagination.Item key={pageNumber} active={pageNumber === this.state.activePage}
51+
onClick={() => this.handleSelectPage(pageNumber)}>
52+
{pageNumber}
53+
</Pagination.Item>,
54+
);
55+
}
56+
57+
return <div>
58+
59+
<Pagination>
60+
<Pagination.First onClick={() => this.handleSelectPage(1)}/>
61+
<Pagination.Prev onClick={() => this.handleSelectPage(this.state.activePage - 1)}/>
62+
{leftEllipses}
63+
{items}
64+
{rightEllipses}
65+
<Pagination.Next onClick={() => this.handleSelectPage(this.state.activePage + 1)}/>
66+
<Pagination.Last onClick={() => this.handleSelectPage(this.state.numberOfPages)}/>
67+
{loadingMessage}
68+
</Pagination>
69+
</div>;
70+
}
71+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Alert from "react-bootstrap/Alert";
55

66
// TODO: should we have one component for displaying and just changing the state or new component for each of the contexts
77

8-
export class FormGenView extends React.Component {
8+
export class SFormsDisplay extends React.Component {
99

1010
constructor() {
1111
super();
@@ -39,6 +39,12 @@ export class FormGenView extends React.Component {
3939
};
4040

4141
render() {
42+
if (!this.props.contextUri) {
43+
return <Alert variant={"light"} className={"h-10"}>
44+
Context not specified...
45+
</Alert>
46+
}
47+
4248
const modalProps = {
4349
onHide: () => {
4450
},

src/components/connections/AddConnectionForm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class AddConnectionForm extends React.Component {
5252
this.state = this.baseState
5353
this.setState({showError: false, showSuccess: true});
5454
}).catch((error) => {
55-
console.log(error)
56-
this.setState({showError: true, showSuccess: false}); // todo: improve handling individual messages
55+
console.log(error)
56+
this.setState({showError: true, showSuccess: false}); // todo: improve handling individual messages
5757
}
5858
);
5959
}

src/components/connections/ConnectionsOverview.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export class ConnectionsOverview extends React.Component {
8383
<Row>
8484
<Col>
8585
<Button variant="outline-secondary">Change</Button>{' '}
86-
<DeleteConnectionForm connectionName={connectionName} refreshCallBack={this.refresh}/>
86+
<DeleteConnectionForm connectionName={connectionName}
87+
refreshCallBack={this.refresh}/>
8788
</Col>
8889
</Row>
8990
</ListGroup.Item>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from "react";
2+
import {ContextLine} from "./ContextLine";
3+
import Alert from "react-bootstrap/Alert";
4+
import {ITEMS_PER_PAGE, Paginator} from "../Paginator";
5+
import API from "../../api";
6+
7+
export class ContextList extends React.Component {
8+
9+
constructor() {
10+
super();
11+
this.state = {
12+
contexts: null,
13+
totalItems: null,
14+
offset: null,
15+
loading: true
16+
}
17+
this.requestPaginatedContexts = this.requestPaginatedContexts.bind(this)
18+
}
19+
20+
componentDidMount() {
21+
this.requestPaginatedContexts(0, ITEMS_PER_PAGE)
22+
}
23+
24+
requestPaginatedContexts(offset, limit) {
25+
this.setState({loading: true})
26+
console.log(offset)
27+
console.log(limit + ".")
28+
API.get("/rest/contexts/paginated", {
29+
params: {
30+
"connectionName": this.props.connectionName,
31+
"offset": offset,
32+
"limit": limit
33+
}
34+
}).then(response => {
35+
return response.data;
36+
}).then(data => {
37+
this.setState({
38+
contexts: data.items,
39+
totalItems: data.totalItems,
40+
offset: data.offset,
41+
loading: false
42+
});
43+
});
44+
}
45+
46+
render() {
47+
if (!this.state.contexts || this.state.contexts.length == 0) {
48+
return <Alert variant={"light"} className={"h-10"}>
49+
Loading contexts...
50+
</Alert>
51+
}
52+
53+
let i = 0;
54+
const contexts = this.state.contexts.filter((context) => {
55+
if (this.props.filterProcessed) {
56+
return !context.processed
57+
} else {
58+
return true;
59+
}
60+
}).map((context) => {
61+
i++;
62+
return (
63+
<ContextLine key={i}
64+
context={context}
65+
connectionName={this.props.connectionName}
66+
clickHandler={this.props.updateActiveContextUri}/>);
67+
});
68+
69+
let paginator;
70+
if (this.state.contexts) {
71+
paginator = <Paginator totalItems={this.state.totalItems}
72+
offset={this.state.offset}
73+
loading={this.state.loading}
74+
requestItemsHandler={this.requestPaginatedContexts}/>;
75+
}
76+
77+
return <div>
78+
79+
{paginator}
80+
{contexts}
81+
82+
</div>
83+
84+
}
85+
}

0 commit comments

Comments
 (0)