From 2ada10ab8d8fdee974a540a4577d94f5a4cf8e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Wed, 10 Apr 2019 16:20:55 +0200 Subject: [PATCH] Fix pending states --- .../repos/branches/containers/CreateBranch.js | 16 +++--- scm-ui/src/repos/branches/modules/branches.js | 50 +++++++++++-------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/scm-ui/src/repos/branches/containers/CreateBranch.js b/scm-ui/src/repos/branches/containers/CreateBranch.js index a38d483af7..3bfc4ac651 100644 --- a/scm-ui/src/repos/branches/containers/CreateBranch.js +++ b/scm-ui/src/repos/branches/containers/CreateBranch.js @@ -11,7 +11,7 @@ import type { Repository, Branch, BranchRequest } from "@scm-manager/ui-types"; import { fetchBranches, getBranches, - getBrancheCreateLink, + getBranchCreateLink, createBranch, createBranchReset, isCreateBranchPending, @@ -39,7 +39,7 @@ type Props = { branch: BranchRequest, callback?: (Branch) => void ) => void, - resetForm: () => void, + resetForm: Repository => void, // context objects t: string => string, @@ -51,7 +51,7 @@ class CreateBranch extends React.Component { componentDidMount() { const { fetchBranches, repository } = this.props; fetchBranches(repository); - this.props.resetForm(); + this.props.resetForm(repository); } branchCreated = (branch: Branch) => { @@ -116,19 +116,21 @@ const mapDispatchToProps = dispatch => { ) => { dispatch(createBranch(createLink, repository, branchRequest, callback)); }, - resetForm: () => { - dispatch(createBranchReset()); + resetForm: (repository: Repository) => { + dispatch(createBranchReset(repository)); } }; }; const mapStateToProps = (state, ownProps) => { const { repository } = ownProps; - const loading = isFetchBranchesPending(state, repository); //|| isCreateBranchPending(state); + const loading = + isFetchBranchesPending(state, repository) || + isCreateBranchPending(state, repository); const error = getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state); const branches = getBranches(state, repository); - const createBranchesLink = getBrancheCreateLink(state, repository); + const createBranchesLink = getBranchCreateLink(state, repository); return { repository, loading, diff --git a/scm-ui/src/repos/branches/modules/branches.js b/scm-ui/src/repos/branches/modules/branches.js index 5fb3da5994..660a3e007c 100644 --- a/scm-ui/src/repos/branches/modules/branches.js +++ b/scm-ui/src/repos/branches/modules/branches.js @@ -2,7 +2,8 @@ import { FAILURE_SUFFIX, PENDING_SUFFIX, - SUCCESS_SUFFIX + SUCCESS_SUFFIX, + RESET_SUFFIX } from "../../../modules/types"; import { apiClient } from "@scm-manager/ui-components"; 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_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}`; +export const CREATE_BRANCH_SUCCESS = `${CREATE_BRANCH}_${SUCCESS_SUFFIX}`; +export const CREATE_BRANCH_FAILURE = `${CREATE_BRANCH}_${FAILURE_SUFFIX}`; +export const CREATE_BRANCH_RESET = `${CREATE_BRANCH}_${RESET_SUFFIX}`; const CONTENT_TYPE_BRANCH_REQUEST = "application/vnd.scmm-branchRequest+json;v=2"; @@ -89,19 +90,19 @@ export function createBranch( callback?: (branch: Branch) => void ) { return function(dispatch: any) { - dispatch(createBranchPending(repository, branchRequest.name)); + dispatch(createBranchPending(repository)); return apiClient .post(link, branchRequest, CONTENT_TYPE_BRANCH_REQUEST) .then(response => response.headers.get("Location")) .then(location => apiClient.get(location)) .then(response => response.json()) .then(branch => { - dispatch(createBranchSuccess()); + dispatch(createBranchSuccess(repository)); if (callback) { 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); if (repoState && repoState.list) { return repoState.list._links.create.href; @@ -191,41 +192,46 @@ export function fetchBranchesFailure(repository: Repository, error: Error) { }; } -export function isCreateBranchPending(state: Object) { - return isPending(state, CREATE_BRANCH); +export function isCreateBranchPending(state: Object, repository: Repository) { + return isPending(state, CREATE_BRANCH, createKey(repository)); } export function getCreateBranchFailure(state: Object) { return getFailure(state, CREATE_BRANCH); } -export function createBranchPending( - repository: Repository, - name: string -): Action { +export function createBranchPending(repository: Repository): Action { return { type: CREATE_BRANCH_PENDING, - payload: { repository, name }, - itemId: createKey(repository) + "/" + name + payload: { repository }, + itemId: createKey(repository) }; } -export function createBranchSuccess(): Action { +export function createBranchSuccess(repository: Repository): Action { 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 { type: CREATE_BRANCH_FAILURE, - payload: error + payload: { repository, error }, + itemId: createKey(repository) }; } -export function createBranchReset(): Action { +export function createBranchReset(repository: Repository): Action { return { - type: CREATE_BRANCH_RESET + type: CREATE_BRANCH_RESET, + payload: { repository }, + itemId: createKey(repository) }; }