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

Commit 375a2a8

Browse files
committed
adding reducer documentation.
1 parent ff99566 commit 375a2a8

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ When an app communicates with a HTTP API, which enforces some form of authentica
1212

1313
With the work flow defined above we'll start with the Reflux _action creators_ and _reducers_.
1414

15-
## Action Creators ##
15+
# Logging In #
16+
17+
### Action Creators ###
1618

1719
In _step 2_ the user taps the submit button, which dispatches the `login(username, password)` function.
1820

@@ -58,22 +60,26 @@ export function login(username, password) {
5860

5961
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`)
6062

61-
## Reducers ##
63+
### Reducers ###
64+
65+
Some of the properties in the reducer can be used to update the
66+
UI.
6267

63-
In our reducer we can use `isLoggingIn`, `isAuthenticated`, and `error`
64-
to update the user-interface.
68+
- `isLoggingIn`: Display a loading indicator.
69+
- `isAuthenticated`: Hide/ Show a login modal
70+
- `error`: Self explanatory.
6571

6672
```
6773
/// reducers/user.js
6874
6975
function user(state = {
70-
isLoggingIn: false, // loading indicator
71-
isAuthenticated: false // show, or hide the authentication modal.
76+
isLoggingIn: false,
77+
isAuthenticated: false
7278
}, action) {
7379
switch(action.type) {
7480
case LOGIN_REQUEST:
7581
return {
76-
isLoggingIn: true,
82+
isLoggingIn: true, // Show a loading indicator.
7783
isAuthenticated: false
7884
}
7985
case LOGIN_FAILURE:
@@ -85,12 +91,14 @@ function user(state = {
8591
case LOGIN_SUCCESS:
8692
return {
8793
isLoggingIn: false,
88-
isAuthenticated: true,
89-
hash: action.hash,
94+
isAuthenticated: true, // Dismiss the login view.
95+
hash: action.hash, // Used in subsequent API requests.
9096
user: action.user
9197
}
9298
default:
9399
return state
94100
}
95101
}
96102
```
103+
104+
# subsequent API requests #

reducers/user.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
2-
// The request, failure, success pattern is very typical when performing
3-
// HTTP requests. The amount of boilerplate can be reduced using Redux
4-
// middleware, _or so I've read._
5-
61
import {
72
LOGIN_REQUEST,
83
LOGIN_FAILURE,
@@ -16,7 +11,7 @@ function user(state = {
1611
switch(action.type) {
1712
case LOGIN_REQUEST:
1813
return {
19-
isLoggingIn: true,
14+
isLoggingIn: true, // Show a loading indicator.
2015
isAuthenticated: false
2116
}
2217
case LOGIN_FAILURE:
@@ -28,8 +23,8 @@ function user(state = {
2823
case LOGIN_SUCCESS:
2924
return {
3025
isLoggingIn: false,
31-
isAuthenticated: true,
32-
hash: action.hash,
26+
isAuthenticated: true, // Hide the login view.
27+
hash: action.hash, // Used in subsequent API requests.
3328
user: action.user
3429
}
3530
default:

0 commit comments

Comments
 (0)