started creating branch form

This commit is contained in:
Florian Scholdei
2019-04-03 13:12:07 +02:00
parent 148f05daee
commit d4e996dde2
3 changed files with 274 additions and 23 deletions

View File

@@ -1,16 +1,103 @@
// @flow // @flow
import React from "react"; import React from "react";
import { translate } from "react-i18next";
import type { Repository, Branch } from "@scm-manager/ui-types";
import {
Select,
InputField,
SubmitButton,
validation as validator
} from "@scm-manager/ui-components";
type Props = {}; type Props = {
submitForm: Branch => void,
repository: Repository,
branches: Branch[],
loading?: boolean,
t: string => string
};
type State = {
source?: string,
name?: string,
nameValidationError: boolean
};
class BranchForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
nameValidationError: false
};
}
isValid = () => {
return true; //TODO
};
submit = (event: Event) => {
event.preventDefault();
if (this.isValid()) {
this.props.submitForm(this.state.branch);
}
};
class CreateBranch extends React.Component<Props> {
render() { render() {
const { t, branches } = this.props;
const { loading } = this.state;
const options = branches.map(branch => ({
label: branch.name,
value: branch.name
}));
return ( return (
<> <>
<p>Form placeholder</p> <form onSubmit={this.submit}>
<div className="columns">
<div className="column">
<Select
name="source"
label={t("branches.create.source")}
options={options}
onChange={this.handleSourceChange}
loading={loading}
/>
<InputField
name="name"
label={t("branches.create.name")}
onChange={this.handleNameChange}
/>
</div>
</div>
<div className="columns">
<div className="column">
<SubmitButton
disabled={!this.isValid()}
loading={loading}
label={t("branches.create.submit")}
/>
</div>
</div>
</form>
</> </>
); );
} }
handleSourceChange = (source: string) => {
this.setState({
...this.state,
source
});
};
handleNameChange = (name: string) => {
this.setState({
nameValidationError: !validator.isNameValid(name),
...this.state,
name
});
};
} }
export default translate("repos")(BranchForm); export default translate("repos")(BranchForm);

View File

@@ -1,23 +1,109 @@
//@flow //@flow
import React from "react"; import React from "react";
import { Subtitle } from "@scm-manager/ui-components"; import { ErrorNotification, Loading, Subtitle } from "@scm-manager/ui-components";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import BranchForm from "../components/BranchForm";
import type { Repository, Branch } from "@scm-manager/ui-types";
import {
fetchBranches,
getBranches,
createBranch,
createBranchReset,
isCreateBranchPending,
getCreateBranchFailure, isFetchBranchesPending, getFetchBranchesFailure
} from "../modules/branches";
import type { History } from "history";
import { connect } from "react-redux";
type Props = { type Props = {
t: string => string loading?: boolean,
error?: Error,
repository: Repository,
branches: Branch[],
// dispatcher functions
fetchBranches: Repository => void,
createBranch: (branch: Branch, callback?: () => void) => void,
resetForm: () => void,
// context objects
t: string => string,
history: History
}; };
class CreateBranch extends React.Component<Props> { class CreateBranch extends React.Component<Props> {
componentDidMount() {
const { fetchBranches, repository } = this.props;
fetchBranches(repository);
this.props.resetForm();
}
branchCreated = (branch: Branch) => {
const { history } = this.props;
history.push("/branch/" + encodeURIComponent(branch.name) + "/info");
};
createBranch = (branch: Branch) => {
this.props.createBranch(branch, () => this.branchCreated(branch));
};
render() { render() {
const { t } = this.props; const { t, loading, error, repository, branches } = this.props;
if (error) {
return <ErrorNotification error={error} />;
}
if(!branches) {
return <Loading/>;
}
return ( return (
<> <>
<Subtitle subtitle={t("branches.create.title")} /> <Subtitle subtitle={t("branches.create.title")} />
<p>Create placeholder</p> <BranchForm
submitForm={branch => this.createBranch(branch)}
loading={loading}
repository={repository}
branches={branches}
/>
</> </>
); );
} }
} }
export default translate("repos")(CreateBranch); const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
},
createBranch: (
repository: Repository,
branch: Branch,
callback?: () => void
) => {
dispatch(createBranch("ghjgkj", repository, branch, callback)); //TODO
},
resetForm: () => {
dispatch(createBranchReset());
}
};
};
const mapStateToProps = (state, ownProps) => {
const { repository } = ownProps;
const loading = isFetchBranchesPending(state, repository) || isCreateBranchPending(state);
const error = getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state);
const branches = getBranches(state, repository);
return {
repository,
loading,
error,
branches
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("repos")(CreateBranch));

View File

