2018-07-31 16:32:16 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
import type { RepositoryCollection } from "../types/Repositories";
|
|
|
|
|
|
|
|
|
|
import { connect } from "react-redux";
|
2018-08-02 16:09:58 +02:00
|
|
|
import {
|
|
|
|
|
fetchRepos,
|
|
|
|
|
fetchReposByLink,
|
|
|
|
|
fetchReposByPage,
|
|
|
|
|
getFetchReposFailure,
|
|
|
|
|
getRepositoryCollection,
|
2018-08-03 08:52:02 +02:00
|
|
|
isAbleToCreateRepos,
|
2018-08-02 16:09:58 +02:00
|
|
|
isFetchReposPending
|
|
|
|
|
} from "../modules/repos";
|
2018-07-31 16:32:16 +02:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import { Page } from "../../components/layout";
|
|
|
|
|
import RepositoryList from "../components/RepositoryList";
|
2018-08-02 16:09:58 +02:00
|
|
|
import Paginator from "../../components/Paginator";
|
|
|
|
|
import { withRouter } from "react-router-dom";
|
2018-08-01 14:56:24 +02:00
|
|
|
import type { History } from "history";
|
2018-08-02 16:09:58 +02:00
|
|
|
import CreateButton from "../../components/buttons/CreateButton";
|
2018-07-31 16:32:16 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-08-01 14:56:24 +02:00
|
|
|
page: number,
|
2018-07-31 16:32:16 +02:00
|
|
|
collection: RepositoryCollection,
|
2018-08-01 10:00:53 +02:00
|
|
|
loading: boolean,
|
|
|
|
|
error: Error,
|
2018-08-03 08:52:02 +02:00
|
|
|
showCreateButton: boolean,
|
2018-07-31 16:32:16 +02:00
|
|
|
|
|
|
|
|
// dispatched functions
|
|
|
|
|
fetchRepos: () => void,
|
2018-08-01 14:56:24 +02:00
|
|
|
fetchReposByPage: number => void,
|
|
|
|
|
fetchReposByLink: string => void,
|
2018-08-03 08:52:02 +02:00
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
// context props
|
2018-08-01 14:56:24 +02:00
|
|
|
t: string => string,
|
|
|
|
|
history: History
|
2018-07-31 16:32:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Overview extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
2018-08-01 14:56:24 +02:00
|
|
|
this.props.fetchReposByPage(this.props.page);
|
2018-07-31 16:32:16 +02:00
|
|
|
}
|
2018-08-01 14:56:24 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 16:09:58 +02:00
|
|
|
}
|
2018-08-01 14:56:24 +02:00
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
render() {
|
2018-08-01 10:00:53 +02:00
|
|
|
const { error, loading, t } = this.props;
|
2018-07-31 16:32:16 +02:00
|
|
|
return (
|
2018-08-02 16:09:58 +02:00
|
|
|
<Page
|
|
|
|
|
title={t("overview.title")}
|
|
|
|
|
subtitle={t("overview.subtitle")}
|
|
|
|
|
loading={loading}
|
|
|
|
|
error={error}
|
|
|
|
|
>
|
2018-07-31 16:32:16 +02:00
|
|
|
{this.renderList()}
|
|
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderList() {
|
2018-08-03 08:52:02 +02:00
|
|
|
const { collection, fetchReposByLink } = this.props;
|
2018-07-31 16:32:16 +02:00
|
|
|
if (collection) {
|
|
|
|
|
return (
|
2018-08-01 14:56:24 +02:00
|
|
|
<div>
|
|
|
|
|
<RepositoryList repositories={collection._embedded.repositories} />
|
|
|
|
|
<Paginator collection={collection} onPageChange={fetchReposByLink} />
|
2018-08-03 08:52:02 +02:00
|
|
|
{this.renderCreateButton()}
|
2018-08-01 14:56:24 +02:00
|
|
|
</div>
|
2018-07-31 16:32:16 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-08-03 08:52:02 +02:00
|
|
|
|
|
|
|
|
renderCreateButton() {
|
|
|
|
|
const { showCreateButton, t } = this.props;
|
|
|
|
|
if (showCreateButton) {
|
|
|
|
|
return (
|
|
|
|
|
<CreateButton
|
|
|
|
|
label={t("overview.create-button")}
|
|
|
|
|
link="/repos/create"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-07-31 16:32:16 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-01 14:56:24 +02:00
|
|
|
const getPageFromProps = props => {
|
|
|
|
|
let page = props.match.params.page;
|
|
|
|
|
if (page) {
|
|
|
|
|
page = parseInt(page, 10);
|
|
|
|
|
} else {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
return page;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-08-01 14:56:24 +02:00
|
|
|
const page = getPageFromProps(ownProps);
|
2018-07-31 16:32:16 +02:00
|
|
|
const collection = getRepositoryCollection(state);
|
2018-08-01 10:00:53 +02:00
|
|
|
const loading = isFetchReposPending(state);
|
|
|
|
|
const error = getFetchReposFailure(state);
|
2018-08-03 08:52:02 +02:00
|
|
|
const showCreateButton = isAbleToCreateRepos(state);
|
2018-07-31 16:32:16 +02:00
|
|
|
return {
|
2018-08-01 14:56:24 +02:00
|
|
|
page,
|
2018-08-01 10:00:53 +02:00
|
|
|
collection,
|
|
|
|
|
loading,
|
2018-08-03 08:52:02 +02:00
|
|
|
error,
|
|
|
|
|
showCreateButton
|
2018-07-31 16:32:16 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
fetchRepos: () => {
|
|
|
|
|
dispatch(fetchRepos());
|
2018-08-01 14:56:24 +02:00
|
|
|
},
|
|
|
|
|
fetchReposByPage: (page: number) => {
|
2018-08-02 16:09:58 +02:00
|
|
|
dispatch(fetchReposByPage(page));
|
2018-08-01 14:56:24 +02:00
|
|
|
},
|
|
|
|
|
fetchReposByLink: (link: string) => {
|
2018-08-02 16:09:58 +02:00
|
|
|
dispatch(fetchReposByLink(link));
|
2018-07-31 16:32:16 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-08-01 14:56:24 +02:00
|
|
|
)(translate("repos")(withRouter(Overview)));
|