implemented paging for repository overview

This commit is contained in:
Sebastian Sdorra
2018-08-01 14:56:24 +02:00
parent b1c65a3a3c
commit 2953c805f9
4 changed files with 155 additions and 20 deletions

View File

@@ -4,26 +4,48 @@ import React from "react";
import type { RepositoryCollection } from "../types/Repositories";
import { connect } from "react-redux";
import {fetchRepos, getFetchReposFailure, getRepositoryCollection, isFetchReposPending} from "../modules/repos";
import {fetchRepos, fetchReposByLink, fetchReposByPage, getFetchReposFailure, getRepositoryCollection, isFetchReposPending} from "../modules/repos";
import { translate } from "react-i18next";
import { Page } from "../../components/layout";
import RepositoryList from "../components/RepositoryList";
import Paginator from '../../components/Paginator';
import {withRouter} from 'react-router-dom';
import type { History } from "history";
type Props = {
page: number,
collection: RepositoryCollection,
loading: boolean,
error: Error,
// dispatched functions
fetchRepos: () => void,
fetchReposByPage: number => void,
fetchReposByLink: string => void,
// context props
t: string => string
t: string => string,
history: History
};
class Overview extends React.Component<Props> {
componentDidMount() {
this.props.fetchRepos();
this.props.fetchReposByPage(this.props.page);
}
/**
* reflect page transitions in the uri
*/
componentDidUpdate() {
const { page, collection } = this.props;
if (collection) {
// backend starts paging by 0
const statePage: number = collection.page + 1;
if (page !== statePage) {
this.props.history.push(`/repos/${statePage}`);
}
}
};
render() {
const { error, loading, t } = this.props;
return (
@@ -34,21 +56,36 @@ class Overview extends React.Component<Props> {
}
renderList() {
const { collection } = this.props;
const { collection, fetchReposByLink } = this.props;
if (collection) {
return (
<RepositoryList repositories={collection._embedded.repositories} />
<div>
<RepositoryList repositories={collection._embedded.repositories} />
<Paginator collection={collection} onPageChange={fetchReposByLink} />
</div>
);
}
return null;
}
}
const getPageFromProps = props => {
let page = props.match.params.page;
if (page) {
page = parseInt(page, 10);
} else {
page = 1;
}
return page;
};
const mapStateToProps = (state, ownProps) => {
const page = getPageFromProps(ownProps);
const collection = getRepositoryCollection(state);
const loading = isFetchReposPending(state);
const error = getFetchReposFailure(state);
return {
page,
collection,
loading,
error
@@ -59,10 +96,16 @@ const mapDispatchToProps = dispatch => {
return {
fetchRepos: () => {
dispatch(fetchRepos());
},
fetchReposByPage: (page: number) => {
dispatch(fetchReposByPage(page))
},
fetchReposByLink: (link: string) => {
dispatch(fetchReposByLink(link))
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("repos")(Overview));
)(translate("repos")(withRouter(Overview)));