-
Alexandru Munteanu authoredc97fc0c2
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
import { connect } from 'react-redux'
import { head, get } from 'lodash'
import { actions } from 'pubsweet-client'
import { ConnectPage } from 'xpub-connect'
import { withJournal } from 'xpub-journal'
import { replace } from 'react-router-redux'
import { withRouter } from 'react-router-dom'
import {
selectFragment,
selectCollection,
selectCurrentUser,
} from 'xpub-selectors'
import { get as apiGet } from 'pubsweet-client/src/helpers/api'
import { compose, lifecycle, withHandlers, withState } from 'recompose'
import { getSignedUrl } from 'pubsweet-components-faraday/src/redux/files'
import { reviewerDecision } from 'pubsweet-components-faraday/src/redux/reviewers'
import {
getHandlingEditors,
selectHandlingEditors,
} from 'pubsweet-components-faraday/src/redux/editors'
import ManuscriptLayout from './ManuscriptLayout'
import { parseSearchParams, redirectToError } from './utils'
export default compose(
withJournal,
withRouter,
withState('editorInChief', 'setEiC', 'N/A'),
ConnectPage(({ match }) => [
actions.getCollection({ id: match.params.project }),
actions.getFragment(
{ id: match.params.project },
{ id: match.params.version },
),
]),
connect(
(state, { match }) => ({
currentUser: selectCurrentUser(state),
handlingEditors: selectHandlingEditors(state),
version: selectFragment(state, match.params.version),
project: selectCollection(state, match.params.project),
}),
{
replace,
getSignedUrl,
reviewerDecision,
getCollection: actions.getCollection,
updateVersion: actions.updateFragment,
},
),
ConnectPage(({ currentUser, handlingEditors, project }) => {
const he = get(project, 'handlingEditor')
if (
!he &&
!handlingEditors.length &&
(get(currentUser, 'admin') || get(currentUser, 'editorInChief'))
) {
return [getHandlingEditors()]
}
return []
}),
withHandlers({
updateManuscript: ({ updateVersion, project, version }) => data =>
updateVersion(project, {
id: version.id,
...data,
}),
setEditorInChief: ({ setEiC }) => eic => {
if (eic) {
const { firstName = '', lastName = '' } = eic
setEiC(`${firstName} ${lastName}`)
}
},
currentUserIs: ({ currentUser }) => type => {
const isAdmin = get(currentUser, 'admin')
const isEic = get(currentUser, 'editorInChief')
const isHe = get(currentUser, 'handlingEditor')
switch (type) {
case 'isHE':
return isHe
case 'staff':
return isAdmin || isEic || isHe
case 'adminEiC':
return isAdmin || isEic
default:
return false
}
},
}),
lifecycle({
componentDidMount() {
const {
match,
replace,
location,
getCollection,
reviewerDecision,
setEditorInChief,
} = this.props
const collectionId = match.params.project
const { agree, invitationId } = parseSearchParams(location.search)
if (agree === 'true') {
replace(location.pathname)
reviewerDecision(invitationId, collectionId, true)
.then(() => getCollection({ id: match.params.project }))
.catch(redirectToError(replace))
}
apiGet(`/users?editorInChief=true`).then(res =>
setEditorInChief(head(res.users)),
)
},
}),
)(ManuscriptLayout)