Skip to content

Data Structure to factory pattern and entity pattern #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add method to filter payload data and abstract the url to request
  • Loading branch information
FelipeBarrosCruz committed May 5, 2017
commit 2ff301bdf50f4453b4d9cc687b36d8b36bc310f0
33 changes: 26 additions & 7 deletions 5-redux-react/src/js/actions/tweetsActions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import axios from "axios";

export function fetchTweets() {
const TWEETS_USER = 'learncode';

function filterPayloadDataToReturn(data) {
if (!Array.isArray(data)) return [];
return data.filter(value => !!value).reduce((merge, value) => {
if (value.myArray && Array.isArray(value.myArray)) {
merge = merge.concat(value.myArray);
}
return merge;
}, []);
}

function buildTweetsUrl(user) {
return `http://rest.learncode.academy/api/${user}/tweets`;
}

function requestTweets(url, done) {
axios.get(url).then(response => done(null, response)).catch(err => done(err, null));
}

return function(dispatch) {
/*
http://rest.learncode.academy is a public test server, so another user's experimentation can break your tests
If you get console errors due to bad data:
- change "reacttest" below to any other username
- post some tweets to http://rest.learncode.academy/api/yourusername/tweets
*/
axios.get("http://rest.learncode.academy/api/reacttest/tweets")
.then((response) => {
dispatch({type: "FETCH_TWEETS_FULFILLED", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_TWEETS_REJECTED", payload: err})
})
requestTweets(buildTweetsUrl(TWEETS_USER), (err, response) => {
if (err || !response && !response.data || Array.isArray(response.data) && !response.data.length) {
return dispatch({type: "FETCH_TWEETS_REJECTED", payload: err || new Error('Tweets are empty')});
}
return dispatch({type: "FETCH_TWEETS_FULFILLED", payload: filterPayloadDataToReturn(response.data)});
})
}
}

Expand Down