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

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-10-04 20:02:18 +02:00
// @flow
import React from "react";
2018-10-05 09:55:17 +02:00
import type { Repository } from "@scm-manager/ui-types";
import { connect } from "react-redux";
import { fetchBranches, getBranch, getBranches } from "../modules/branches";
2018-10-04 20:02:18 +02:00
import DropDown from "../components/DropDown";
type Props = {
repository: Repository,
fetchBranches: Repository => void,
callback: Branch => void, //TODO use correct branch type
branches: Branch[], //TODO use correct branch type
selectedBranchName: string
};
2018-10-05 09:55:17 +02:00
type State = {
selectedBranchName: string
};
2018-10-04 20:02:18 +02:00
class BranchChooser extends React.Component<Props, State> {
2018-10-05 09:55:17 +02:00
constructor(props: Props) {
super(props);
this.state = {
selectedBranchName: props.selectedBranchName
};
}
2018-10-04 20:02:18 +02:00
componentDidMount() {
const { repository, fetchBranches } = this.props;
fetchBranches(repository);
}
render() {
2018-10-05 09:55:17 +02:00
const { branches } = this.props;
2018-10-04 20:02:18 +02:00
return (
<DropDown
options={branches.map(b => b.name)}
2018-10-05 09:55:17 +02:00
preselectedOption={this.state.selectedBranchName}
2018-10-04 20:02:18 +02:00
optionSelected={branch => this.branchChanged(branch)}
/>
);
}
2018-10-05 09:55:17 +02:00
branchChanged = (branchName: string) => {
const { callback } = this.props;
this.setState({ ...this.state, selectedBranchName: branchName });
const branch = this.props.branches.find(b => b.name === branchName);
callback(branch);
};
2018-10-04 20:02:18 +02:00
}
2018-10-05 09:55:17 +02:00
const mapStateToProps = (state: State, ownProps: Props) => {
return {
branches: getBranches(state, ownProps.repository)
};
2018-10-04 20:02:18 +02:00
};
const mapDispatchToProps = (dispatch: any) => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(BranchChooser);