Fix pending states

This commit is contained in:
René Pfeuffer
2019-04-10 16:20:55 +02:00
parent 260770d7c5
commit 2ada10ab8d
2 changed files with 37 additions and 29 deletions

View File

@@ -11,7 +11,7 @@ import type { Repository, Branch, BranchRequest } from "@scm-manager/ui-types";
import { import {
fetchBranches, fetchBranches,
getBranches, getBranches,
getBrancheCreateLink, getBranchCreateLink,
createBranch, createBranch,
createBranchReset, createBranchReset,
isCreateBranchPending, isCreateBranchPending,
@@ -39,7 +39,7 @@ type Props = {
branch: BranchRequest, branch: BranchRequest,
callback?: (Branch) => void callback?: (Branch) => void
) => void, ) => void,
resetForm: () => void, resetForm: Repository => void,
// context objects // context objects
t: string => string, t: string => string,
@@ -51,7 +51,7 @@ class CreateBranch extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { fetchBranches, repository } = this.props; const { fetchBranches, repository } = this.props;
fetchBranches(repository); fetchBranches(repository);
this.props.resetForm(); this.props.resetForm(repository);
} }
branchCreated = (branch: Branch) => { branchCreated = (branch: Branch) => {
@@ -116,19 +116,21 @@ const mapDispatchToProps = dispatch => {
) => { ) => {
dispatch(createBranch(createLink, repository, branchRequest, callback)); dispatch(createBranch(createLink, repository, branchRequest, callback));
}, },
resetForm: () => { resetForm: (repository: Repository) => {
dispatch(createBranchReset()); dispatch(createBranchReset(repository));
} }
}; };
}; };
const mapStateToProps = (state, ownProps) => { const mapStateToProps = (state, ownProps) => {
const { repository } = ownProps; const { repository } = ownProps;
const loading = isFetchBranchesPending(state, repository); //|| isCreateBranchPending(state); const loading =
isFetchBranchesPending(state, repository) ||
isCreateBranchPending(state, repository);
const error = const error =
getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state); getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state);
const branches = getBranches(state, repository); const branches = getBranches(state, repository);
const createBranchesLink = getBrancheCreateLink(state, repository); const createBranchesLink = getBranchCreateLink(state, repository);
return { return {
repository, repository,
loading, loading,

View File

@@ -2,7 +2,8 @@
import { import {
FAILURE_SUFFIX, FAILURE_SUFFIX,
PENDING_SUFFIX, PENDING_SUFFIX,
SUCCESS_SUFFIX SUCCESS_SUFFIX,
RESET_SUFFIX
} from "../../../modules/types"; } from "../../../modules/types";
import { apiClient } from "@scm-manager/ui-components"; import { apiClient } from "@scm-manager/ui-components";
import type { import type {
@@ -26,9 +27,9 @@ export const FETCH_BRANCH_FAILURE = `${FETCH_BRANCH}_${FAILURE_SUFFIX}`;
export const CREATE_BRANCH = "scm/repos/CREATE_BRANCH"; export const CREATE_BRANCH = "scm/repos/CREATE_BRANCH";
export const CREATE_BRANCH_PENDING = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_PENDING = `${CREATE_BRANCH}_${PENDING_SUFFIX}`;
export const CREATE_BRANCH_SUCCESS = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_SUCCESS = `${CREATE_BRANCH}_${SUCCESS_SUFFIX}`;
export const CREATE_BRANCH_FAILURE = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_FAILURE = `${CREATE_BRANCH}_${FAILURE_SUFFIX}`;
export const CREATE_BRANCH_RESET = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_RESET = `${CREATE_BRANCH}_${RESET_SUFFIX}`;
const CONTENT_TYPE_BRANCH_REQUEST = const CONTENT_TYPE_BRANCH_REQUEST =
"application/vnd.scmm-branchRequest+json;v=2"; "application/vnd.scmm-branchRequest+json;v=2";
@@ -89,19 +90,19 @@ export function createBranch(
callback?: (branch: Branch) => void callback?: (branch: Branch) => void
) { ) {
return function(dispatch: any) { return function(dispatch: any) {
dispatch(createBranchPending(repository, branchRequest.name)); dispatch(createBranchPending(repository));
return apiClient return apiClient
.post(link, branchRequest, CONTENT_TYPE_BRANCH_REQUEST) .post(link, branchRequest, CONTENT_TYPE_BRANCH_REQUEST)
.then(response => response.headers.get("Location")) .then(response => response.headers.get("Location"))
.then(location => apiClient.get(location)) .then(location => apiClient.get(location))
.then(response => response.json()) .then(response => response.json())
.then(branch => { .then(branch => {
dispatch(createBranchSuccess()); dispatch(createBranchSuccess(repository));
if (callback) { if (callback) {
callback(branch); callback(branch);
} }
}) })
.catch(error => dispatch(createBranchFailure(error))); .catch(error => dispatch(createBranchFailure(repository, error)));
}; };
} }
@@ -116,7 +117,7 @@ export function getBranches(state: Object, repository: Repository) {
} }
} }
export function getBrancheCreateLink(state: Object, repository: Repository) { export function getBranchCreateLink(state: Object, repository: Repository) {
const repoState = getRepoState(state, repository); const repoState = getRepoState(state, repository);
if (repoState && repoState.list) { if (repoState && repoState.list) {
return repoState.list._links.create.href; return repoState.list._links.create.href;
@@ -191,41 +192,46 @@ export function fetchBranchesFailure(repository: Repository, error: Error) {
}; };
} }
export function isCreateBranchPending(state: Object) { export function isCreateBranchPending(state: Object, repository: Repository) {
return isPending(state, CREATE_BRANCH); return isPending(state, CREATE_BRANCH, createKey(repository));
} }
export function getCreateBranchFailure(state: Object) { export function getCreateBranchFailure(state: Object) {
return getFailure(state, CREATE_BRANCH); return getFailure(state, CREATE_BRANCH);
} }
export function createBranchPending( export function createBranchPending(repository: Repository): Action {
repository: Repository,
name: string
): Action {
return { return {
type: CREATE_BRANCH_PENDING, type: CREATE_BRANCH_PENDING,
payload: { repository, name }, payload: { repository },
itemId: createKey(repository) + "/" + name itemId: createKey(repository)
}; };
} }
export function createBranchSuccess(): Action { export function createBranchSuccess(repository: Repository): Action {
return { return {
type: CREATE_BRANCH_SUCCESS type: CREATE_BRANCH_SUCCESS,
payload: { repository },
itemId: createKey(repository)
}; };
} }
export function createBranchFailure(error: Error): Action { export function createBranchFailure(
repository: Repository,
error: Error
): Action {
return { return {
type: CREATE_BRANCH_FAILURE, type: CREATE_BRANCH_FAILURE,
payload: error payload: { repository, error },
itemId: createKey(repository)
}; };
} }
export function createBranchReset(): Action { export function createBranchReset(repository: Repository): Action {
return { return {
type: CREATE_BRANCH_RESET type: CREATE_BRANCH_RESET,
payload: { repository },
itemId: createKey(repository)
}; };
} }