Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit ff99566

Browse files
committed
added documentation on reducers.
1 parent 8779849 commit ff99566

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

+38-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export function login(username, password) {
4545
})
4646
.then(
4747
data => {
48+
// We pass the `authentication hash` down to the reducer so that it
49+
// can be used in subsequent API requests.
4850
// data = { authenticated: true, user: 'admin' }
4951
dispatch(loginSuccess(hash, data.user))
5052
},
@@ -54,8 +56,41 @@ export function login(username, password) {
5456
}
5557
```
5658

57-
The above function dispatches 3 other actions: `LOGIN_REQUEST`, `LOGIN_FAILURE`, `LOGIN_SUCCESS`. (They're fairly generic, and not really worth documenting - Check `actions/user.js` for their implementation.)
58-
59-
Which brings us to the reducer.
59+
The above function dispatches 3 other actions: `LOGIN_REQUEST`, `LOGIN_FAILURE`, `LOGIN_SUCCESS`. (They're fairly generic, and not really worth documenting - Check `actions/user.js`)
6060

6161
## Reducers ##
62+
63+
In our reducer we can use `isLoggingIn`, `isAuthenticated`, and `error`
64+
to update the user-interface.
65+
66+
```
67+
/// reducers/user.js
68+
69+
function user(state = {
70+
isLoggingIn: false, // loading indicator
71+
isAuthenticated: false // show, or hide the authentication modal.
72+
}, action) {
73+
switch(action.type) {
74+
case LOGIN_REQUEST:
75+
return {
76+
isLoggingIn: true,
77+
isAuthenticated: false
78+
}
79+
case LOGIN_FAILURE:
80+
return {
81+
isLoggingIn: false,
82+
isAuthenticated: false,
83+
error: action.error
84+
}
85+
case LOGIN_SUCCESS:
86+
return {
87+
isLoggingIn: false,
88+
isAuthenticated: true,
89+
hash: action.hash,
90+
user: action.user
91+
}
92+
default:
93+
return state
94+
}
95+
}
96+
```

actions/user.js

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function login(username, password) {
4949
.then(
5050
// success
5151
data => {
52+
// We pass the `authentication hash` down to the reducer so that it
53+
// can be used in subsequent API requests.
5254
// data = { authenticated: true, user: 'admin' }
5355
dispatch(loginSuccess(hash, data.user))
5456
},

0 commit comments

Comments
 (0)