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

104 lines
3.2 KiB
JavaScript
Raw Normal View History

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