mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-18 03:01:05 +01:00
add fetch changeset mock
This commit is contained in:
@@ -1,44 +1,153 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import {FAILURE_SUFFIX, PENDING_SUFFIX, SUCCESS_SUFFIX} from "../../modules/types";
|
import {
|
||||||
import {apiClient} from "@scm-manager/ui-components";
|
FAILURE_SUFFIX,
|
||||||
import {isPending} from "../../modules/pending";
|
PENDING_SUFFIX,
|
||||||
import {getFailure} from "../../modules/failure";
|
SUCCESS_SUFFIX
|
||||||
|
} from "../../modules/types";
|
||||||
|
import { apiClient } from "@scm-manager/ui-components";
|
||||||
|
import { isPending } from "../../modules/pending";
|
||||||
|
import { getFailure } from "../../modules/failure";
|
||||||
|
|
||||||
export const FETCH_CHANGESETS = "scm/repos/FETCH_CHANGESETS";
|
export const FETCH_CHANGESETS = "scm/repos/FETCH_CHANGESETS";
|
||||||
export const FETCH_CHANGESETS_PENDING = `${FETCH_CHANGESETS}_${PENDING_SUFFIX}`;
|
export const FETCH_CHANGESETS_PENDING = `${FETCH_CHANGESETS}_${PENDING_SUFFIX}`;
|
||||||
export const FETCH_CHANGESETS_SUCCESS = `${FETCH_CHANGESETS}_${SUCCESS_SUFFIX}`;
|
export const FETCH_CHANGESETS_SUCCESS = `${FETCH_CHANGESETS}_${SUCCESS_SUFFIX}`;
|
||||||
export const FETCH_CHANGESETS_FAILURE = `${FETCH_CHANGESETS}_${FAILURE_SUFFIX}`;
|
export const FETCH_CHANGESETS_FAILURE = `${FETCH_CHANGESETS}_${FAILURE_SUFFIX}`;
|
||||||
|
|
||||||
|
/****added for detailed view of changesets****/
|
||||||
|
|
||||||
|
export const FETCH_CHANGESET = "scm/repos/FETCH_CHANGESET";
|
||||||
|
export const FETCH_CHANGESET_PENDING = `${FETCH_CHANGESET}_${PENDING_SUFFIX}`;
|
||||||
|
export const FETCH_CHANGESET_SUCCESS = `${FETCH_CHANGESET}_${SUCCESS_SUFFIX}`;
|
||||||
|
export const FETCH_CHANGESET_FAILURE = `${FETCH_CHANGESET}_${FAILURE_SUFFIX}`;
|
||||||
|
|
||||||
|
/********/
|
||||||
|
|
||||||
const REPO_URL = "repositories";
|
const REPO_URL = "repositories";
|
||||||
|
|
||||||
|
/****added for detailed view of changesets****/
|
||||||
|
|
||||||
|
export function fetchChangesetIfNeeded(state: Object, namespace: string, repoName: string, id: string) {
|
||||||
|
return function(dispatch) {
|
||||||
|
if (shouldFetchChangeset(state, namespace, repoName, id)) {
|
||||||
|
dispatch(fetchChangeset(url));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function shouldFetchChangeset(state: Object, namespace: string, repoName: string, id: string) {
|
||||||
|
// decide if changeset should be fetched here
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchChangeset(namespace: string, repoName: string, id: string) {
|
||||||
|
return function(dispatch) {
|
||||||
|
dispatch(fetchChangesetPending(namespace, repoName, id));
|
||||||
|
return apiClient
|
||||||
|
.get(url)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(json => dispatch(fetchChangesetSuccess(namespace, repoName, id)))
|
||||||
|
.catch(err => {
|
||||||
|
dispatch(fetchChangesetFailure(namespace, repoName, id, err));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fetchChangesetPending(
|
||||||
|
namespace: string, repoName: string, id: string
|
||||||
|
): Action {
|
||||||
|
return {
|
||||||
|
type: FETCH_CHANGESET_PENDING,
|
||||||
|
payload: {
|
||||||
|
namespace,
|
||||||
|
repoName,
|
||||||
|
id
|
||||||
|
},
|
||||||
|
itemId: createItemId(namespace, repoName) + "/" + id
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fetchChangesetSuccess(
|
||||||
|
namespace: string, repoName: string, id: string
|
||||||
|
): Action {
|
||||||
|
return {
|
||||||
|
type: FETCH_CHANGESET_SUCCESS,
|
||||||
|
payload: { namespace, repoName, id },
|
||||||
|
itemId: createItemId(namespace, repoName) + "/" + id
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchChangesetFailure(
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
id: string,
|
||||||
|
error: Error
|
||||||
|
): Action {
|
||||||
|
return {
|
||||||
|
type: FETCH_CHANGESET_FAILURE,
|
||||||
|
payload: {
|
||||||
|
namespace,
|
||||||
|
name,
|
||||||
|
id,
|
||||||
|
error
|
||||||
|
},
|
||||||
|
itemId: createItemId(namespace, repoName) + "/" + id
|
||||||
|
};
|
||||||
|
|
||||||
|
/********/
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
export function fetchChangesetsByNamespaceAndName(namespace: string, name: string) {
|
export function fetchChangesetsByNamespaceAndName(
|
||||||
return function (dispatch: any) {
|
namespace: string,
|
||||||
|
name: string
|
||||||
|
) {
|
||||||
|
return function(dispatch: any) {
|
||||||
dispatch(fetchChangesetsPending(namespace, name));
|
dispatch(fetchChangesetsPending(namespace, name));
|
||||||
return apiClient.get(REPO_URL + "/" + namespace + "/" + name + "/changesets").then(response => response.json())
|
return apiClient
|
||||||
|
.get(REPO_URL + "/" + namespace + "/" + name + "/changesets")
|
||||||
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
dispatch(fetchChangesetsSuccess(data, namespace, name))
|
dispatch(fetchChangesetsSuccess(data, namespace, name));
|
||||||
}).catch(cause => {
|
|
||||||
dispatch(fetchChangesetsFailure(namespace, name, cause))
|
|
||||||
})
|
})
|
||||||
}
|
.catch(cause => {
|
||||||
|
dispatch(fetchChangesetsFailure(namespace, name, cause));
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchChangesetsByNamespaceNameAndBranch(namespace: string, name: string, branch: string) {
|
export function fetchChangesetsByNamespaceNameAndBranch(
|
||||||
return function (dispatch: any) {
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch: string
|
||||||
|
) {
|
||||||
|
return function(dispatch: any) {
|
||||||
dispatch(fetchChangesetsPending(namespace, name, branch));
|
dispatch(fetchChangesetsPending(namespace, name, branch));
|
||||||
return apiClient.get(REPO_URL + "/" + namespace + "/" + name + "/branches/" + branch + "/changesets").then(response => response.json())
|
return apiClient
|
||||||
|
.get(
|
||||||
|
REPO_URL +
|
||||||
|
"/" +
|
||||||
|
namespace +
|
||||||
|
"/" +
|
||||||
|
name +
|
||||||
|
"/branches/" +
|
||||||
|
branch +
|
||||||
|
"/changesets"
|
||||||
|
)
|
||||||
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
dispatch(fetchChangesetsSuccess(data, namespace, name, branch))
|
dispatch(fetchChangesetsSuccess(data, namespace, name, branch));
|
||||||
}).catch(cause => {
|
|
||||||
dispatch(fetchChangesetsFailure(namespace, name, branch, cause))
|
|
||||||
})
|
})
|
||||||
}
|
.catch(cause => {
|
||||||
|
dispatch(fetchChangesetsFailure(namespace, name, branch, cause));
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchChangesetsPending(namespace: string, name: string, branch?: string): Action {
|
export function fetchChangesetsPending(
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string
|
||||||
|
): Action {
|
||||||
return {
|
return {
|
||||||
type: FETCH_CHANGESETS_PENDING,
|
type: FETCH_CHANGESETS_PENDING,
|
||||||
payload: {
|
payload: {
|
||||||
@@ -47,18 +156,28 @@ export function fetchChangesetsPending(namespace: string, name: string, branch?:
|
|||||||
branch
|
branch
|
||||||
},
|
},
|
||||||
itemId: createItemId(namespace, name, branch)
|
itemId: createItemId(namespace, name, branch)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchChangesetsSuccess(collection: any, namespace: string, name: string, branch?: string): Action {
|
export function fetchChangesetsSuccess(
|
||||||
|
collection: any,
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string
|
||||||
|
): Action {
|
||||||
return {
|
return {
|
||||||
type: FETCH_CHANGESETS_SUCCESS,
|
type: FETCH_CHANGESETS_SUCCESS,
|
||||||
payload: {collection, namespace, name, branch},
|
payload: { collection, namespace, name, branch },
|
||||||
itemId: createItemId(namespace, name, branch)
|
itemId: createItemId(namespace, name, branch)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchChangesetsFailure(namespace: string, name: string, branch?: string, error: Error): Action {
|
function fetchChangesetsFailure(
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string,
|
||||||
|
error: Error
|
||||||
|
): Action {
|
||||||
return {
|
return {
|
||||||
type: FETCH_CHANGESETS_FAILURE,
|
type: FETCH_CHANGESETS_FAILURE,
|
||||||
payload: {
|
payload: {
|
||||||
@@ -68,10 +187,14 @@ function fetchChangesetsFailure(namespace: string, name: string, branch?: string
|
|||||||
error
|
error
|
||||||
},
|
},
|
||||||
itemId: createItemId(namespace, name, branch)
|
itemId: createItemId(namespace, name, branch)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function createItemId(namespace: string, name: string, branch?: string): string {
|
function createItemId(
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string
|
||||||
|
): string {
|
||||||
let itemId = namespace + "/" + name;
|
let itemId = namespace + "/" + name;
|
||||||
if (branch && branch !== "") {
|
if (branch && branch !== "") {
|
||||||
itemId = itemId + "/" + branch;
|
itemId = itemId + "/" + branch;
|
||||||
@@ -80,16 +203,27 @@ function createItemId(namespace: string, name: string, branch?: string): string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reducer
|
// reducer
|
||||||
export default function reducer(state: any = {}, action: Action = {type: "UNKNOWN"}): Object {
|
export default function reducer(
|
||||||
|
state: any = {},
|
||||||
|
action: Action = { type: "UNKNOWN" }
|
||||||
|
): Object {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case FETCH_CHANGESETS_SUCCESS:
|
case FETCH_CHANGESETS_SUCCESS:
|
||||||
const {namespace, name, branch} = action.payload;
|
const { namespace, name, branch } = action.payload;
|
||||||
const key = createItemId(namespace, name, branch);
|
const key = createItemId(namespace, name, branch);
|
||||||
let oldChangesets = {[key]: {}};
|
let oldChangesets = { [key]: {} };
|
||||||
if (state[key] !== undefined) {
|
if (state[key] !== undefined) {
|
||||||
oldChangesets[key] = state[key]
|
oldChangesets[key] = state[key];
|
||||||
}
|
}
|
||||||
return {...state, [key]: {byId: extractChangesetsByIds(action.payload.collection, oldChangesets[key].byId)}};
|
return {
|
||||||
|
...state,
|
||||||
|
[key]: {
|
||||||
|
byId: extractChangesetsByIds(
|
||||||
|
action.payload.collection,
|
||||||
|
oldChangesets[key].byId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
@@ -111,7 +245,11 @@ function extractChangesetsByIds(data: any, oldChangesetsByIds: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//selectors
|
//selectors
|
||||||
export function getChangesetsForNamespaceAndNameFromState(namespace: string, name: string, state: Object) {
|
export function getChangesetsForNamespaceAndNameFromState(
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
state: Object
|
||||||
|
) {
|
||||||
const key = createItemId(namespace, name);
|
const key = createItemId(namespace, name);
|
||||||
if (!state.changesets[key]) {
|
if (!state.changesets[key]) {
|
||||||
return null;
|
return null;
|
||||||
@@ -119,7 +257,12 @@ export function getChangesetsForNamespaceAndNameFromState(namespace: string, nam
|
|||||||
return Object.values(state.changesets[key].byId);
|
return Object.values(state.changesets[key].byId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getChangesets(state: Object, namespace: string, name: string, branch: string) {
|
export function getChangesets(
|
||||||
|
state: Object,
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch: string
|
||||||
|
) {
|
||||||
const key = createItemId(namespace, name, branch);
|
const key = createItemId(namespace, name, branch);
|
||||||
if (!state.changesets[key]) {
|
if (!state.changesets[key]) {
|
||||||
return null;
|
return null;
|
||||||
@@ -127,11 +270,28 @@ export function getChangesets(state: Object, namespace: string, name: string, br
|
|||||||
return Object.values(state.changesets[key].byId);
|
return Object.values(state.changesets[key].byId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isFetchChangesetsPending(state: Object, namespace: string, name: string, branch?: string) {
|
export function isFetchChangesetsPending(
|
||||||
return isPending(state, FETCH_CHANGESETS, createItemId(namespace, name, branch))
|
state: Object,
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string
|
||||||
|
) {
|
||||||
|
return isPending(
|
||||||
|
state,
|
||||||
|
FETCH_CHANGESETS,
|
||||||
|
createItemId(namespace, name, branch)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFetchChangesetsFailure(state: Object, namespace: string, name: string, branch?: string) {
|
export function getFetchChangesetsFailure(
|
||||||
return getFailure(state, FETCH_CHANGESETS, createItemId(namespace, name, branch));
|
state: Object,
|
||||||
|
namespace: string,
|
||||||
|
name: string,
|
||||||
|
branch?: string
|
||||||
|
) {
|
||||||
|
return getFailure(
|
||||||
|
state,
|
||||||
|
FETCH_CHANGESETS,
|
||||||
|
createItemId(namespace, name, branch)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user