Files
SCM-Manager/scm-ui/src/repos/containers/Changesets.js

125 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-09-19 13:49:04 +02:00
// @flow
2018-09-17 14:03:13 +02:00
import React from "react";
import { withRouter } from "react-router-dom";
import type {
Branch,
Changeset,
PagedCollection,
Repository
} from "@scm-manager/ui-types";
2018-09-17 14:03:13 +02:00
import {
2018-10-17 10:38:46 +02:00
fetchChangesets,
2018-10-09 16:24:19 +02:00
getChangesets,
2018-10-04 19:08:11 +02:00
getFetchChangesetsFailure,
isFetchChangesetsPending,
selectListAsCollection
2018-09-17 14:03:13 +02:00
} from "../modules/changesets";
2018-10-17 10:38:46 +02:00
import { connect } from "react-redux";
import ChangesetList from "../components/changesets/ChangesetList";
2018-10-17 11:55:47 +02:00
import {
ErrorNotification,
LinkPaginator,
Loading
} from "@scm-manager/ui-components";
import { translate } from "react-i18next";
2018-10-17 11:55:47 +02:00
import { compose } from "redux";
2018-09-17 14:03:13 +02:00
type Props = {
2018-10-17 11:55:47 +02:00
repository: Repository,
branch: Branch,
2018-10-17 10:38:46 +02:00
page: number,
2018-10-16 20:19:22 +02:00
// State props
2018-10-04 19:08:11 +02:00
changesets: Changeset[],
list: PagedCollection,
2018-10-17 10:38:46 +02:00
loading: boolean,
2018-10-11 17:29:50 +02:00
error: Error,
2018-10-16 20:19:22 +02:00
// Dispatch props
2018-10-17 10:38:46 +02:00
fetchChangesets: (Repository, Branch, number) => void,
2018-10-16 20:19:22 +02:00
// Context Props
2018-10-11 17:29:50 +02:00
t: string => string
};
2018-10-17 10:38:46 +02:00
class Changesets extends React.Component<Props> {
2018-09-17 14:03:13 +02:00
componentDidMount() {
2018-10-17 10:38:46 +02:00
const { fetchChangesets, repository, branch, page } = this.props;
2018-10-17 10:38:46 +02:00
fetchChangesets(repository, branch, page);
}
render() {
const { changesets, loading, error, t } = this.props;
if (error) {
2018-10-17 11:55:47 +02:00
return <ErrorNotification error={error} />;
}
if (loading) {
2018-09-18 16:51:31 +02:00
return <Loading />;
2018-09-17 14:03:13 +02:00
}
2018-10-17 11:55:47 +02:00
if (!changesets || changesets.length === 0) {
return null;
}
2018-09-18 16:51:31 +02:00
return (
<>
{this.renderList()}
2018-09-19 13:49:04 +02:00
{this.renderPaginator()}
</>
2018-09-18 16:51:31 +02:00
);
}
renderList = () => {
const { repository, changesets } = this.props;
return <ChangesetList repository={repository} changesets={changesets} />;
};
renderPaginator = () => {
2018-10-17 10:57:01 +02:00
const { page, list } = this.props;
2018-09-19 13:49:04 +02:00
if (list) {
2018-10-17 10:57:01 +02:00
return <LinkPaginator page={page} collection={list} />;
2018-09-19 13:49:04 +02:00
}
return null;
2018-09-17 14:03:13 +02:00
};
}
const mapDispatchToProps = dispatch => {
return {
2018-10-17 10:38:46 +02:00
fetchChangesets: (repo: Repository, branch: Branch, page: number) => {
dispatch(fetchChangesets(repo, branch, page));
2018-09-17 14:03:13 +02:00
}
2018-09-18 16:51:31 +02:00
};
2018-09-17 14:03:13 +02:00
};
2018-10-17 11:55:47 +02:00
export function getPageFromMatch(match: any) {
let page = parseInt(match.params.page);
if (isNaN(page) || !page) {
page = 1;
}
return page;
}
const mapStateToProps = (state: any, ownProps: Props) => {
2018-10-17 10:38:46 +02:00
const { repository, branch, match } = ownProps;
const changesets = getChangesets(state, repository, branch);
const loading = isFetchChangesetsPending(state, repository, branch);
const error = getFetchChangesetsFailure(state, repository, branch);
const list = selectListAsCollection(state, repository, branch);
2018-10-17 11:55:47 +02:00
const page = getPageFromMatch(match);
2018-10-17 10:38:46 +02:00
return { changesets, list, page, loading, error };
};
2018-10-17 10:38:46 +02:00
2018-10-17 11:55:47 +02:00
export default compose(
withRouter,
2018-09-18 16:51:31 +02:00
connect(
mapStateToProps,
mapDispatchToProps
2018-10-17 11:55:47 +02:00
),
translate("repos")
)(Changesets);