@@ -2,7 +2,8 @@
import { import {
FAILURE_SUFFIX, FAILURE_SUFFIX,
PENDING_SUFFIX, PENDING_SUFFIX,
SUCCESS_SUFFIX SUCCESS_SUFFIX,
default as types
} from "../../../modules/types"; } from "../../../modules/types";
import { apiClient } from "@scm-manager/ui-components"; import { apiClient } from "@scm-manager/ui-components";
import type { Action, Branch, Repository } from "@scm-manager/ui-types"; import type { Action, Branch, Repository } from "@scm-manager/ui-types";
@@ -19,6 +20,14 @@ export const FETCH_BRANCH_PENDING = `${FETCH_BRANCH}_${PENDING_SUFFIX}`;
export const FETCH_BRANCH_SUCCESS = `${FETCH_BRANCH}_${SUCCESS_SUFFIX}`; export const FETCH_BRANCH_SUCCESS = `${FETCH_BRANCH}_${SUCCESS_SUFFIX}`;
export const FETCH_BRANCH_FAILURE = `${FETCH_BRANCH}_${FAILURE_SUFFIX}`; export const FETCH_BRANCH_FAILURE = `${FETCH_BRANCH}_${FAILURE_SUFFIX}`;
export const CREATE_BRANCH = "scm/repos/CREATE_BRANCH";
export const CREATE_BRANCH_PENDING = `${CREATE_BRANCH}_${PENDING_SUFFIX}`;
export const CREATE_BRANCH_SUCCESS = `${CREATE_BRANCH}_${PENDING_SUFFIX}`;
export const CREATE_BRANCH_FAILURE = `${CREATE_BRANCH}_${PENDING_SUFFIX}`;
export const CREATE_BRANCH_RESET = `${CREATE_BRANCH}_${PENDING_SUFFIX}`;
const CONTENT_TYPE_BRANCH = "application/vnd.scmm-branch+json;v=2";
// Fetching branches // Fetching branches
export function fetchBranches(repository: Repository) { export function fetchBranches(repository: Repository) {
@@ -44,10 +53,7 @@ export function fetchBranches(repository: Repository) {
}; };
} }
export function fetchBranch( export function fetchBranch(repository: Repository, name: string) {
repository: Repository,
name: string
) {
let link = repository._links.branches.href; let link = repository._links.branches.href;
if (!link.endsWith("/")) { if (!link.endsWith("/")) {
link += "/"; link += "/";
@@ -69,6 +75,28 @@ export function fetchBranch(
}; };
} }
// create branch
export function createBranch(
link: string,
repository: Repository,
branch: Branch,
callback?: () => void
) {
return function(dispatch: any) {
dispatch(createBranchPending(repository, branch.name));
return apiClient
.post(link, branch, CONTENT_TYPE_BRANCH)
.then(() => {
dispatch(createBranchSuccess());
if (callback) {
callback();
}
})
.catch(error => dispatch(createBranchFailure(error)));
};
}
// Selectors // Selectors
export function getBranches(state: Object, repository: Repository) { export function getBranches(state: Object, repository: Repository) {
@@ -79,6 +107,10 @@ export function getBranches(state: Object, repository: Repository) {
return null; return null;
} }
export const isPermittedToCreateBranch = (state: Object): boolean => {
return false; // TODO
};
export function getBranch( export function getBranch(
state: Object, state: Object,
repository: Repository, repository: Repository,
@@ -103,14 +135,6 @@ export function getFetchBranchesFailure(state: Object, repository: Repository) {
return getFailure(state, FETCH_BRANCHES, createKey(repository)); return getFailure(state, FETCH_BRANCHES, createKey(repository));
} }
export function isFetchBranchPending(state: Object, repository: Repository, name: string) {
return isPending(state, FETCH_BRANCH, createKey(repository) + "/" + name);
}
export function getFetchBranchFailure(state: Object, repository: Repository, name: string) {
return getFailure(state, FETCH_BRANCH, createKey(repository) + "/" + name);
}
export function fetchBranchesPending(repository: Repository) { export function fetchBranchesPending(repository: Repository) {
return { return {
type: FETCH_BRANCHES_PENDING, type: FETCH_BRANCHES_PENDING,
@@ -135,6 +159,60 @@ export function fetchBranchesFailure(repository: Repository, error: Error) {
}; };
} }
export function isCreateBranchPending(state: Object) {
return isPending(state, CREATE_BRANCH);
}
export function getCreateBranchFailure(state: Object) {
return getFailure(state, CREATE_BRANCH);
}
export function createBranchPending(
repository: Repository,
name: string
): Action {
return {
type: CREATE_BRANCH_PENDING,
payload: { repository, name },
itemId: createKey(repository) + "/" + name
};
}
export function createBranchSuccess(): Action {
return {
type: CREATE_BRANCH_SUCCESS
};
}
export function createBranchFailure(error: Error): Action {
return {
type: CREATE_BRANCH_FAILURE,
payload: error
};
}
export function createBranchReset(): Action {
return {
type: CREATE_BRANCH_RESET
};
}
export function isFetchBranchPending(
state: Object,
repository: Repository,
name: string
) {
return isPending(state, FETCH_BRANCH, createKey(repository) + "/" + name);
}
export function getFetchBranchFailure(
state: Object,
repository: Repository,
name: string
) {
return getFailure(state, FETCH_BRANCH, createKey(repository) + "/" + name);
}
export function fetchBranchPending( export function fetchBranchPending(
repository: Repository, repository: Repository,
name: string name: string