mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
created pending and failure module
This commit is contained in:
@@ -6,6 +6,8 @@ import { routerReducer, routerMiddleware } from "react-router-redux";
|
||||
|
||||
import users from "./users/modules/users";
|
||||
import auth from "./modules/auth";
|
||||
import pending from "./modules/pending";
|
||||
import failure from "./modules/failure";
|
||||
|
||||
import type { BrowserHistory } from "history/createBrowserHistory";
|
||||
|
||||
@@ -15,6 +17,8 @@ function createReduxStore(history: BrowserHistory) {
|
||||
|
||||
const reducer = combineReducers({
|
||||
router: routerReducer,
|
||||
pending,
|
||||
failure,
|
||||
users,
|
||||
auth
|
||||
});
|
||||
|
||||
65
scm-ui/src/modules/failure.js
Normal file
65
scm-ui/src/modules/failure.js
Normal file
@@ -0,0 +1,65 @@
|
||||
// @flow
|
||||
import type { Action } from "../types/Action";
|
||||
|
||||
const FAILURE_SUFFIX = "_FAILURE";
|
||||
const RESET_PATTERN = /^(.*)_(SUCCESS|RESET)$/;
|
||||
|
||||
function extractIdentifierFromFailure(action: Action) {
|
||||
const type = action.type;
|
||||
let identifier = type.substring(0, type.length - FAILURE_SUFFIX.length);
|
||||
if (action.itemId) {
|
||||
identifier += "/" + action.itemId;
|
||||
}
|
||||
return identifier;
|
||||
}
|
||||
|
||||
function removeFromState(state: Object, identifier: string) {
|
||||
const newState = {};
|
||||
for (let failureType in state) {
|
||||
if (failureType !== identifier) {
|
||||
newState[failureType] = state[failureType];
|
||||
}
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
|
||||
export default function reducer(state: Object = {}, action: Action): Object {
|
||||
const type = action.type;
|
||||
if (type.endsWith(FAILURE_SUFFIX)) {
|
||||
const identifier = extractIdentifierFromFailure(action);
|
||||
let payload;
|
||||
if (action.payload instanceof Error) {
|
||||
payload = action.payload;
|
||||
} else if (action.payload) {
|
||||
payload = action.payload.error;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
[identifier]: payload
|
||||
};
|
||||
} else {
|
||||
const match = RESET_PATTERN.exec(type);
|
||||
if (match) {
|
||||
let identifier = match[1];
|
||||
if (action.itemId) {
|
||||
identifier += "/" + action.itemId;
|
||||
}
|
||||
return removeFromState(state, identifier);
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
export function getFailure(
|
||||
state: Object,
|
||||
actionType: string,
|
||||
itemId?: string | number
|
||||
) {
|
||||
if (state.failure) {
|
||||
let identifier = actionType;
|
||||
if (itemId) {
|
||||
identifier += "/" + itemId;
|
||||
}
|
||||
return state.failure[identifier];
|
||||
}
|
||||
}
|
||||
104
scm-ui/src/modules/failure.test.js
Normal file
104
scm-ui/src/modules/failure.test.js
Normal file
@@ -0,0 +1,104 @@
|
||||
// @flow
|
||||
import reducer, { getFailure } from "./failure";
|
||||
|
||||
const err = new Error("something failed");
|
||||
const otherErr = new Error("something else failed");
|
||||
|
||||
describe("failure reducer", () => {
|
||||
it("should set the error for FETCH_ITEMS", () => {
|
||||
const newState = reducer({}, { type: "FETCH_ITEMS_FAILURE", payload: err });
|
||||
expect(newState["FETCH_ITEMS"]).toBe(err);
|
||||
});
|
||||
|
||||
it("should set the error for FETCH_ITEMS, if payload has multiple values", () => {
|
||||
const newState = reducer(
|
||||
{},
|
||||
{
|
||||
type: "FETCH_ITEMS_FAILURE",
|
||||
payload: { something: "something", error: err }
|
||||
}
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBe(err);
|
||||
});
|
||||
|
||||
it("should set the error for FETCH_ITEMS, but should not affect others", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_USERS: otherErr
|
||||
},
|
||||
{ type: "FETCH_ITEMS_FAILURE", payload: err }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBe(err);
|
||||
expect(newState["FETCH_USERS"]).toBe(otherErr);
|
||||
});
|
||||
|
||||
it("should reset FETCH_ITEMS after FETCH_ITEMS_SUCCESS", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: err
|
||||
},
|
||||
{ type: "FETCH_ITEMS_SUCCESS" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should reset FETCH_ITEMS after FETCH_ITEMS_RESET", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: err
|
||||
},
|
||||
{ type: "FETCH_ITEMS_RESET" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should set the error for a single item of FETCH_ITEM", () => {
|
||||
const newState = reducer(
|
||||
{},
|
||||
{ type: "FETCH_ITEM_FAILURE", payload: err, itemId: 42 }
|
||||
);
|
||||
expect(newState["FETCH_ITEM/42"]).toBe(err);
|
||||
});
|
||||
});
|
||||
|
||||
describe("failure selector", () => {
|
||||
it("should return failure, if FETCH_ITEMS failure exists", () => {
|
||||
const failure = getFailure(
|
||||
{
|
||||
failure: {
|
||||
FETCH_ITEMS: err
|
||||
}
|
||||
},
|
||||
"FETCH_ITEMS"
|
||||
);
|
||||
expect(failure).toBe(err);
|
||||
});
|
||||
|
||||
it("should return undefined, if state has no failure", () => {
|
||||
const failure = getFailure({}, "FETCH_ITEMS");
|
||||
expect(failure).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined, if FETCH_ITEMS is not defined", () => {
|
||||
const failure = getFailure(
|
||||
{
|
||||
failure: {}
|
||||
},
|
||||
"FETCH_ITEMS"
|
||||
);
|
||||
expect(failure).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should return failure, if FETCH_ITEM 42 failure exists", () => {
|
||||
const failure = getFailure(
|
||||
{
|
||||
failure: {
|
||||
"FETCH_ITEM/42": err
|
||||
}
|
||||
},
|
||||
"FETCH_ITEM",
|
||||
42
|
||||
);
|
||||
expect(failure).toBe(err);
|
||||
});
|
||||
});
|
||||
56
scm-ui/src/modules/pending.js
Normal file
56
scm-ui/src/modules/pending.js
Normal file
@@ -0,0 +1,56 @@
|
||||
// @flow
|
||||
import type { Action } from "../types/Action";
|
||||
|
||||
const PENDING_SUFFIX = "_PENDING";
|
||||
const RESET_PATTERN = /^(.*)_(SUCCESS|FAILURE|RESET)$/;
|
||||
|
||||
function removeFromState(state: Object, identifier: string) {
|
||||
let newState = {};
|
||||
for (let childType in state) {
|
||||
if (childType !== identifier) {
|
||||
newState[childType] = state[childType];
|
||||
}
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
|
||||
function extractIdentifierFromPending(action: Action) {
|
||||
const type = action.type;
|
||||
let identifier = type.substring(0, type.length - PENDING_SUFFIX.length);
|
||||
if (action.itemId) {
|
||||
identifier += "/" + action.itemId;
|
||||
}
|
||||
return identifier;
|
||||
}
|
||||
|
||||
export default function reducer(state: Object = {}, action: Action): Object {
|
||||
const type = action.type;
|
||||
if (type.endsWith(PENDING_SUFFIX)) {
|
||||
const identifier = extractIdentifierFromPending(action);
|
||||
return {
|
||||
...state,
|
||||
[identifier]: true
|
||||
};
|
||||
} else {
|
||||
const matches = RESET_PATTERN.exec(type);
|
||||
if (matches) {
|
||||
return removeFromState(state, matches[1]);
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
export function isPending(
|
||||
state: Object,
|
||||
actionType: string,
|
||||
itemId?: string | number
|
||||
) {
|
||||
let type = actionType;
|
||||
if (itemId) {
|
||||
type += "/" + itemId;
|
||||
}
|
||||
if (state.pending && state.pending[type]) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
117
scm-ui/src/modules/pending.test.js
Normal file
117
scm-ui/src/modules/pending.test.js
Normal file
@@ -0,0 +1,117 @@
|
||||
import reducer, { isPending } from "./pending";
|
||||
|
||||
describe("pending reducer", () => {
|
||||
it("should set pending for FETCH_ITEMS to true", () => {
|
||||
const newState = reducer({}, { type: "FETCH_ITEMS_PENDING" });
|
||||
expect(newState["FETCH_ITEMS"]).toBe(true);
|
||||
});
|
||||
|
||||
it("should set pending for FETCH_ITEMS to true, but should not affect others", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_USERS: true
|
||||
},
|
||||
{ type: "FETCH_ITEMS_PENDING" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBe(true);
|
||||
expect(newState["FETCH_USERS"]).toBe(true);
|
||||
});
|
||||
|
||||
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_SUCCESS", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: true
|
||||
},
|
||||
{ type: "FETCH_ITEMS_SUCCESS" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_FAILURE", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: true
|
||||
},
|
||||
{ type: "FETCH_ITEMS_FAILURE" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_RESET", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_ITEMS: true
|
||||
},
|
||||
{ type: "FETCH_ITEMS_RESET" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should reset pending state for FETCH_ITEMS after FETCH_ITEMS_SUCCESS, but should not affect others", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
FETCH_USERS: true,
|
||||
FETCH_ITEMS: true
|
||||
},
|
||||
{ type: "FETCH_ITEMS_SUCCESS" }
|
||||
);
|
||||
expect(newState["FETCH_ITEMS"]).toBeFalsy();
|
||||
expect(newState["FETCH_USERS"]).toBe(true);
|
||||
});
|
||||
|
||||
it("should set pending for a single item", () => {
|
||||
const newState = reducer(
|
||||
{
|
||||
"FETCH_USER/42": false
|
||||
},
|
||||
{ type: "FETCH_USER_PENDING", itemId: 21 }
|
||||
);
|
||||
expect(newState["FETCH_USER/21"]).toBe(true);
|
||||
expect(newState["FETCH_USER/42"]).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pending selectors", () => {
|
||||
it("should return true, while FETCH_ITEMS is pending", () => {
|
||||
const result = isPending(
|
||||
{
|
||||
pending: {
|
||||
FETCH_ITEMS: true
|
||||
}
|
||||
},
|
||||
"FETCH_ITEMS"
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false, if pending is not defined", () => {
|
||||
const result = isPending({}, "FETCH_ITEMS");
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true, while FETCH_ITEM 42 is pending", () => {
|
||||
const result = isPending(
|
||||
{
|
||||
pending: {
|
||||
"FETCH_ITEM/42": true
|
||||
}
|
||||
},
|
||||
"FETCH_ITEM",
|
||||
42
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true, while FETCH_ITEM 42 is undefined", () => {
|
||||
const result = isPending(
|
||||
{
|
||||
pending: {
|
||||
"FETCH_ITEM/21": true
|
||||
}
|
||||
},
|
||||
"FETCH_ITEM",
|
||||
42
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
// @flow
|
||||
export type Action = {
|
||||
type: string,
|
||||
payload?: any
|
||||
payload?: any,
|
||||
itemId?: string | number
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user