2019-10-19 16:38:07 +02:00
|
|
|
import { Action } from '@scm-manager/ui-types';
|
|
|
|
|
import * as types from './types';
|
2018-07-30 11:18:20 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
const PENDING_SUFFIX = '_' + types.PENDING_SUFFIX;
|
2018-07-30 15:29:23 +02:00
|
|
|
const RESET_ACTIONTYPES = [
|
|
|
|
|
types.SUCCESS_SUFFIX,
|
|
|
|
|
types.FAILURE_SUFFIX,
|
2019-10-19 16:38:07 +02:00
|
|
|
types.RESET_SUFFIX,
|
2018-07-30 15:29:23 +02:00
|
|
|
];
|
2018-07-30 11:18:20 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
function removeFromState(state: object, identifier: string) {
|
2018-07-30 11:18:20 +02:00
|
|
|
let newState = {};
|
|
|
|
|
for (let childType in state) {
|
|
|
|
|
if (childType !== identifier) {
|
|
|
|
|
newState[childType] = state[childType];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newState;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-11 15:45:55 +02:00
|
|
|
function removeAllEntriesOfIdentifierFromState(
|
2019-10-19 16:38:07 +02:00
|
|
|
state: object,
|
2018-09-11 15:45:55 +02:00
|
|
|
payload: any,
|
2019-10-19 16:38:07 +02:00
|
|
|
identifier: string,
|
2018-09-11 15:45:55 +02:00
|
|
|
) {
|
|
|
|
|
const newState = {};
|
|
|
|
|
for (let childType in state) {
|
|
|
|
|
if (childType !== identifier && !childType.startsWith(identifier)) {
|
|
|
|
|
newState[childType] = state[childType];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newState;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 11:18:20 +02:00
|
|
|
function extractIdentifierFromPending(action: Action) {
|
|
|
|
|
const type = action.type;
|
|
|
|
|
let identifier = type.substring(0, type.length - PENDING_SUFFIX.length);
|
|
|
|
|
if (action.itemId) {
|
2019-10-19 16:38:07 +02:00
|
|
|
identifier += '/' + action.itemId;
|
2018-07-30 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
return identifier;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 15:51:45 +02:00
|
|
|
export default function reducer(
|
2019-10-19 16:38:07 +02:00
|
|
|
state: object = {},
|
|
|
|
|
action: Action = {
|
|
|
|
|
type: 'UNKNOWN',
|
|
|
|
|
},
|
|
|
|
|
): object {
|
2018-07-30 11:18:20 +02:00
|
|
|
const type = action.type;
|
|
|
|
|
if (type.endsWith(PENDING_SUFFIX)) {
|
|
|
|
|
const identifier = extractIdentifierFromPending(action);
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
2019-10-19 16:38:07 +02:00
|
|
|
[identifier]: true,
|
2018-07-30 11:18:20 +02:00
|
|
|
};
|
|
|
|
|
} else {
|
2019-10-19 16:38:07 +02:00
|
|
|
const index = type.lastIndexOf('_');
|
2018-07-30 15:29:23 +02:00
|
|
|
if (index > 0) {
|
|
|
|
|
const actionType = type.substring(index + 1);
|
|
|
|
|
if (RESET_ACTIONTYPES.indexOf(actionType) >= 0 || action.resetPending) {
|
|
|
|
|
let identifier = type.substring(0, index);
|
|
|
|
|
if (action.itemId) {
|
2019-10-19 16:38:07 +02:00
|
|
|
identifier += '/' + action.itemId;
|
2018-07-30 15:29:23 +02:00
|
|
|
}
|
2018-09-11 15:45:55 +02:00
|
|
|
if (action.payload)
|
2019-10-19 16:38:07 +02:00
|
|
|
return removeAllEntriesOfIdentifierFromState(
|
|
|
|
|
state,
|
|
|
|
|
action.payload,
|
|
|
|
|
identifier,
|
|
|
|
|
);
|
|
|
|
|
else return removeFromState(state, identifier);
|
2018-07-30 13:38:15 +02:00
|
|
|
}
|
2018-07-30 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isPending(
|
2019-10-19 16:38:07 +02:00
|
|
|
state: object,
|
2018-07-30 11:18:20 +02:00
|
|
|
actionType: string,
|
2019-10-19 16:38:07 +02:00
|
|
|
itemId?: string | number,
|
2018-07-30 11:18:20 +02:00
|
|
|
) {
|
|
|
|
|
let type = actionType;
|
|
|
|
|
if (itemId) {
|
2019-10-19 16:38:07 +02:00
|
|
|
type += '/' + itemId;
|
2018-07-30 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
if (state.pending && state.pending[type]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|