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

319 lines
8.7 KiB
TypeScript
Raw Normal View History

2019-10-21 10:57:56 +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";
2019-10-21 10:57:56 +02:00
import { 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";
2018-09-10 17:00:53 +02:00
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}`;
export const FETCH_CHANGESET = "scm/repos/FETCH_CHANGESET";
2018-09-20 10:06:38 +02:00
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);
}
2019-10-21 10:57:56 +02:00
export function fetchChangesetPending(repository: Repository, id: string): Action {
2018-09-20 10:06:38 +02:00
return {
type: FETCH_CHANGESET_PENDING,
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
2019-10-21 10:57:56 +02:00
export function fetchChangesetSuccess(changeset: any, repository: Repository, id: string): Action {
2018-09-20 10:06:38 +02:00
return {
type: FETCH_CHANGESET_SUCCESS,
payload: {
changeset,
repository,
id
},
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
2019-10-21 10:57:56 +02:00
function fetchChangesetFailure(repository: Repository, id: string, error: Error): Action {
2018-09-20 10:06:38 +02:00
return {
type: FETCH_CHANGESET_FAILURE,
payload: {
repository,
2018-09-20 10:06:38 +02:00
id,
error
2018-09-20 10:06:38 +02:00
},
itemId: createChangesetItemId(repository, id)
2018-09-20 10:06:38 +02:00
};
}
2018-09-10 17:00:53 +02:00
2019-10-21 10:57:56 +02:00
export function fetchChangesets(repository: Repository, branch?: Branch, page?: number) {
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
});
};
}
2019-10-21 10:57:56 +02:00
function createChangesetsLink(repository: Repository, branch?: Branch, page?: number) {
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
}
2019-10-21 10:57:56 +02:00
export function fetchChangesetsPending(repository: Repository, branch?: Branch): 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,
itemId
2018-09-19 13:49:04 +02:00
};
2018-09-10 17:00:53 +02:00
}
2019-10-21 10:57:56 +02:00
export function fetchChangesetsSuccess(repository: Repository, branch?: Branch, changesets: any): Action {
2018-09-10 17:00:53 +02:00
return {
type: FETCH_CHANGESETS_SUCCESS,
payload: {
repository,
branch,
changesets
},
itemId: createItemId(repository, branch)
2018-09-19 13:49:04 +02:00
};
2018-09-10 17:00:53 +02:00
}
2019-10-21 10:57:56 +02:00
function fetchChangesetsFailure(repository: Repository, branch?: Branch, error: Error): 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
},
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;
2018-10-18 10:52:48 +02:00
}
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 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 = {};
2019-10-21 10:57:56 +02:00
for (const changeset of changesets) {
2018-09-10 17:00:53 +02:00
changesetsByIds[changeset.id] = changeset;
}
return changesetsByIds;
}
//selectors
2019-10-21 10:57:56 +02:00
export function getChangesets(state: object, repository: Repository, branch?: Branch) {
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];
});
}
2019-10-21 10:57:56 +02:00
export function getChangeset(state: object, repository: Repository, id: string) {
2018-10-18 10:52:48 +02:00
const key = createItemId(repository);
2019-10-21 10:57:56 +02:00
const changesets = state.changesets && state.changesets[key] ? state.changesets[key].byId : null;
if (changesets != null && changesets[id]) {
return changesets[id];
}
return null;
}
2019-10-21 10:57:56 +02:00
export function shouldFetchChangeset(state: object, repository: Repository, id: string) {
if (getChangeset(state, repository, id)) {
return false;
}
return true;
}
2019-10-21 10:57:56 +02:00
export function isFetchChangesetPending(state: object, repository: Repository, id: string) {
return isPending(state, FETCH_CHANGESET, createChangesetItemId(repository, id));
}
2019-10-21 10:57:56 +02:00
export function getFetchChangesetFailure(state: object, repository: Repository, id: string) {
return getFailure(state, FETCH_CHANGESET, createChangesetItemId(repository, id));
}
2019-10-21 10:57:56 +02:00
export function isFetchChangesetsPending(state: object, repository: Repository, branch?: Branch) {
2018-10-04 17:12:38 +02:00
return isPending(state, FETCH_CHANGESETS, createItemId(repository, branch));
}
2019-10-21 10:57:56 +02:00
export function getFetchChangesetsFailure(state: object, repository: Repository, branch?: Branch) {
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 {};
};
2019-10-21 10:57:56 +02:00
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 {};
};
2019-10-21 10:57:56 +02:00
export const selectListAsCollection = (state: object, repository: Repository, branch?: Branch): PagedCollection => {
return selectListEntry(state, repository, branch);
2018-09-19 13:49:04 +02:00
};