-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.js
96 lines (83 loc) · 1.94 KB
/
actions.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import fetchPosts from './lib/posts'
import toQuery from './lib/toQuery'
export const RECEIVE_POSTS = 'RECEIVE_POSTS'
export const RECEIVE_NEXT_PAGE = 'RECEIVE_NEXT_PAGE'
export const RECEIVE_NEW_POSTS = 'RECEIVE_NEW_POSTS'
export const MERGE_NEW_POSTS = 'MERGE_NEW_POSTS'
export const SET_ACTIVE_TAB = 'SET_ACTIVE_TAB'
export const RECEIVE_LOADING = 'RECEIVE_LOADING'
export const RECEIVE_LOCATION = 'RECEIVE_LOCATION'
export const RECEIVE_PARAMS = 'RECEIVE_PARAMS'
const handleError = (err) => {
console.log(err)
}
export function setActiveTab(activeTab) {
return {
type: SET_ACTIVE_TAB,
activeTab: activeTab || 'All'
}
}
export function isLoading(bool) {
return {
type: RECEIVE_LOADING,
isLoading: bool,
}
}
export function getNextPage(after, flair, page) {
return (dispatch, getState) => {
dispatch(isLoading(true))
const { location } = getState()
return fetchPosts(location.subreddit, {
after,
q: toQuery(flair)
})
.then((posts) => {
dispatch(receivePosts(posts, page))
dispatch(isLoading(false))
})
.catch(handleError)
}
}
export function search(flair, query) {
return (dispatch, getState) => {
dispatch(isLoading(true))
const { location } = getState()
return fetchPosts(location.subreddit, { q: toQuery(flair, query) })
.then((posts) => {
dispatch(receivePosts(posts))
dispatch(isLoading(false))
})
.catch(handleError)
}
}
export function receivePosts(posts, page = 1) {
return {
type: RECEIVE_POSTS,
posts,
page
}
}
export function receiveNewPosts(posts, page = 0) {
return {
type: RECEIVE_NEW_POSTS,
posts,
page
}
}
export function mergeNewPosts() {
return {
type: MERGE_NEW_POSTS
}
}
export function receiveLocation(location) {
return {
type: RECEIVE_LOCATION,
location
}
}
export function receiveParams(params) {
return {
type: RECEIVE_PARAMS,
params
}
}