Restructured changeset module

This commit is contained in:
Philipp Czora
2018-09-18 16:30:37 +02:00
parent e5e931ea93
commit f75ede4bf0
2 changed files with 21 additions and 32 deletions

View File

@@ -39,21 +39,18 @@ export function fetchChangesetsByNamespaceNameAndBranch(namespace: string, name:
}
export function fetchChangesetsPending(namespace: string, name: string, branch?: string): Action {
const itemId = createItemId(namespace, name, branch);
return {
type: FETCH_CHANGESETS_PENDING,
payload: {
namespace,
name,
branch
},
itemId: createItemId(namespace, name, branch)
payload: itemId,
itemId
}
}
export function fetchChangesetsSuccess(collection: any, namespace: string, name: string, branch?: string): Action {
export function fetchChangesetsSuccess(changesets: any, namespace: string, name: string, branch?: string): Action {
return {
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace, name, branch},
payload: changesets,
itemId: createItemId(namespace, name, branch)
}
}
@@ -83,13 +80,12 @@ function createItemId(namespace: string, name: string, branch?: string): string
export default function reducer(state: any = {}, action: Action = {type: "UNKNOWN"}): Object {
switch (action.type) {
case FETCH_CHANGESETS_SUCCESS:
const {namespace, name, branch} = action.payload;
const key = createItemId(namespace, name, branch);
const key = action.itemId
let oldChangesets = {[key]: {}};
if (state[key] !== undefined) {
oldChangesets[key] = state[key]
}
return {...state, [key]: {byId: extractChangesetsByIds(action.payload.collection, oldChangesets[key].byId)}};
return {...state, [key]: {byId: extractChangesetsByIds(action.payload, oldChangesets[key].byId)}};
default:
return state;
}

View File

@@ -17,7 +17,7 @@ import {
} from "./changesets";
import reducer from "./changesets";
const collection = {};
const changesets = {};
describe("changesets", () => {
describe("fetching of changesets", () => {
@@ -35,12 +35,12 @@ describe("changesets", () => {
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"},
type: FETCH_CHANGESETS_PENDING, payload: "foo/bar",
itemId: "foo/bar"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar"},
payload: changesets,
itemId: "foo/bar"
}
];
@@ -52,17 +52,18 @@ describe("changesets", () => {
});
it("should fetch changesets for specific branch", () => {
const itemId = "foo/bar/specific";
fetchMock.getOnce(SPECIFIC_BRANCH_URL, "{}");
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
type: FETCH_CHANGESETS_PENDING, payload: itemId,
itemId
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
payload: changesets,
itemId
}
];
@@ -73,17 +74,13 @@ describe("changesets", () => {
});
it("should fail fetching changesets on error", () => {
const itemId = "foo/bar";
fetchMock.getOnce(DEFAULT_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"},
itemId: "foo/bar"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar"},
itemId: "foo/bar"
type: FETCH_CHANGESETS_PENDING, payload: itemId,
itemId
}
];
@@ -96,17 +93,13 @@ describe("changesets", () => {
})
it("should fail fetching changesets for specific branch on error", () => {
const itemId = "foo/bar/specific";
fetchMock.getOnce(SPECIFIC_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
type: FETCH_CHANGESETS_PENDING, payload: itemId,
itemId
}
];