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

144 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-09-18 16:51:31 +02:00
import React from "react";
import { connect } from "react-redux";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
2018-09-17 14:03:13 +02:00
import {
2018-09-18 16:51:31 +02:00
fetchChangesetsByNamespaceAndName,
fetchChangesetsByNamespaceNameAndBranch,
getChangesets,
getFetchChangesetsFailure,
isFetchChangesetsPending
2018-09-17 14:03:13 +02:00
} from "../modules/changesets";
2018-09-18 16:51:31 +02:00
import type { History } from "history";
import {
fetchBranchesByNamespaceAndName,
getBranchNames
} from "../../repos/modules/branches";
import type { Repository } from "@scm-manager/ui-types";
2018-09-17 14:03:13 +02:00
import ChangesetTable from "../components/ChangesetTable";
import DropDown from "../components/DropDown";
2018-09-18 16:51:31 +02:00
import { withRouter } from "react-router-dom";
2018-09-17 14:03:13 +02:00
type Props = {
repository: Repository,
branchName: string,
history: History,
2018-09-18 16:51:31 +02:00
fetchChangesetsByNamespaceNameAndBranch: (
namespace: string,
name: string,
branch: string
) => void
};
2018-09-17 14:03:13 +02:00
class Changesets extends React.Component<State, Props> {
constructor(props) {
super(props);
this.state = {};
2018-09-17 14:03:13 +02:00
}
componentDidMount() {
2018-09-18 16:51:31 +02:00
const { namespace, name } = this.props.repository;
const branchName = this.props.match.params.branch;
2018-09-18 16:51:31 +02:00
const {
fetchChangesetsByNamespaceNameAndBranch,
fetchChangesetsByNamespaceAndName,
fetchBranchesByNamespaceAndName
} = this.props;
if (branchName) {
fetchChangesetsByNamespaceNameAndBranch(namespace, name, branchName);
} else {
fetchChangesetsByNamespaceAndName(namespace, name);
}
fetchBranchesByNamespaceAndName(namespace, name);
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-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-18 16:51:31 +02:00
return (
<div>
<ErrorNotification error={error} />
{this.renderContent()}
</div>
);
}
renderContent = () => {
const branch = this.props.match.params.branch;
2018-09-18 16:51:31 +02:00
const { changesets, branchNames } = this.props;
if (branchNames && branchNames.length > 0) {
2018-09-18 16:51:31 +02:00
return (
<div>
<label className="label">Branch: </label>
<DropDown
options={branchNames}
preselectedOption={branch}
optionSelected={branch => this.branchChanged(branch)}
/>
<hr />
<ChangesetTable changesets={changesets} />
</div>
);
2018-09-17 14:03:13 +02:00
}
2018-09-18 16:51:31 +02:00
return <ChangesetTable changesets={changesets} />;
};
2018-09-17 14:03:13 +02:00
branchChanged = (branchName: string) => {
2018-09-18 16:51:31 +02:00
const { history, repository } = this.props;
history.push(
`/repo/${repository.namespace}/${repository.name}/history/${branchName}`
);
2018-09-17 14:03:13 +02:00
};
}
const mapStateToProps = (state, ownProps: Props) => {
2018-09-18 16:51:31 +02:00
const { namespace, name } = ownProps.repository;
2018-09-17 14:03:13 +02:00
return {
2018-09-17 17:20:17 +02:00
loading: isFetchChangesetsPending(namespace, name, state),
2018-09-18 16:51:31 +02:00
changesets: getChangesets(
state,
namespace,
name,
ownProps.match.params.branch
),
branchNames: getBranchNames(namespace, name, state),
2018-09-18 16:51:31 +02:00
error: getFetchChangesetsFailure(
state,
namespace,
name,
ownProps.match.params.branch
)
};
2018-09-17 14:03:13 +02:00
};
const mapDispatchToProps = dispatch => {
return {
fetchChangesetsByNamespaceAndName: (namespace: string, name: string) => {
dispatch(fetchChangesetsByNamespaceAndName(namespace, name));
},
2018-09-18 16:51:31 +02:00
fetchChangesetsByNamespaceNameAndBranch: (
namespace: string,
name: string,
branch: string
) => {
dispatch(
fetchChangesetsByNamespaceNameAndBranch(namespace, name, branch)
);
2018-09-17 14:03:13 +02:00
},
fetchBranchesByNamespaceAndName: (namespace: string, name: string) => {
dispatch(fetchBranchesByNamespaceAndName(namespace, name));
}
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
)(Changesets)
);