-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.js
63 lines (58 loc) · 2.02 KB
/
data.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
import { Schemas } from './api'
import { USER_SUCCESS, USER_FAILURE, REPO_SUCCESS, REPO_FAILURE, STARRED_SUCCESS, STARRED_FAILURE, STARGAZERS_SUCCESS, STARGAZERS_FAILURE } from '../actions'
// Fetches a single user from Github API.
export function fetchUser(login) {
return {
type: 'GITHUB_API',
endpoint: `users/${login}`,
schema: Schemas.USER,
events: {
resolved: (response) => ({ type: USER_SUCCESS, response }),
rejected: (response) => ({ type: USER_FAILURE, response })
}
}
}
// Fetches a single repository from Github API.
export function fetchRepo(fullName) {
return {
type: 'GITHUB_API',
endpoint: `repos/${fullName}`,
schema: Schemas.REPO,
events: {
resolved: (response) => ({ type: REPO_SUCCESS, response }),
rejected: (response) => ({ type: REPO_FAILURE, response })
}
}
}
// Fetches a page of starred repos by a particular user.
export function fetchStarred(login, nextPageUrl = `users/${login}/starred`) {
return {
type: 'GITHUB_API',
endpoint: nextPageUrl,
schema: Schemas.REPO_ARRAY,
events: {
resolved: (response) => ({ type: STARRED_SUCCESS, response, login }),
rejected: (response) => ({ type: STARRED_FAILURE, response, login })
}
}
}
// Fetches a page of stargazers for a particular repo.
export function fetchStargazers(fullName, nextPageUrl = `repos/${fullName}/stargazers`) {
return {
type: 'GITHUB_API',
endpoint: nextPageUrl,
schema: Schemas.USER_ARRAY,
events: {
resolved: (response) => ({ type: STARGAZERS_SUCCESS, response, fullName }),
rejected: (response) => ({ type: STARGAZERS_FAILURE, response, fullName })
}
}
}
export function dataReactions(id, entities, paginations, requiredFields, fetchEntity, fetchRelated) {
var entity = entities[id]
var pagination = paginations[id]
return [
(!entity || !requiredFields.every(key => entity.hasOwnProperty(key))) && fetchEntity(id),
(!pagination || pagination.isFetching) && fetchRelated(id, pagination && pagination.nextPageUrl)
]
}