Skip to content

Commit

Permalink
save user asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
rikukissa committed Aug 26, 2017
1 parent a7d9f93 commit d26717b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/UserCreator/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ export function updateNameField(name) {
}

export const CREATE_USER = "USER_CREATOR/CREATE_USER";

export function createUser() {
return { type: CREATE_USER };
}

export const USER_SAVED = "USER_CREATOR/USER_SAVED";
export function userSaved(user) {
return { type: USER_SAVED, payload: user };
}

export function saveUser(user) {
return Promise.resolve(user);
}
16 changes: 14 additions & 2 deletions src/UserCreator/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { loop, Cmd } from "redux-loop";

import { CREATE_USER, UPDATE_NAME_FIELD } from "./actions";
import { CREATE_USER, UPDATE_NAME_FIELD, USER_SAVED, saveUser, userSaved } from "./actions";
import { globalAction } from "redux-subspace";

import { userCreated } from "../state/users";

const initialState = {
Expand All @@ -12,8 +13,19 @@ export default function(state = initialState, action) {
switch (action.type) {
case UPDATE_NAME_FIELD:
return { ...state, name: action.payload };

case USER_SAVED:
return loop(state, Cmd.action(globalAction(userCreated(action.payload))));

case CREATE_USER:
return loop(state, Cmd.action(globalAction(userCreated(state))));
return loop(
state,
Cmd.run(saveUser, {
successActionCreator: userSaved,
args: [state]
})
);

default:
return state;
}
Expand Down
35 changes: 31 additions & 4 deletions src/state/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import { namespaced } from "redux-subspace";
import userCreatorReducer from "../UserCreator/reducer";
import { combineReducers } from "redux-loop";
import { namespacedAction } from "redux-subspace";
import { loop, getCmd, getModel, isLoop, combineReducers } from "redux-loop";

function namespacedLoopReducer(namespace) {
const namespacer = namespaced(namespace);
const actionNamespacer = namespacedAction(namespace);
return reducer => {
return namespacer((state, action) => {
const result = reducer(state, action);

if (isLoop(result)) {
const cmd = getCmd(result);
const state = getModel(result);

if (cmd.type === "RUN") {
return loop(state, {
...cmd,
successActionCreator: (...args) => actionNamespacer(cmd.successActionCreator(...args)),
failActionCreator: (...args) => actionNamespacer(cmd.failActionCreator(...args))
});
}

return result;
}
return result;
});
};
}

export default combineReducers({
userCreator1: namespaced("userCreator1")(userCreatorReducer),
userCreator2: namespaced("userCreator2")(userCreatorReducer),
userCreator3: namespaced("userCreator3")(userCreatorReducer)
userCreator1: namespacedLoopReducer("userCreator1")(userCreatorReducer),
userCreator2: namespacedLoopReducer("userCreator2")(userCreatorReducer),
userCreator3: namespacedLoopReducer("userCreator3")(userCreatorReducer)
});

0 comments on commit d26717b

Please sign in to comment.