Skip to content

Commit

Permalink
feat(ZNTA-2380): save user permissions in header data reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
efloden committed Feb 12, 2018
1 parent 7f78235 commit c081b8f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 73 deletions.
29 changes: 0 additions & 29 deletions server/zanata-frontend/src/app/actions/profile-actions.js
Expand Up @@ -16,10 +16,6 @@ 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_PERMISSION_REQUEST = 'USER_PERMISSION_REQUEST'
export const USER_PERMISSION_SUCCESS = 'USER_PERMISSION_SUCCESS'
export const USER_PERMISSION_FAILURE = 'USER_PERMISSION_FAILURE'

export const USER_STATS_REQUEST = 'USER_STATS_REQUEST'
export const USER_STATS_SUCCESS = 'USER_STATS_SUCCESS'
export const USER_STATS_FAILURE = 'USER_STATS_FAILURE'
Expand All @@ -36,31 +32,6 @@ const getStatsEndPoint = (username, fromDate, toDate) => {
return apiUrl + '/stats/user/' + username + '/' + fromDate + '..' + toDate
}

export const getUserPermissions = (localeId, projectSlug) => {
const endpoint = `/permission/roles/locale/${localeId}/project/${projectSlug}`
const apiTypes = [
USER_PERMISSION_REQUEST,
{
type: USER_PERMISSION_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_PERMISSION_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}

const getUserStatistics = (username, fromDate, toDate) => {
const endpoint = getStatsEndPoint(username, fromDate, toDate)
const apiTypes = [
Expand Down
46 changes: 8 additions & 38 deletions server/zanata-frontend/src/app/editor/actions/header-actions.js
Expand Up @@ -4,7 +4,8 @@ import {
fetchMyInfo,
fetchProjectInfo,
fetchDocuments,
fetchVersionLocales
fetchVersionLocales,
getUserPermissions
} from '../api'
import {
HIDE_SIDEBAR,
Expand All @@ -17,14 +18,9 @@ import {
DOCUMENT_SELECTED,
LOCALE_SELECTED,
STATS_FETCHED,
HEADER_DATA_FETCHED,
USER_PERMISSION_REQUEST,
USER_PERMISSION_SUCCESS,
USER_PERMISSION_FAILURE
HEADER_DATA_FETCHED
} from './header-action-types'
import { buildAPIRequest, getJsonHeaders } from '../../actions/common-actions'
import { CALL_API } from 'redux-api-middleware'
import { some, curry, isEmpty, includes } from 'lodash'
import { some, curry, isEmpty } from 'lodash'
import { createAction } from 'redux-actions'
import { equals } from '../utils/string-utils'

Expand All @@ -42,31 +38,6 @@ const unwrapResponse = (dispatch, errorMsg, response) => {
return response.json()
}

const getUserPermissions = (localeId, projectSlug) => {
const endpoint = `/permission/roles/locale/${localeId}/project/${projectSlug}`
const apiTypes = [
USER_PERMISSION_REQUEST,
{
type: USER_PERMISSION_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_PERMISSION_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}

export const uiLocaleFetched = createAction(UI_LOCALES_FETCHED)

export function fetchUiLocales () {
Expand Down Expand Up @@ -112,6 +83,8 @@ export function fetchHeaderInfo (projectSlug, versionSlug, docId, localeId) {
return (dispatch, getState) => {
const checkResponse = curry(unwrapResponse)(dispatch)

dispatch(getUserPermissions(localeId, projectSlug))

// FIXME make the checkResponse just reject with the error code
// no need to handle error messages or anything.

Expand Down Expand Up @@ -139,19 +112,17 @@ export function fetchHeaderInfo (projectSlug, versionSlug, docId, localeId) {
e.message = 'version locales fetch failed: ' + e.message
throw e
})
const userPermissionsPromise = getUserPermissions(localeId, projectSlug)

// FIXME Split to separate handlers for easier debugging and maintenance.
// There is no real dependency for each of these requests to have to
// all complete before storing the relevant data.
Promise.all([docListPromise, projectInfoPromise,
myInfoPromise, versionLocalesPromise, userPermissionsPromise])
myInfoPromise, versionLocalesPromise])
.then((all) => {
const documents = all[0]
const projectInfo = all[1]
const myInfo = all[2]
const locales = all[3]
const permissions = all[4]

if (isEmpty(documents)) {
// redirect if no documents in version
Expand All @@ -178,8 +149,7 @@ export function fetchHeaderInfo (projectSlug, versionSlug, docId, localeId) {
projectInfo: projectInfo,
versionSlug: versionSlug,
documents: documents,
locales: locales,
permissions: permissions
locales: locales
}

dispatch(headerDataFetched(data))
Expand Down
31 changes: 31 additions & 0 deletions server/zanata-frontend/src/app/editor/api/index.js
Expand Up @@ -13,6 +13,14 @@ import {
STATUS_TRANSLATED,
STATUS_APPROVED
} from '../utils/status-util'
import {
USER_PERMISSION_REQUEST,
USER_PERMISSION_SUCCESS,
USER_PERMISSION_FAILURE
} from '../actions/header-action-types'
import { buildAPIRequest, getJsonHeaders } from '../../actions/common-actions'
import { CALL_API } from 'redux-api-middleware'
import { includes } from 'lodash'
import { apiUrl, serverUrl } from '../../config'
import { stableStringify } from 'faster-stable-stringify'

Expand All @@ -26,6 +34,29 @@ export function profileUrl (username) {
return `${serverUrl}/profile/view/${username}`
}

export function getUserPermissions (localeId, projectSlug) {
const endpoint =
`${apiUrl}/user/permission/roles/locale/${localeId}/project/${projectSlug}`
const apiTypes = [
USER_PERMISSION_REQUEST,
{
type: USER_PERMISSION_SUCCESS,
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type')
if (contentType && includes(contentType, 'json')) {
return res.json().then((json) => {
return json
})
}
}
},
USER_PERMISSION_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}

export function fetchStatistics (projectSlug, versionSlug, docId, localeId) {
const statsUrl =
`${apiUrl}/stats/project/${projectSlug}/version/${versionSlug}/doc/${encode(docId)}/locale/${localeId}` // eslint-disable-line max-len
Expand Down
Expand Up @@ -5,7 +5,8 @@ import {
DOCUMENT_SELECTED,
HEADER_DATA_FETCHED,
LOCALE_SELECTED,
STATS_FETCHED
STATS_FETCHED,
USER_PERMISSION_SUCCESS
} from '../actions/header-action-types'
import update from 'immutability-helper'
import {prepareLocales, prepareStats, prepareDocs} from '../utils/Util'
Expand Down Expand Up @@ -39,7 +40,11 @@ const defaultState = {
},
id: ''
},
selectedLocale: ''
selectedLocale: '',
permissions: {
reviewer: false,
translator: false
}
}
}

Expand All @@ -51,7 +56,6 @@ const headerDataReducer = handleActions({
[HEADER_DATA_FETCHED]: (state, { payload: {
documents, locales, versionSlug, projectInfo, myInfo, permissions } }) => {
const projectSlug = projectInfo.id
console.log(permissions)
return update(state, {
user: {
name: {$set: myInfo.name},
Expand All @@ -68,8 +72,7 @@ const headerDataReducer = handleActions({
version: {$set: versionSlug},
url: {$set: projectPageUrl(projectSlug, versionSlug)},
docs: {$set: prepareDocs(documents)},
locales: {$set: prepareLocales(locales)},
permissions: {$set: permissions}
locales: {$set: prepareLocales(locales)}
}
}
})
Expand All @@ -82,7 +85,10 @@ const headerDataReducer = handleActions({
update(state, { context: { selectedLocale: {$set: payload} } }),

[STATS_FETCHED]: (state, { payload }) => update(state, {
context: { selectedDoc: { counts: {$set: prepareStats(payload)} } } })
context: { selectedDoc: { counts: {$set: prepareStats(payload)} } } }),

[USER_PERMISSION_SUCCESS]: (state, { payload }) => update(state, {
permissions: {$set: payload} })
}, defaultState)

export default headerDataReducer

0 comments on commit c081b8f

Please sign in to comment.