2018-09-19 13:49:04 +02:00
|
|
|
// @flow
|
2018-09-18 16:51:31 +02:00
|
|
|
import React from "react";
|
2018-10-08 17:34:11 +02:00
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import {
|
|
|
|
|
ErrorNotification,
|
|
|
|
|
Loading,
|
|
|
|
|
Paginator
|
|
|
|
|
} from "@scm-manager/ui-components";
|
2018-09-17 14:03:13 +02:00
|
|
|
|
|
|
|
|
import {
|
2018-10-04 19:08:11 +02:00
|
|
|
fetchChangesets,
|
|
|
|
|
fetchChangesetsByBranchAndPage,
|
2018-09-19 17:18:24 +02:00
|
|
|
fetchChangesetsByLink,
|
2018-09-20 16:28:41 +02:00
|
|
|
fetchChangesetsByPage,
|
2018-10-09 16:24:19 +02:00
|
|
|
getChangesets,
|
2018-10-04 19:08:11 +02:00
|
|
|
getChangesetsFromState,
|
|
|
|
|
getFetchChangesetsFailure,
|
|
|
|
|
isFetchChangesetsPending,
|
|
|
|
|
selectListAsCollection
|
2018-09-17 14:03:13 +02:00
|
|
|
} from "../modules/changesets";
|
2018-10-08 17:34:11 +02:00
|
|
|
import type { History } from "history";
|
|
|
|
|
import type {
|
|
|
|
|
Changeset,
|
|
|
|
|
PagedCollection,
|
|
|
|
|
Repository,
|
|
|
|
|
Branch
|
|
|
|
|
} from "@scm-manager/ui-types";
|
2018-09-27 16:29:33 +02:00
|
|
|
import ChangesetList from "../components/changesets/ChangesetList";
|
2018-10-08 17:34:11 +02:00
|
|
|
import { withRouter } from "react-router-dom";
|
|
|
|
|
import { fetchBranches, getBranch, getBranchNames } from "../modules/branches";
|
2018-10-05 09:55:17 +02:00
|
|
|
import BranchChooser from "./BranchChooser";
|
2018-09-17 14:03:13 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branchName: string,
|
2018-10-04 19:08:11 +02:00
|
|
|
branchNames: string[],
|
2018-09-17 14:03:13 +02:00
|
|
|
history: History,
|
2018-09-18 16:51:31 +02:00
|
|
|
fetchChangesetsByNamespaceNameAndBranch: (
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string,
|
|
|
|
|
branch: string
|
2018-09-19 13:49:04 +02:00
|
|
|
) => void,
|
2018-09-19 17:18:24 +02:00
|
|
|
list: PagedCollection,
|
2018-10-05 17:13:12 +02:00
|
|
|
fetchChangesetsByLink: (Repository, string, Branch) => void,
|
2018-10-04 19:08:11 +02:00
|
|
|
fetchChangesetsByPage: (Repository, number) => void,
|
2018-10-08 17:34:11 +02:00
|
|
|
fetchChangesetsByBranchAndPage: (Repository, Branch, number) => void,
|
2018-10-04 19:08:11 +02:00
|
|
|
fetchBranches: Repository => void,
|
2018-09-27 16:29:33 +02:00
|
|
|
page: number,
|
2018-10-04 19:08:11 +02:00
|
|
|
t: string => string,
|
|
|
|
|
match: any,
|
|
|
|
|
changesets: Changeset[],
|
|
|
|
|
loading: boolean,
|
2018-10-05 17:13:12 +02:00
|
|
|
error: boolean,
|
|
|
|
|
branch: Branch
|
2018-09-27 16:29:33 +02:00
|
|
|
};
|
|
|
|
|
|
2018-10-09 16:24:19 +02:00
|
|
|
type State = {};
|
2018-09-17 14:03:13 +02:00
|
|
|
|
2018-10-04 19:08:11 +02:00
|
|
|
class Changesets extends React.PureComponent<Props, State> {
|
2018-10-05 17:13:12 +02:00
|
|
|
constructor(props: Props) {
|
2018-09-17 14:03:13 +02:00
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 14:46:01 +02:00
|
|
|
onPageChange = (link: string) => {
|
2018-10-05 17:13:12 +02:00
|
|
|
const { repository, branch } = this.props;
|
2018-10-04 17:12:38 +02:00
|
|
|
this.props.fetchChangesetsByLink(repository, link, branch);
|
2018-09-19 14:46:01 +02:00
|
|
|
};
|
2018-09-19 17:18:24 +02:00
|
|
|
|
2018-09-17 14:03:13 +02:00
|
|
|
componentDidMount() {
|
2018-10-08 17:34:11 +02:00
|
|
|
if (!this.props.loading) {
|
|
|
|
|
this.updateContent();
|
|
|
|
|
}
|
2018-09-27 16:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateContent() {
|
2018-09-18 16:51:31 +02:00
|
|
|
const {
|
2018-10-05 17:13:12 +02:00
|
|
|
repository,
|
|
|
|
|
branch,
|
|
|
|
|
page,
|
2018-09-20 16:28:41 +02:00
|
|
|
fetchChangesetsByPage,
|
2018-10-05 17:13:12 +02:00
|
|
|
fetchChangesetsByBranchAndPage
|
2018-09-18 16:51:31 +02:00
|
|
|
} = this.props;
|
2018-10-05 17:13:12 +02:00
|
|
|
if (branch) {
|
|
|
|
|
fetchChangesetsByBranchAndPage(repository, branch, page);
|
2018-09-17 16:31:19 +02:00
|
|
|
} else {
|
2018-10-05 17:13:12 +02:00
|
|
|
fetchChangesetsByPage(repository, page);
|
2018-09-17 16:31:19 +02:00
|
|
|
}
|
2018-09-17 14:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-08 17:34:11 +02:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2018-09-20 16:28:41 +02:00
|
|
|
const { page, list, repository, match } = this.props;
|
|
|
|
|
const { namespace, name } = repository;
|
2018-10-09 16:24:19 +02:00
|
|
|
const branch = match.params.branch;
|
|
|
|
|
|
2018-10-08 12:54:11 +02:00
|
|
|
if (!this.props.loading) {
|
|
|
|
|
if (prevProps.branch !== this.props.branch) {
|
2018-10-08 17:34:11 +02:00
|
|
|
this.updateContent();
|
2018-10-08 12:54:11 +02:00
|
|
|
}
|
2018-09-27 16:29:33 +02:00
|
|
|
|
2018-10-08 12:54:11 +02:00
|
|
|
if (list && (list.page || list.page === 0)) {
|
2018-10-09 16:24:19 +02:00
|
|
|
console.log(list);
|
2018-10-08 12:54:11 +02:00
|
|
|
// backend starts paging at 0
|
|
|
|
|
const statePage: number = list.page + 1;
|
2018-10-09 16:24:19 +02:00
|
|
|
console.log(`page: ${page} - statePage: ${statePage}`);
|
2018-10-08 12:54:11 +02:00
|
|
|
if (page !== statePage) {
|
|
|
|
|
if (branch) {
|
|
|
|
|
this.props.history.push(
|
2018-10-08 17:34:11 +02:00
|
|
|
`/repo/${namespace}/${name}/${branch}/changesets/${statePage}`
|
2018-10-08 12:54:11 +02:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.props.history.push(
|
2018-10-08 17:34:11 +02:00
|
|
|
`/repo/${namespace}/${name}/changesets/${statePage}`
|
2018-10-08 12:54:11 +02:00
|
|
|
);
|
|
|
|
|
}
|
2018-09-20 16:28:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 14:03:13 +02:00
|
|
|
render() {
|
2018-09-18 16:51:31 +02:00
|
|
|
const { changesets, loading, error } = this.props;
|
2018-09-27 16:29:33 +02:00
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 17:20:17 +02:00
|
|
|
if (loading || !changesets) {
|
2018-09-18 16:51:31 +02:00
|
|
|
return <Loading />;
|
2018-09-17 14:03:13 +02:00
|
|
|
}
|
2018-09-20 16:28:41 +02:00
|
|
|
|
2018-09-18 16:51:31 +02:00
|
|
|
return (
|
|
|
|
|
<div>
|
2018-10-04 15:52:17 +02:00
|
|
|
{this.renderList()}
|
2018-09-19 13:49:04 +02:00
|
|
|
{this.renderPaginator()}
|
2018-09-18 16:51:31 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-09-18 09:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 15:52:17 +02:00
|
|
|
renderList = () => {
|
2018-10-08 17:34:11 +02:00
|
|
|
const branch = decodeURIComponent(this.props.match.params.branch);
|
2018-10-05 17:13:12 +02:00
|
|
|
const { repository, changesets, t } = this.props;
|
2018-09-17 14:03:13 +02:00
|
|
|
|
2018-10-05 17:13:12 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
2018-10-09 09:22:06 +02:00
|
|
|
<BranchChooser
|
|
|
|
|
repository={repository}
|
|
|
|
|
selectedBranchName={branch}
|
|
|
|
|
label={t("changesets.branchselector-label")}
|
|
|
|
|
callback={branch => this.branchChanged(branch)}
|
|
|
|
|
/>
|
2018-10-05 17:13:12 +02:00
|
|
|
<ChangesetList repository={repository} changesets={changesets} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
2018-09-18 09:22:08 +02:00
|
|
|
};
|
|
|
|
|
|
2018-09-19 13:49:04 +02:00
|
|
|
renderPaginator() {
|
|
|
|
|
const { list } = this.props;
|
|
|
|
|
if (list) {
|
|
|
|
|
return <Paginator collection={list} onPageChange={this.onPageChange} />;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 17:13:12 +02:00
|
|
|
branchChanged = (branch: Branch): void => {
|
2018-09-18 16:51:31 +02:00
|
|
|
const { history, repository } = this.props;
|
2018-10-05 17:13:12 +02:00
|
|
|
if (branch === undefined) {
|
2018-10-08 17:34:11 +02:00
|
|
|
history.push(
|
|
|
|
|
`/repo/${repository.namespace}/${repository.name}/changesets`
|
|
|
|
|
);
|
2018-09-27 16:29:33 +02:00
|
|
|
} else {
|
2018-10-08 17:34:11 +02:00
|
|
|
const branchName = encodeURIComponent(branch.name);
|
2018-10-05 17:13:12 +02:00
|
|
|
this.setState({ branch: branchName });
|
2018-09-27 16:29:33 +02:00
|
|
|
history.push(
|
2018-10-08 17:34:11 +02:00
|
|
|
`/repo/${repository.namespace}/${
|
|
|
|
|
repository.name
|
|
|
|
|
}/${branchName}/changesets`
|
2018-09-27 16:29:33 +02:00
|
|
|
);
|
|
|
|
|
}
|
2018-09-17 14:03:13 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 16:28:41 +02:00
|
|
|
const getPageFromProps = props => {
|
|
|
|
|
let page = props.match.params.page;
|
|
|
|
|
if (page) {
|
|
|
|
|
page = parseInt(page, 10);
|
|
|
|
|
} else {
|
|
|
|
|
page = 1;
|
|
|
|
|
}
|
|
|
|
|
return page;
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-17 14:03:13 +02:00
|
|
|
const mapStateToProps = (state, ownProps: Props) => {
|
2018-10-04 17:12:38 +02:00
|
|
|
const { repository } = ownProps;
|
2018-10-05 17:13:12 +02:00
|
|
|
const branchName = ownProps.match.params.branch;
|
2018-10-09 16:24:19 +02:00
|
|
|
const branch = getBranch(state, repository, decodeURIComponent(branchName));
|
2018-10-04 17:12:38 +02:00
|
|
|
const loading = isFetchChangesetsPending(state, repository, branch);
|
2018-10-09 16:24:19 +02:00
|
|
|
const changesets = getChangesets(state, repository, branch);
|
2018-10-04 17:12:38 +02:00
|
|
|
const branchNames = getBranchNames(state, repository);
|
|
|
|
|
const error = getFetchChangesetsFailure(state, repository, branch);
|
2018-10-08 17:34:11 +02:00
|
|
|
const list = selectListAsCollection(state, repository);
|
2018-09-20 16:28:41 +02:00
|
|
|
const page = getPageFromProps(ownProps);
|
2018-09-19 13:49:04 +02:00
|
|
|
|
2018-09-17 14:03:13 +02:00
|
|
|
return {
|
2018-09-19 13:49:04 +02:00
|
|
|
loading,
|
|
|
|
|
changesets,
|
|
|
|
|
branchNames,
|
|
|
|
|
error,
|
2018-09-20 16:28:41 +02:00
|
|
|
list,
|
2018-10-05 17:13:12 +02:00
|
|
|
page,
|
|
|
|
|
branch
|
2018-09-18 16:51:31 +02:00
|
|
|
};
|
2018-09-17 14:03:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
2018-10-04 17:12:38 +02:00
|
|
|
fetchBranches: (repository: Repository) => {
|
|
|
|
|
dispatch(fetchBranches(repository));
|
|
|
|
|
},
|
|
|
|
|
fetchChangesets: (repository: Repository) => {
|
|
|
|
|
dispatch(fetchChangesets(repository));
|
2018-09-20 16:28:41 +02:00
|
|
|
},
|
2018-10-04 17:12:38 +02:00
|
|
|
fetchChangesetsByPage: (repository, page: number) => {
|
|
|
|
|
dispatch(fetchChangesetsByPage(repository, page));
|
2018-09-17 14:03:13 +02:00
|
|
|
},
|
2018-09-20 16:28:41 +02:00
|
|
|
fetchChangesetsByBranchAndPage: (
|
2018-10-04 17:12:38 +02:00
|
|
|
repository,
|
2018-10-08 17:34:11 +02:00
|
|
|
branch: Branch,
|
2018-09-20 16:28:41 +02:00
|
|
|
page: number
|
2018-09-18 16:51:31 +02:00
|
|
|
) => {
|
2018-10-04 17:12:38 +02:00
|
|
|
dispatch(fetchChangesetsByBranchAndPage(repository, branch, page));
|
2018-09-19 17:18:24 +02:00
|
|
|
},
|
|
|
|
|
fetchChangesetsByLink: (
|
2018-10-04 17:12:38 +02:00
|
|
|
repository: Repository,
|
2018-09-19 17:18:24 +02:00
|
|
|
link: string,
|
2018-10-08 17:34:11 +02:00
|
|
|
branch?: Branch
|
2018-09-19 17:18:24 +02:00
|
|
|
) => {
|
2018-10-04 17:12:38 +02:00
|
|
|
dispatch(fetchChangesetsByLink(repository, link, branch));
|
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-09-18 16:51:31 +02:00
|
|
|
export default withRouter(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-09-27 16:29:33 +02:00
|
|
|
)(translate("repos")(Changesets))
|
2018-09-18 16:51:31 +02:00
|
|
|
);
|