Skip to content

Commit 6b95227

Browse files
committed
Use $http in async example
1 parent 9f2c547 commit 6b95227

File tree

7 files changed

+75
-69
lines changed

7 files changed

+75
-69
lines changed

examples/async/actions/async.js

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as types from '../constants/ActionTypes';
2+
3+
function selectReddit(reddit) {
4+
return {
5+
type: types.SELECT_REDDIT,
6+
reddit
7+
};
8+
}
9+
10+
function invalidateReddit(reddit) {
11+
return {
12+
type: types.INVALIDATE_REDDIT,
13+
reddit
14+
};
15+
}
16+
17+
function requestPosts(reddit) {
18+
return {
19+
type: types.REQUEST_POSTS,
20+
reddit
21+
};
22+
}
23+
24+
function receivePosts(reddit, json) {
25+
return {
26+
type: types.RECEIVE_POSTS,
27+
reddit: reddit,
28+
posts: json.data.children.map(child => child.data),
29+
receivedAt: Date.now()
30+
};
31+
}
32+
33+
export default function asyncService($http) {
34+
function fetchPosts(reddit) {
35+
return dispatch => {
36+
dispatch(requestPosts(reddit));
37+
return $http.get(`http://www.reddit.com/r/${reddit}.json`)
38+
.then(response => response.data)
39+
.then(json => dispatch(receivePosts(reddit, json)));
40+
};
41+
}
42+
43+
function shouldFetchPosts(state, reddit) {
44+
const posts = state.postsByReddit[reddit];
45+
if (!posts) {
46+
return true;
47+
}
48+
if (posts.isFetching) {
49+
return false;
50+
}
51+
return posts.didInvalidate;
52+
}
53+
54+
function fetchPostsIfNeeded(reddit) {
55+
return (dispatch, getState) => {
56+
if (shouldFetchPosts(getState(), reddit)) {
57+
return dispatch(fetchPosts(reddit));
58+
}
59+
};
60+
}
61+
62+
return {
63+
selectReddit,
64+
invalidateReddit,
65+
fetchPostsIfNeeded
66+
};
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const REQUEST_POSTS = 'REQUEST_POSTS';
2+
export const RECEIVE_POSTS = 'RECEIVE_POSTS';
3+
export const SELECT_REDDIT = 'SELECT_REDDIT';
4+
export const INVALIDATE_REDDIT = 'INVALIDATE_REDDIT';

examples/async/containers/app.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as AsyncActions from '../actions/async';
2-
31
export default function app() {
42
return {
53
restrict: 'E',
@@ -12,7 +10,7 @@ export default function app() {
1210

1311
class AppController {
1412

15-
constructor($ngRedux, $scope) {
13+
constructor($ngRedux, $scope, AsyncActions) {
1614
const unsubscribe = $ngRedux.connect(this.mapStateToThis, AsyncActions)((selectedState, actions) => {
1715
this.componentWillReceiveStateAndActions(selectedState, actions);
1816
Object.assign(this, selectedState, actions);

examples/async/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ngRedux from 'ng-redux';
44
import thunk from 'redux-thunk';
55
import createLogger from 'redux-logger';
66
import rootReducer from './reducers';
7+
import asyncService from './actions/asyncService';
78
import app from './containers/app';
89
import picker from './components/picker';
910
import posts from './components/posts';
@@ -12,6 +13,7 @@ angular.module('async', [ngRedux])
1213
.config(($ngReduxProvider) => {
1314
$ngReduxProvider.createStoreWith(rootReducer, [thunk, createLogger()]);
1415
})
16+
.service('AsyncActions', asyncService)
1517
.directive('ngrAsync', app)
1618
.directive('ngrPicker', picker)
1719
.directive('ngrPosts', posts);

examples/async/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"homepage": "https://github.com/wbuchwalter/ngRedux#readme",
1717
"dependencies": {
1818
"angular": "^1.4.4",
19-
"isomorphic-fetch": "^2.1.1",
2019
"ng-redux": "^3.0.0",
2120
"redux": "^3.0.0",
2221
"redux-logger": "^2.0.2",

examples/async/reducers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { combineReducers } from 'redux';
22
import {
33
SELECT_REDDIT, INVALIDATE_REDDIT,
44
REQUEST_POSTS, RECEIVE_POSTS
5-
} from '../actions/async';
5+
} from '../constants/ActionTypes';
66

77
function selectedReddit(state = 'angularjs', action) {
88
switch (action.type) {

0 commit comments

Comments
 (0)