Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 5b1e799

Browse files
author
Alex Eng
authored
refactor(profile): migrate profile page to redux (#1218)
https://zanata.atlassian.net/browse/ZNTA-1163
1 parent 66c421b commit 5b1e799

22 files changed

+693
-11504
lines changed

frontend/src/main/web/package.json

+1-9
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,16 @@
6363
"classnames": "^2.1.3",
6464
"defined": "^1.0.0",
6565
"dom-helpers": "^2.4.0",
66-
"es6-promise": "^2.0.1",
67-
"events": "^1.0.2",
6866
"fixed-data-table": "^0.5.0",
6967
"flat": "^1.6.0",
70-
"flux": "^2.0.3",
7168
"history": "^2.0.0",
7269
"immutable": "^3.7.6",
7370
"isomorphic-fetch": "^2.2.1",
7471
"keymirror": "^0.1.1",
75-
"lodash": "^4.0.1",
72+
"lodash": "4.13.1",
7673
"moment": "^2.12.0",
7774
"moment-range": "^2.2.0",
7875
"normalizr": "^2.0.0",
79-
"object-assign": "^2.0.0",
8076
"react": "^0.14.7",
8177
"react-a11y": "^0.2.8",
8278
"react-addons-pure-render-mixin": "^0.14.6",
@@ -96,7 +92,6 @@
9692
"redux-api-middleware": "^1.0.0-beta3",
9793
"redux-logger": "^2.5.0",
9894
"redux-thunk": "^1.0.3",
99-
"superagent": "^0.21.0",
10095
"warning": "^2.1.0",
10196
"webfontloader": "^1.6.21"
10297
},
@@ -107,10 +102,7 @@
107102
"./node_modules/react-dom",
108103
"./node_modules/react-addons-test-utils",
109104
"./node_modules/fbjs",
110-
"./node_modules/object-assign",
111-
"./node_modules/es6-promise",
112105
"./node_modules/lodash",
113-
"./node_modules/events",
114106
"./src"
115107
],
116108
"moduleFileExtensions": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { createAction } from 'redux-actions'
2+
import { CALL_API } from 'redux-api-middleware'
3+
import { isEmpty, includes } from 'lodash'
4+
import utilsDate from '../utils/DateHelper'
5+
6+
import {
7+
getJsonHeaders,
8+
buildAPIRequest
9+
} from './common'
10+
11+
export const FILTER_UPDATE = 'FILTER_UPDATE'
12+
export const DATE_RANGE_UPDATE = 'DATE_RANGE_UPDATE'
13+
export const SELECT_DAY_UPDATE = 'SELECT_DAY_UPDATE'
14+
15+
export const LOAD_USER_REQUEST = 'LOAD_USER_REQUEST'
16+
export const LOAD_USER_SUCCESS = 'LOAD_USER_SUCCESS'
17+
export const LOAD_USER_FAILURE = 'LOAD_USER_FAILURE'
18+
19+
export const USER_STATS_REQUEST = 'USER_STATS_REQUEST'
20+
export const USER_STATS_SUCCESS = 'USER_STATS_SUCCESS'
21+
export const USER_STATS_FAILURE = 'USER_STATS_FAILURE'
22+
23+
export const updateDateRange = createAction(DATE_RANGE_UPDATE)
24+
export const updateFilter = createAction(FILTER_UPDATE)
25+
export const updateSelectDay = createAction(SELECT_DAY_UPDATE)
26+
27+
const getStatsEndPoint = (username, fromDate, toDate) => {
28+
return window.config.baseUrl + window.config.apiRoot +
29+
'/stats/user/' + username + '/' + fromDate + '..' + toDate
30+
}
31+
32+
const getUserStatistics = (username, fromDate, toDate) => {
33+
const endpoint = getStatsEndPoint(username, fromDate, toDate)
34+
const apiTypes = [
35+
USER_STATS_REQUEST,
36+
{
37+
type: USER_STATS_SUCCESS,
38+
payload: (action, state, res) => {
39+
const contentType = res.headers.get('Content-Type')
40+
if (contentType && includes(contentType, 'json')) {
41+
return res.json().then((json) => {
42+
return json
43+
})
44+
}
45+
},
46+
meta: {
47+
receivedAt: Date.now()
48+
}
49+
},
50+
USER_STATS_FAILURE
51+
]
52+
return {
53+
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
54+
}
55+
}
56+
57+
const loadUserStats = (username, dateRangeOption) => {
58+
return (dispatch, getState) => {
59+
const dateRange = utilsDate.getDateRangeFromOption(dateRangeOption)
60+
dispatch(getUserStatistics(username, dateRange.fromDate, dateRange.toDate))
61+
}
62+
}
63+
64+
const getUserInfo = (dispatch, username, dateRangeOption) => {
65+
const endpoint = window.config.baseUrl + window.config.apiRoot + '/user' +
66+
(isEmpty(username) ? '' : '/' + username)
67+
68+
const apiTypes = [
69+
LOAD_USER_REQUEST,
70+
{
71+
type: LOAD_USER_SUCCESS,
72+
payload: (action, state, res) => {
73+
const contentType = res.headers.get('Content-Type')
74+
if (contentType && includes(contentType, 'json')) {
75+
return res.json().then((json) => {
76+
dispatch(loadUserStats(username, dateRangeOption))
77+
return json
78+
})
79+
}
80+
},
81+
meta: {
82+
receivedAt: Date.now()
83+
}
84+
},
85+
LOAD_USER_FAILURE
86+
]
87+
return {
88+
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
89+
}
90+
}
91+
92+
export const profileInitialLoad = (username) => {
93+
return (dispatch, getState) => {
94+
dispatch(getUserInfo(dispatch, username || window.config.user.username,
95+
getState().profile.dateRangeOption))
96+
}
97+
}
98+
99+
export const dateRangeChanged = (dataRangeOption) => {
100+
return (dispatch, getState) => {
101+
const username = getState().profile.user.username
102+
dispatch(updateDateRange(dataRangeOption))
103+
dispatch(loadUserStats(username, dataRangeOption))
104+
}
105+
}
106+
107+
export const filterUpdate = (contentState) => {
108+
return (dispatch, getState) => {
109+
if (getState().profile.contentStateOption !== contentState) {
110+
dispatch(updateFilter(contentState))
111+
}
112+
}
113+
}
114+
115+
export const selectDayChanged = (day) => {
116+
return (dispatch, getState) => {
117+
// click the same day again will cancel selection
118+
const selectedDay = getState().profile.selectedDay !== day ? day : null
119+
dispatch(updateSelectDay(selectedDay))
120+
}
121+
}

