Added possibility to fetch changesets by branches

This commit is contained in:
Philipp Czora
2018-09-14 16:15:13 +02:00
parent 75002ffcff
commit 5687a552b8
6 changed files with 359 additions and 148 deletions

View File

@@ -50,4 +50,45 @@ export function fetchBranchesFailure(namespace: string, name: string, error: Err
// Reducers
export default function reducer(state: Object = {}, action: Action = {type: "UNKNOWN"}): Object {
switch (action.type) {
case FETCH_BRANCHES_SUCCESS:
const key = action.payload.namespace + "/" + action.payload.name;
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;
}
// 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);
}