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

392 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-09-10 17:00:53 +02:00
// @flow
2018-10-19 08:44:03 +02:00
import {FAILURE_SUFFIX, PENDING_SUFFIX, SUCCESS_SUFFIX} from "../../modules/types";
import {apiClient, urls} from "@scm-manager/ui-components";
import {isPending} from "../../modules/pending";
import {getFailure} from "../../modules/failure";
import type {Action, Branch, PagedCollection, Repository} from "@scm-manager/ui-types";
2018-09-10 17:00:53 +02:00
export const FETCH_CHANGESETS = "scm/repos/FETCH_CHANGESETS";
export const FETCH_CHANGESETS_PENDING = `${FETCH_CHANGESETS}_${PENDING_SUFFIX}`;
export const FETCH_CHANGESETS_SUCCESS = `${FETCH_CHANGESETS}_${SUCCESS_SUFFIX}`;
export const FETCH_CHANGESETS_FAILURE = `${FETCH_CHANGESETS}_${FAILURE_SUFFIX}`;
2018-09-20 10:06:38 +02:00
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}`;
// actions
2018-09-19 13:49:04 +02:00
//TODO: Content type
2018-09-20 10:06:38 +02:00
2018-10-18 10:52:48 +02:00
export function fetchChangesetIfNeeded(repository: Repository, id: string) {
2018-09-25 13:18:59 +02:00
return (dispatch: any, getState: any) => {
if (shouldFetchChangeset(getState(), repository, id)) {
return dispatch(fetchChangeset(repository, id));
2018-09-20 10:06:38 +02:00
}
};
}
2018-10-18 10:52:48 +02:00
export function fetchChangeset(repository: Repository, id: string) {
return function(dispatch: any) {
dispatch(fetchChangesetPending(repository, id));
2018-09-20 10:06:38 +02:00
return apiClient
.get(createChangesetUrl(repository, id))
2018-09-20 10:06:38 +02:00
.then(response => response.json())
2018-10-18 10:52:48 +02:00
.then(data => dispatch(fetchChangesetSuccess(data, repository, id)))
2018-09-20 10:06:38 +02:00
.catch(err => {
dispatch(fetchChangesetFailure(repository, id, err));
2018-09-20 10:06:38 +02:00
});
};
}
function createChangesetUrl(repository: Repository, id: string) {
return urls.concat(repository._links.changesets.href, id);
}
2018-09-20 10:06:38 +02:00
export function fetchChangesetPending(
repository: Repository,
2018-09-20 10:06:38 +02:00
id: string
): Action {
return {
type: FETCH_CHANGESET_PENDING,
2018-10-18 10:52:48 +02:00
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
export function fetchChangesetSuccess(
changeset: any,
repository: Repository,
2018-09-20 10:06:38 +02:00
id: string
): Action {
return {
type: FETCH_CHANGESET_SUCCESS,
payload: { changeset, repository, id },
2018-10-18 10:52:48 +02:00
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
function fetchChangesetFailure(
repository: Repository,
2018-09-20 10:06:38 +02:00
id: string,
error: Error
): Action {
return {
type: FETCH_CHANGESET_FAILURE,
payload: {
repository,
2018-09-20 10:06:38 +02:00
id,
error
},
2018-10-18 10:52:48 +02:00
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
2018-09-10 17:00:53 +02:00
2018-10-17 10:38:46 +02:00
export function fetchChangesets(
2018-10-04 17:12:38 +02:00
repository: Repository,
2018-10-17 10:38:46 +02:00
branch?: Branch,
2018-10-17 14:11:28 +02:00
page?: number
2018-09-19 17:18:24 +02:00
) {
2018-10-17 10:38:46 +02:00
const link = createChangesetsLink(repository, branch, page);
2018-09-19 17:18:24 +02:00
return function(dispatch: any) {
2018-10-04 17:12:38 +02:00
dispatch(fetchChangesetsPending(repository, branch));
2018-09-19 17:18:24 +02:00
return apiClient
.get(link)
.then(response => response.json())
.then(data => {
2018-10-17 10:38:46 +02:00
dispatch(fetchChangesetsSuccess(repository, branch, data));
2018-09-19 17:18:24 +02:00
})
.catch(cause => {
2018-10-17 10:38:46 +02:00
dispatch(fetchChangesetsFailure(repository, branch, cause));
2018-09-19 17:18:24 +02:00
});
};
}
2018-10-17 10:38:46 +02:00
function createChangesetsLink(
2018-10-04 17:12:38 +02:00
repository: Repository,
branch?: Branch,
2018-10-17 14:11:28 +02:00
page?: number
2018-09-19 13:49:04 +02:00
) {
2018-10-04 17:12:38 +02:00
let link = repository._links.changesets.href;
if (branch) {
link = branch._links.history.href;
2018-09-19 13:49:04 +02:00
}
2018-10-04 17:12:38 +02:00
2018-10-17 10:38:46 +02:00
if (page) {
link = link + `?page=${page - 1}`;
2018-09-19 13:49:04 +02:00
}
2018-10-17 10:38:46 +02:00
return link;
2018-09-19 13:49:04 +02:00
}
export function fetchChangesetsPending(
2018-10-04 17:12:38 +02:00
repository: Repository,
branch?: Branch
2018-09-19 13:49:04 +02:00
): Action {
2018-10-04 17:12:38 +02:00
const itemId = createItemId(repository, branch);
2018-10-08 17:34:11 +02:00
2018-09-10 17:00:53 +02:00
return {
type: FETCH_CHANGESETS_PENDING,
2018-09-18 16:30:37 +02:00
itemId
2018-09-19 13:49:04 +02:00
};
2018-09-10 17:00:53 +02:00
}
2018-09-19 13:49:04 +02:00
export function fetchChangesetsSuccess(
2018-10-04 17:12:38 +02:00
repository: Repository,
2018-10-17 10:38:46 +02:00
branch?: Branch,
changesets: any
2018-09-19 13:49:04 +02:00
): Action {
2018-09-10 17:00:53 +02:00
return {
type: FETCH_CHANGESETS_SUCCESS,
payload: {
repository,
branch,
changesets
},
2018-10-04 17:12:38 +02:00
itemId: createItemId(repository, branch)
2018-09-19 13:49:04 +02:00
};
2018-09-10 17:00:53 +02:00
}
2018-09-19 13:49:04 +02:00
function fetchChangesetsFailure(
2018-10-04 17:12:38 +02:00
repository: Repository,
2018-10-17 10:38:46 +02:00
branch?: Branch,
error: Error
2018-09-19 13:49:04 +02:00
): Action {
2018-09-10 17:00:53 +02:00
return {
type: FETCH_CHANGESETS_FAILURE,
payload: {
2018-10-04 17:12:38 +02:00
repository,
error,
branch
},
2018-10-04 17:12:38 +02:00
itemId: createItemId(repository, branch)
2018-09-19 13:49:04 +02:00
};
2018-09-10 17:00:53 +02:00
}
2018-10-18 10:52:48 +02:00
function createChangesetItemId(repository: Repository, id: string) {
const { namespace, name } = repository;
return namespace + "/" + name + "/" + id;
}
function createItemId(repository: Repository, branch?: Branch): string {
2018-10-04 17:12:38 +02:00
const { namespace, name } = repository;
let itemId = namespace + "/" + name;
2018-10-17 10:38:46 +02:00
if (branch) {
itemId = itemId + "/" + branch.name;
}
return itemId;
}
// reducer
2018-10-17 10:38:46 +02:00
export default function reducer(
2018-09-19 13:49:04 +02:00
state: any = {},
action: Action = { type: "UNKNOWN" }
): Object {
2018-10-17 14:11:28 +02:00
if (!action.payload) {
return state;
}
2018-10-18 10:52:48 +02:00
2018-10-17 14:11:28 +02:00
const payload = action.payload;
2018-09-10 17:00:53 +02:00
switch (action.type) {
case FETCH_CHANGESET_SUCCESS:
2018-10-18 10:52:48 +02:00
const _key = createItemId(payload.repository);
let _oldByIds = {};
if (state[_key] && state[_key].byId) {
_oldByIds = state[_key].byId;
}
2018-10-18 10:52:48 +02:00
const changeset = payload.changeset;
return {
...state,
[_key]: {
2018-10-18 10:52:48 +02:00
...state[_key],
byId: {
..._oldByIds,
[changeset.id]: changeset
}
}
};
2018-10-18 10:52:48 +02:00
2018-09-10 17:00:53 +02:00
case FETCH_CHANGESETS_SUCCESS:
const changesets = payload.changesets._embedded.changesets;
2018-09-19 17:18:24 +02:00
const changesetIds = changesets.map(c => c.id);
2018-09-19 13:49:04 +02:00
const key = action.itemId;
2018-10-08 17:34:11 +02:00
if (!key) {
return state;
}
const repoId = createItemId(payload.repository);
let oldState = {};
if (state[repoId]) {
oldState = state[repoId];
}
const branchName = payload.branch ? payload.branch.name : "";
const byIds = extractChangesetsByIds(changesets);
2018-09-19 13:49:04 +02:00
return {
...state,
[repoId]: {
byId: {
...oldState.byId,
...byIds
},
byBranch: {
...oldState.byBranch,
[branchName]: {
entries: changesetIds,
entry: {
page: payload.changesets.page,
pageTotal: payload.changesets.pageTotal,
_links: payload.changesets._links
}
2018-09-19 17:18:24 +02:00
}
}
2018-09-19 13:49:04 +02:00
}
};
2018-09-10 17:00:53 +02:00
default:
return state;
}
}
function extractChangesetsByIds(changesets: any) {
2018-09-10 17:00:53 +02:00
const changesetsByIds = {};
2018-09-10 17:00:53 +02:00
for (let changeset of changesets) {
changesetsByIds[changeset.id] = changeset;
}
return changesetsByIds;
}
//selectors
2018-10-05 09:55:17 +02:00
export function getChangesets(
state: Object,
repository: Repository,
2018-10-08 17:34:11 +02:00
branch?: Branch
2018-10-05 09:55:17 +02:00
) {
const repoKey = createItemId(repository);
const stateRoot = state.changesets[repoKey];
if (!stateRoot || !stateRoot.byBranch) {
return null;
}
const branchName = branch ? branch.name : "";
const changesets = stateRoot.byBranch[branchName];
if (!changesets) {
return null;
}
return changesets.entries.map((id: string) => {
return stateRoot.byId[id];
});
}
export function getChangeset(
state: Object,
repository: Repository,
2018-10-18 10:52:48 +02:00
id: string
) {
2018-10-18 10:52:48 +02:00
const key = createItemId(repository);
2018-09-25 14:11:18 +02:00
const changesets =
2018-10-18 10:52:48 +02:00
state.changesets && state.changesets[key]
? state.changesets[key].byId
2018-09-25 14:11:18 +02:00
: null;
if (changesets != null && changesets[id]) {
return changesets[id];
}
return null;
}
export function shouldFetchChangeset(
state: Object,
repository: Repository,
id: string
) {
if (getChangeset(state, repository, id)) {
return false;
}
return true;
}
export function isFetchChangesetPending(
state: Object,
repository: Repository,
id: string
) {
2018-10-18 10:52:48 +02:00
return isPending(
state,
FETCH_CHANGESET,
createChangesetItemId(repository, id)
);
}
export function getFetchChangesetFailure(
state: Object,
repository: Repository,
id: string
) {
2018-10-18 10:52:48 +02:00
return getFailure(
state,
FETCH_CHANGESET,
createChangesetItemId(repository, id)
);
}
2018-09-19 13:49:04 +02:00
export function isFetchChangesetsPending(
state: Object,
2018-10-04 17:12:38 +02:00
repository: Repository,
2018-10-08 17:34:11 +02:00
branch?: Branch
2018-09-19 13:49:04 +02:00
) {
2018-10-04 17:12:38 +02:00
return isPending(state, FETCH_CHANGESETS, createItemId(repository, branch));
}
2018-09-19 13:49:04 +02:00
export function getFetchChangesetsFailure(
state: Object,
2018-10-04 17:12:38 +02:00
repository: Repository,
2018-10-08 17:34:11 +02:00
branch?: Branch
2018-09-19 13:49:04 +02:00
) {
2018-10-04 17:12:38 +02:00
return getFailure(state, FETCH_CHANGESETS, createItemId(repository, branch));
}
const selectList = (state: Object, repository: Repository, branch?: Branch) => {
const repoId = createItemId(repository);
const branchName = branch ? branch.name : "";
if (state.changesets[repoId]) {
const repoState = state.changesets[repoId];
if (repoState.byBranch && repoState.byBranch[branchName]) {
return repoState.byBranch[branchName];
}
2018-09-19 13:49:04 +02:00
}
return {};
};
const selectListEntry = (
state: Object,
repository: Repository,
branch?: Branch
): Object => {
const list = selectList(state, repository, branch);
2018-09-19 13:49:04 +02:00
if (list.entry) {
return list.entry;
}
return {};
};
2018-09-19 17:18:24 +02:00
export const selectListAsCollection = (
state: Object,
repository: Repository,
branch?: Branch
2018-09-19 17:18:24 +02:00
): PagedCollection => {
return selectListEntry(state, repository, branch);
2018-09-19 13:49:04 +02:00
};