Fixed issue with branch selector

This commit is contained in:
Philipp Czora
2018-12-14 20:20:00 +01:00
parent 5cb1a2e0cd
commit 161885ff12
2 changed files with 51 additions and 18 deletions

View File

@@ -17,7 +17,8 @@ type State = {
submitPending: boolean, submitPending: boolean,
error: Error, error: Error,
branches: Branch[], branches: Branch[],
selectedBranchName: string selectedBranchName: string,
defaultBranchChanged: boolean
}; };
const GIT_CONFIG_CONTENT_TYPE = "application/vnd.scmm-gitConfig+json"; const GIT_CONFIG_CONTENT_TYPE = "application/vnd.scmm-gitConfig+json";
@@ -55,8 +56,10 @@ class RepositoryConfig extends React.Component<Props, State> {
} }
branchSelected = (branch: Branch) => { branchSelected = (branch: Branch) => {
console.log(branch)
if (!branch) { if (!branch) {
this.setState({ ...this.state, selectedBranchName: null }); this.setState({ ...this.state, selectedBranchName: null });
return;
} }
this.setState({ ...this.state, selectedBranchName: branch.name }); this.setState({ ...this.state, selectedBranchName: branch.name });
}; };
@@ -75,7 +78,13 @@ class RepositoryConfig extends React.Component<Props, State> {
newConfig, newConfig,
GIT_CONFIG_CONTENT_TYPE GIT_CONFIG_CONTENT_TYPE
) )
.then(() => this.setState({ ...this.state, submitPending: false })) .then(() =>
this.setState({
...this.state,
submitPending: false,
defaultBranchChanged: true
})
)
.catch(error => this.setState({ ...this.state, error })); .catch(error => this.setState({ ...this.state, error }));
}; };
@@ -92,29 +101,47 @@ class RepositoryConfig extends React.Component<Props, State> {
/> />
); );
} }
if (!(loadingBranches || loadingDefaultBranch)) {
if (!(loadingBranches || loadingDefaultBranch)) {
return ( return (
<form onSubmit={this.submit}> <>
<BranchSelector {this.renderBranchChangedNotification()}
label={t("scm-git-plugin.repo-config.default-branch")} <form onSubmit={this.submit}>
branches={this.state.branches} <BranchSelector
selected={this.branchSelected} label={t("scm-git-plugin.repo-config.default-branch")}
selectedBranch={this.state.selectedBranchName} branches={this.state.branches}
/> selected={this.branchSelected}
<SubmitButton selectedBranch={this.state.selectedBranchName}
label={t("scm-git-plugin.repo-config.submit")} />
loading={submitPending} <SubmitButton
disabled={ label={t("scm-git-plugin.repo-config.submit")}
!this.state.selectedBranchName loading={submitPending}
} disabled={!this.state.selectedBranchName}
/> />
</form> </form>
</>
); );
} else { } else {
return <Loading />; return <Loading />;
} }
} }
renderBranchChangedNotification = () => {
if (this.state.defaultBranchChanged) {
return (
<div className="notification is-primary">
<button
className="delete"
onClick={() =>
this.setState({ ...this.state, defaultBranchChanged: false })
}
/>
Default branch changed!
</div>
);
}
return null;
};
} }
export default translate("plugins")(RepositoryConfig); export default translate("plugins")(RepositoryConfig);

View File

@@ -79,6 +79,12 @@ class BranchSelector extends React.Component<Props, State> {
branchSelected = (branchName: string) => { branchSelected = (branchName: string) => {
const { branches, selected } = this.props; const { branches, selected } = this.props;
if (!branchName) {
this.setState({ selectedBranch: undefined });
selected(undefined);
return;
}
const branch = branches.find(b => b.name === branchName); const branch = branches.find(b => b.name === branchName);
selected(branch); selected(branch);