Skip to content

Commit 5b6986d

Browse files
committed
save user asynchronously
1 parent 82f8a6b commit 5b6986d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/UserCreator/actions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ export function updateNameField(name) {
77
}
88

99
export const CREATE_USER = "USER_CREATOR/CREATE_USER";
10+
1011
export function createUser() {
1112
return { type: CREATE_USER };
1213
}
14+
15+
export const USER_SAVED = "USER_CREATOR/USER_SAVED";
16+
export function userSaved(user) {
17+
return { type: USER_SAVED, payload: user };
18+
}
19+
20+
export function saveUser(user) {
21+
return Promise.resolve(user);
22+
}

src/UserCreator/reducer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { loop, Cmd } from "redux-loop";
22

3-
import { CREATE_USER, UPDATE_NAME_FIELD } from "./actions";
3+
import { CREATE_USER, UPDATE_NAME_FIELD, USER_SAVED, saveUser, userSaved } from "./actions";
44
import { globalAction } from "redux-subspace";
5+
56
import { userCreated } from "../state/users";
67

78
const initialState = {
@@ -12,8 +13,19 @@ export default function(state = initialState, action) {
1213
switch (action.type) {
1314
case UPDATE_NAME_FIELD:
1415
return { ...state, name: action.payload };
16+
17+
case USER_SAVED:
18+
return loop(state, Cmd.action(globalAction(userCreated(action.payload))));
19+
1520
case CREATE_USER:
16-
return loop(state, Cmd.action(globalAction(userCreated(state))));
21+
return loop(
22+
state,
23+
Cmd.run(saveUser, {
24+
successActionCreator: userSaved,
25+
args: [state]
26+
})
27+
);
28+
1729
default:
1830
return state;
1931
}

0 commit comments

Comments
 (0)