Skip to content

Commit

Permalink
Format all the things!
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Apr 2, 2021
1 parent d0106e1 commit 6e53b1e
Show file tree
Hide file tree
Showing 66 changed files with 732 additions and 849 deletions.
18 changes: 9 additions & 9 deletions docs/Troubleshooting.md
Expand Up @@ -31,7 +31,7 @@ function todos(state = [], action) {
// Wrong! This mutates state
state.push({
text: action.text,
completed: false
completed: false,
})
return state
case 'COMPLETE_TODO':
Expand All @@ -55,16 +55,16 @@ function todos(state = [], action) {
...state,
{
text: action.text,
completed: false
}
completed: false,
},
]
case 'COMPLETE_TODO':
// Return a new array
return state.map((todo, index) => {
if (index === action.index) {
// Copy the object before mutating
return Object.assign({}, todo, {
completed: true
completed: true,
})
}
return todo
Expand All @@ -82,7 +82,7 @@ It's more code, but it's exactly what makes Redux predictable and efficient. If
return state.map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: true
completed: true,
})
}
return todo
Expand All @@ -92,9 +92,9 @@ return state.map((todo, index) => {
return update(state, {
[action.index]: {
completed: {
$set: true
}
}
$set: true,
},
},
})
```

Expand All @@ -109,7 +109,7 @@ You can also enable the [object spread operator proposal](recipes/UsingObjectSpr
return state.map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: true
completed: true,
})
}
return todo
Expand Down
56 changes: 28 additions & 28 deletions docs/advanced/AsyncActions.md
Expand Up @@ -62,7 +62,7 @@ export const SELECT_SUBREDDIT = 'SELECT_SUBREDDIT'
export function selectSubreddit(subreddit) {
return {
type: SELECT_SUBREDDIT,
subreddit
subreddit,
}
}
```
Expand All @@ -75,7 +75,7 @@ export const INVALIDATE_SUBREDDIT = 'INVALIDATE_SUBREDDIT'
export function invalidateSubreddit(subreddit) {
return {
type: INVALIDATE_SUBREDDIT,
subreddit
subreddit,
}
}
```
Expand All @@ -90,7 +90,7 @@ export const REQUEST_POSTS = 'REQUEST_POSTS'
function requestPosts(subreddit) {
return {
type: REQUEST_POSTS,
subreddit
subreddit,
}
}
```
Expand All @@ -106,8 +106,8 @@ function receivePosts(subreddit, json) {
return {
type: RECEIVE_POSTS,
subreddit,
posts: json.data.children.map(child => child.data),
receivedAt: Date.now()
posts: json.data.children.map((child) => child.data),
receivedAt: Date.now(),
}
}
```
Expand Down Expand Up @@ -225,7 +225,7 @@ import {
SELECT_SUBREDDIT,
INVALIDATE_SUBREDDIT,
REQUEST_POSTS,
RECEIVE_POSTS
RECEIVE_POSTS,
} from '../actions'

function selectedSubreddit(state = 'reactjs', action) {
Expand All @@ -241,26 +241,26 @@ function posts(
state = {
isFetching: false,
didInvalidate: false,
items: []
items: [],
},
action
) {
switch (action.type) {
case INVALIDATE_SUBREDDIT:
return Object.assign({}, state, {
didInvalidate: true
didInvalidate: true,
})
case REQUEST_POSTS:
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false
didInvalidate: false,
})
case RECEIVE_POSTS:
return Object.assign({}, state, {
isFetching: false,
didInvalidate: false,
items: action.posts,
lastUpdated: action.receivedAt
lastUpdated: action.receivedAt,
})
default:
return state
Expand All @@ -273,7 +273,7 @@ function postsBySubreddit(state = {}, action) {
case RECEIVE_POSTS:
case REQUEST_POSTS:
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
[action.subreddit]: posts(state[action.subreddit], action),
})
default:
return state
Expand All @@ -282,7 +282,7 @@ function postsBySubreddit(state = {}, action) {

const rootReducer = combineReducers({
postsBySubreddit,
selectedSubreddit
selectedSubreddit,
})

export default rootReducer
Expand All @@ -294,7 +294,7 @@ In this code, there are two interesting parts:

```js
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
[action.subreddit]: posts(state[action.subreddit], action),
})
```

Expand Down Expand Up @@ -327,7 +327,7 @@ export const REQUEST_POSTS = 'REQUEST_POSTS'
function requestPosts(subreddit) {
return {
type: REQUEST_POSTS,
subreddit
subreddit,
}
}

Expand All @@ -336,16 +336,16 @@ function receivePosts(subreddit, json) {
return {
type: RECEIVE_POSTS,
subreddit,
posts: json.data.children.map(child => child.data),
receivedAt: Date.now()
posts: json.data.children.map((child) => child.data),
receivedAt: Date.now(),
}
}

export const INVALIDATE_SUBREDDIT = 'INVALIDATE_SUBREDDIT'
export function invalidateSubreddit(subreddit) {
return {
type: INVALIDATE_SUBREDDIT,
subreddit
subreddit,
}
}

Expand All @@ -358,7 +358,7 @@ export function fetchPosts(subreddit) {
// It passes the dispatch method as an argument to the function,
// thus making it able to dispatch actions itself.

return function(dispatch) {
return function (dispatch) {
// First dispatch: the app state is updated to inform
// that the API call is starting.

Expand All @@ -372,14 +372,14 @@ export function fetchPosts(subreddit) {

return fetch(`https://www.reddit.com/r/${subreddit}.json`)
.then(
response => response.json(),
(response) => response.json(),
// Do not use catch, because that will also catch
// any errors in the dispatch and resulting render,
// causing a loop of 'Unexpected batch number' errors.
// https://github.com/facebook/react/issues/6895
error => console.log('An error occurred.', error)
(error) => console.log('An error occurred.', error)
)
.then(json =>
.then((json) =>
// We can dispatch many times!
// Here, we update the app state with the results of the API call.

Expand Down Expand Up @@ -443,7 +443,7 @@ export const REQUEST_POSTS = 'REQUEST_POSTS'
function requestPosts(subreddit) {
return {
type: REQUEST_POSTS,
subreddit
subreddit,
}
}

Expand All @@ -452,25 +452,25 @@ function receivePosts(subreddit, json) {
return {
type: RECEIVE_POSTS,
subreddit,
posts: json.data.children.map(child => child.data),
receivedAt: Date.now()
posts: json.data.children.map((child) => child.data),
receivedAt: Date.now(),
}
}

export const INVALIDATE_SUBREDDIT = 'INVALIDATE_SUBREDDIT'
export function invalidateSubreddit(subreddit) {
return {
type: INVALIDATE_SUBREDDIT,
subreddit
subreddit,
}
}

function fetchPosts(subreddit) {
return dispatch => {
return (dispatch) => {
dispatch(requestPosts(subreddit))
return fetch(`https://www.reddit.com/r/${subreddit}.json`)
.then(response => response.json())
.then(json => dispatch(receivePosts(subreddit, json)))
.then((response) => response.json())
.then((json) => dispatch(receivePosts(subreddit, json)))
}
}

Expand Down

0 comments on commit 6e53b1e

Please sign in to comment.