Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add import effect #2

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ You will have to write actions with the following params:

```

Your service will receive an object with the nextPage prop.

## Middlewares

Middlewares allow to inject logic between dispatching the action and the actual desired change in the store. Middlewares are particularly helpful when handling asynchronous actions.
Expand Down
33 changes: 14 additions & 19 deletions src/completers/completeReducer/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import onLoading from "../../effects/onLoading";
import onSuccess from "../../effects/onSuccess";
import onFailure from "../../effects/onFailure";
import onLoading from '../../effects/onLoading';
import onSuccess from '../../effects/onSuccess';
import onSuccessPagination from '../../effects/onSuccessPagination';
import onFailure from '../../effects/onFailure';

import onSubscribe from "../../effects/onSubscribe";
import onUnsubscribe from "../../effects/onUnsubscribe";
import onSubscribe from '../../effects/onSubscribe';
import onUnsubscribe from '../../effects/onUnsubscribe';

import { isStringArray, isValidObject } from "../../utils/typeUtils";
import { isStringArray, isValidObject } from '../../utils/typeUtils';

// Given a reducer description, it returns a reducerHandler with all success and failure cases
function completeReducer(reducerDescription) {
if (
!reducerDescription ||
((!reducerDescription.primaryActions ||
!reducerDescription.primaryActions.length) &&
(!reducerDescription.modalActions ||
!reducerDescription.modalActions.length))
((!reducerDescription.primaryActions || !reducerDescription.primaryActions.length) &&
(!reducerDescription.modalActions || !reducerDescription.modalActions.length))
) {
throw new Error(
"Reducer description is incomplete, should contain at least an actions field to complete"
);
throw new Error('Reducer description is incomplete, should contain at least an actions field to complete');
}

let reducerHandler = {};

if (reducerDescription.primaryActions) {
if (!isStringArray(reducerDescription.primaryActions)) {
throw new Error("Primary actions must be a string array");
throw new Error('Primary actions must be a string array');
}
reducerDescription.primaryActions.forEach(actionName => {
reducerHandler[actionName] = onLoading();
Expand All @@ -36,7 +33,7 @@ function completeReducer(reducerDescription) {

if (reducerDescription.paginationActions) {
if (!isStringArray(reducerDescription.paginationActions)) {
throw new Error("Primary actions must be a string array");
throw new Error('Primary actions must be a string array');
}
reducerDescription.paginationActions.forEach(actionName => {
reducerHandler[actionName] = onLoading();
Expand All @@ -47,7 +44,7 @@ function completeReducer(reducerDescription) {

if (reducerDescription.modalActions) {
if (!isStringArray(reducerDescription.modalActions)) {
throw new Error("Modal actions must be a string array");
throw new Error('Modal actions must be a string array');
}
reducerDescription.modalActions.forEach(actionName => {
reducerHandler[`${actionName}_OPEN`] = onSubscribe();
Expand All @@ -57,9 +54,7 @@ function completeReducer(reducerDescription) {

if (reducerDescription.override) {
if (!isValidObject(reducerDescription.override)) {
throw new Error(
"Reducer description containing a override is not an object"
);
throw new Error('Reducer description containing a override is not an object');
}
reducerHandler = { ...reducerHandler, ...reducerDescription.override };
}
Expand Down