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 30, 2017
1 parent 82f8a6b commit 5b6986d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 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

0 comments on commit 5b6986d

Please sign in to comment.