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

Commit 8779849

Browse files
committed
first function.
1 parent abdf500 commit 8779849

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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 ##
1516

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

@@ -21,10 +22,14 @@ In _step 2_ the user taps the submit button, which dispatches the `login(usernam
2122
export function login(username, password) {
2223
return (dispatch) => {
2324
24-
/* We use this to update the state of `isLoggingIn` to `true` in our store, which can be used to display an activity indicator on the login view. */
25+
// We use this to update the state of `isLoggingIn` to `true` in our
26+
// store, which can be used to display an activity indicator on the login
27+
// view.
2528
dispatch(loginRequest())
2629
27-
/* This only works in Node, use an implementation that work for the platform you're using, e.g.: `base64-js` for React Native, or `btoa()`` for browsers. */
30+
// This only works in Node, use an implementation that work for the
31+
// platform you're using, e.g.: `base64-js` for React Native, or `btoa()`
32+
// for browsers, etc...
2833
const hash = new Buffer(`${username}:${password}`).toString('base64')
2934
return fetch('https://httpbin.org/basic-auth/admin/secret', {
3035
headers: {
@@ -48,3 +53,9 @@ export function login(username, password) {
4853
}
4954
}
5055
```
56+
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.
60+
61+
## Reducers ##

actions/user.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function loginFailure(error) {
2626
export function login(username, password) {
2727
return (dispatch) => {
2828

29+
// We use this to update `isLoggingIn` to `true` in our store, which can
30+
// be used to display an activity indicator on the login view.
2931
dispatch(loginRequest())
3032

3133
// Note: This only works in node.js, use an implementation that works
@@ -45,10 +47,12 @@ export function login(username, password) {
4547
return json
4648
})
4749
.then(
50+
// success
4851
data => {
4952
// data = { authenticated: true, user: 'admin' }
5053
dispatch(loginSuccess(hash, data.user))
5154
},
55+
// failure
5256
(response, data) => dispatch(loginFailure(data.error || 'Log in failed'))
5357
)
5458
}

0 commit comments

Comments
 (0)