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

222 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-09-10 17:00:53 +02:00
// @flow
import {
FAILURE_SUFFIX,
PENDING_SUFFIX,
SUCCESS_SUFFIX
} from "../../modules/types";
import { apiClient } 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-19 13:49:04 +02:00
//TODO: Content type
// actions
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,
2018-09-18 16:30:37 +02:00
payload: 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
}
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;
}
const payload = action.payload;
2018-09-10 17:00:53 +02:00
switch (action.type) {
case FETCH_CHANGESETS_SUCCESS:
2018-10-17 14:11:28 +02:00
const changesets = payload._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;
}
2018-09-19 13:49:04 +02:00
let oldChangesets = { [key]: {} };
2018-10-08 17:34:11 +02:00
if (state[key]) {
2018-09-19 13:49:04 +02:00
oldChangesets[key] = state[key];
}
const byIds = extractChangesetsByIds(changesets);
2018-09-19 13:49:04 +02:00
return {
...state,
2018-09-19 13:49:04 +02:00
[key]: {
byId: byIds,
2018-09-19 17:18:24 +02:00
list: {
entries: changesetIds,
entry: {
2018-10-17 14:11:28 +02:00
page: payload.page,
pageTotal: payload.pageTotal,
_links: payload._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
) {
2018-10-04 17:12:38 +02:00
const key = createItemId(repository, branch);
2018-10-17 10:38:46 +02:00
if (!state.changesets[key]) {
return null;
}
2018-10-17 10:38:46 +02:00
return Object.values(state.changesets[key].byId);
}
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 itemId = createItemId(repository, branch);
2018-10-17 10:38:46 +02:00
if (state.changesets[itemId] && state.changesets[itemId].list) {
return state.changesets[itemId].list;
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
};