mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-04 20:45:52 +01:00
implemented paging for repository overview
This commit is contained in:
@@ -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)));
|
||||
|
||||
Reference in New Issue
Block a user