-
Notifications
You must be signed in to change notification settings - Fork 51
/
profile-actions.js
158 lines (142 loc) · 4.57 KB
/
profile-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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { createAction } from 'redux-actions'
import { CALL_API } from 'redux-api-middleware'
import { isEmpty, includes, forEach } from 'lodash'
import utilsDate from '../utils/DateHelper'
import {
getJsonHeaders,
buildAPIRequest,
LOAD_USER_REQUEST,
LOAD_USER_SUCCESS,
LOAD_USER_FAILURE
} from './common-actions'
import { apiUrl, isLoggedIn, links, serverUrl } from '../config'
export const FILTER_UPDATE = 'FILTER_UPDATE'
export const DATE_RANGE_UPDATE = 'DATE_RANGE_UPDATE'
export const SELECT_DAY_UPDATE = 'SELECT_DAY_UPDATE'
export const USER_STATS_REQUEST = 'USER_STATS_REQUEST'
export const USER_STATS_SUCCESS = 'USER_STATS_SUCCESS'
export const USER_STATS_FAILURE = 'USER_STATS_FAILURE'
export const GET_LOCALE_REQUEST = 'GET_LOCALE_REQUEST'
export const GET_LOCALE_SUCCESS = 'GET_LOCALE_SUCCESS'
export const GET_LOCALE_FAILURE = 'GET_LOCALE_FAILURE'
export const updateDateRange = createAction(DATE_RANGE_UPDATE)
export const updateFilter = createAction(FILTER_UPDATE)
export const updateSelectDay = createAction(SELECT_DAY_UPDATE)
const getStatsEndPoint = (username, fromDate, toDate) => {
return apiUrl + '/stats/user/' + username + '/' + fromDate + '..' + toDate
}
const getUserStatistics = (username, fromDate, toDate) => {
const endpoint = getStatsEndPoint(username, fromDate, toDate)
const apiTypes = [
USER_STATS_REQUEST,
{
type: USER_STATS_SUCCESS,
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type')
if (contentType && includes(contentType, 'json')) {
return res.json().then((json) => {
return json
})
}
},
meta: {
receivedAt: Date.now()
}
},
USER_STATS_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}
const loadUserStats = (username, dateRangeOption) => {
return (dispatch, getState) => {
const dateRange = utilsDate.getDateRangeFromOption(dateRangeOption)
dispatch(getUserStatistics(username, dateRange.fromDate, dateRange.toDate))
}
}
const getLocaleDetail = (localeId) => {
const endpoint = apiUrl + '/locales/locale/' + localeId
const apiTypes = [
GET_LOCALE_REQUEST,
{
type: GET_LOCALE_SUCCESS,
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type')
if (contentType && includes(contentType, 'json')) {
return res.json().then((json) => {
return json
})
}
},
meta: {
receivedAt: Date.now()
}
},
GET_LOCALE_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}
const getUserInfo = (dispatch, username, dateRangeOption) => {
const endpoint = apiUrl + '/user' + (isEmpty(username) ? '' : '/' + username)
const apiTypes = [
LOAD_USER_REQUEST,
{
type: LOAD_USER_SUCCESS,
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type')
if (contentType && includes(contentType, 'json')) {
return res.json().then((json) => {
forEach(json.languageTeams, function (localeId) {
dispatch(getLocaleDetail(localeId))
})
dispatch(loadUserStats(username, dateRangeOption))
return json
})
}
},
meta: {
receivedAt: Date.now()
}
},
LOAD_USER_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}
export const profileInitialLoad = (username) => {
return (dispatch, getState) => {
if (isEmpty(username) && !isLoggedIn) {
// redirect to login screen if no username is found url
// and user is not logged in
window.location = serverUrl + links.loginUrl
} else {
dispatch(getUserInfo(dispatch, username,
getState().profile.dateRange))
}
}
}
export const dateRangeChanged = (dataRangeOption) => {
return (dispatch, getState) => {
const username = getState().profile.user.username
dispatch(updateDateRange(dataRangeOption))
dispatch(loadUserStats(username, dataRangeOption))
}
}
export const filterUpdate = (contentState) => {
return (dispatch, getState) => {
if (getState().profile.contentStateOption !== contentState) {
dispatch(updateFilter(contentState))
}
}
}
export const selectDayChanged = (day) => {
return (dispatch, getState) => {
// click the same day again will cancel selection
const selectedDay = getState().profile.selectedDay !== day ? day : null
dispatch(updateSelectDay(selectedDay))
}
}