-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtweetsActions.js
More file actions
62 lines (54 loc) · 1.67 KB
/
tweetsActions.js
File metadata and controls
62 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
*/
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)})
})
}
}
export function addTweet(id, text) {
return {
type: 'ADD_TWEET',
payload: {
id,
text,
},
}
}
export function updateTweet(id, text) {
return {
type: 'UPDATE_TWEET',
payload: {
id,
text,
},
}
}
export function deleteTweet(id) {
return { type: 'DELETE_TWEET', payload: id}
}