frontend/src/main/web/src/actions/userMatrix.js

-42
This file was deleted.

frontend/src/main/web/src/constants/ActionTypes.js

-9
This file was deleted.

frontend/src/main/web/src/constants/Options.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var ContentStates = ['Total', 'Approved', 'Translated', 'Needs Work']
2-
var ContentStateStyles = ['plain', 'primary', 'success', 'unsure']
3-
var DateRanges = [
1+
export const ContentStates = ['Total', 'Approved', 'Translated', 'Needs Work']
2+
export const ContentStateStyles = ['plain', 'primary', 'success', 'unsure']
3+
export const DateRanges = [
44
{
55
value: 'thisWeek',
66
label: 'This Week'
@@ -18,7 +18,3 @@ var DateRanges = [
1818
label: 'Last Month'
1919
}
2020
]
21-
22-
exports.ContentStates = ContentStates
23-
exports.ContentStateStyles = ContentStateStyles
24-
exports.DateRanges = DateRanges

frontend/src/main/web/src/containers/Root.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class Root extends Component {
2525
onEnter={() => store.dispatch(glossaryInitialLoad())} />
2626
<Route path='profile/:username' component={UserProfile} />
2727
<Route path='explore' component={Explore}
28-
onEnter={() => store.dispatch(searchPageInitialLoad())}/>
28+
onEnter={() => store.dispatch(searchPageInitialLoad())} />
2929
<Redirect from='profile' to={`profile/${username}`} />
3030
<Redirect from='/' to={`profile/${username}`} />
3131
</Route>

0 commit comments

Comments
 (0)