Files
SCM-Manager/scm-ui/src/repos/modules/branches.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

// @flow
import {
FAILURE_SUFFIX,
PENDING_SUFFIX,
SUCCESS_SUFFIX
} from "../../modules/types";
import { apiClient } from "@scm-manager/ui-components";
2018-10-04 17:12:38 +02:00
import type { Repository } from "@scm-manager/ui-types";
2018-09-12 17:15:16 +02:00
export const FETCH_BRANCHES = "scm/repos/FETCH_BRANCHES";
export const FETCH_BRANCHES_PENDING = `${FETCH_BRANCHES}_${PENDING_SUFFIX}`;
export const FETCH_BRANCHES_SUCCESS = `${FETCH_BRANCHES}_${SUCCESS_SUFFIX}`;
export const FETCH_BRANCHES_FAILURE = `${FETCH_BRANCHES}_${FAILURE_SUFFIX}`;
// Fetching branches
2018-10-04 17:12:38 +02:00
export function fetchBranches(repository: Repository) {
return function(dispatch: any) {
2018-10-04 17:12:38 +02:00
dispatch(fetchBranchesPending(repository));
return apiClient
2018-10-04 17:12:38 +02:00
.get(repository._links.branches.href)
2018-09-12 17:15:16 +02:00
.then(response => response.json())
.then(data => {
2018-10-04 17:12:38 +02:00
dispatch(fetchBranchesSuccess(data, repository));
2018-09-12 17:15:16 +02:00
})
.catch(error => {
2018-10-04 17:12:38 +02:00
dispatch(fetchBranchesFailure(repository, error));
});
};
2018-09-12 17:15:16 +02:00
}
2018-10-04 17:12:38 +02:00
// export function fetchBranchesByNamespaceAndName(
// namespace: string,
// name: string
// ) {
// return function(dispatch: any) {
// dispatch(fetchBranchesPending(namespace, name));
// return apiClient
// .get(REPO_URL + "/" + namespace + "/" + name + "/branches")
// .then(response => response.json())
// .then(data => {
// dispatch(fetchBranchesSuccess(data, namespace, name));
// })
// .catch(error => {
// dispatch(fetchBranchesFailure(namespace, name, error));
// });
// };
// }
2018-09-12 17:15:16 +02:00
// Action creators
2018-10-04 17:12:38 +02:00
export function fetchBranchesPending(repository: Repository) {
const { namespace, name } = repository;
2018-09-12 17:15:16 +02:00
return {
type: FETCH_BRANCHES_PENDING,
2018-10-04 17:12:38 +02:00
payload: { repository },
2018-09-12 17:15:16 +02:00
itemId: namespace + "/" + name
};
2018-09-12 17:15:16 +02:00
}
2018-10-04 17:12:38 +02:00
export function fetchBranchesSuccess(data: string, repository: Repository) {
const { namespace, name } = repository;
2018-09-12 17:15:16 +02:00
return {
type: FETCH_BRANCHES_SUCCESS,
2018-10-04 17:12:38 +02:00
payload: { data, repository },
2018-09-12 17:15:16 +02:00
itemId: namespace + "/" + name
};
2018-09-12 17:15:16 +02:00
}
2018-10-04 17:12:38 +02:00
export function fetchBranchesFailure(repository: Repository, error: Error) {
const { namespace, name } = repository;
2018-09-12 17:15:16 +02:00
return {
type: FETCH_BRANCHES_FAILURE,
2018-10-04 17:12:38 +02:00
payload: { error, repository },
2018-09-12 17:15:16 +02:00
itemId: namespace + "/" + name
};
2018-09-12 17:15:16 +02:00
}
// Reducers
export default function reducer(
state: Object = {},
action: Action = { type: "UNKNOWN" }
): Object {
switch (action.type) {
case FETCH_BRANCHES_SUCCESS:
2018-10-04 17:12:38 +02:00
const key = action.itemId;
let oldBranchesByNames = { [key]: {} };
if (state[key] !== undefined) {
oldBranchesByNames[key] = state[key];
}
return {
[key]: {
byNames: extractBranchesByNames(
action.payload.data,
oldBranchesByNames[key].byNames
)
}
};
default:
return state;
}
}
function extractBranchesByNames(data: any, oldBranchesByNames: any): Branch[] {
const branches = data._embedded.branches;
const branchesByNames = {};
for (let branch of branches) {
branchesByNames[branch.name] = branch;
}
for (let name in oldBranchesByNames) {
branchesByNames[name] = oldBranchesByNames[name];
}
return branchesByNames;
}
2018-09-12 17:15:16 +02:00
// Selectors
export function getBranchesForNamespaceAndNameFromState(
namespace: string,
name: string,
state: Object
) {
const key = namespace + "/" + name;
if (!state.branches[key]) {
return null;
}
return Object.values(state.branches[key].byNames);
}
2018-09-17 14:03:13 +02:00
2018-10-04 17:12:38 +02:00
export function getBranchNames(state: Object, repository: Repository) {
const { namespace, name } = repository;
2018-09-17 14:03:13 +02:00
const key = namespace + "/" + name;
if (!state.branches[key] || !state.branches[key].byNames) {
2018-09-17 14:03:13 +02:00
return null;
}
return Object.keys(state.branches[key].byNames);
}