Newer
Older
import { connect } from 'react-redux'
import { actions } from 'pubsweet-client'
import { ConnectPage } from 'xpub-connect'
import { withJournal } from 'xpub-journal'
import { getFormValues } from 'redux-form'
import { replace } from 'react-router-redux'
import { withRouter } from 'react-router-dom'
import { head, get, isEmpty, isUndefined } from 'lodash'
import {
selectFragment,
selectCollection,
selectCurrentUser,
} from 'xpub-selectors'
import { get as apiGet } from 'pubsweet-client/src/helpers/api'
import {
compose,
withState,
lifecycle,
withProps,
withHandlers,
setDisplayName,
fromRenderProps,
} from 'recompose'
import { getSignedUrl } from 'pubsweet-components-faraday/src/redux/files'
import {
inviteReviewer,

Alexandru Munteanu
committed
revokeReviewer,
reviewerDecision,
} from 'pubsweet-components-faraday/src/redux/reviewers'
import {
hasManuscriptFailure,
clearCustomError,
} from 'pubsweet-components-faraday/src/redux/errors'
import { selectEditorialRecommendations } from 'pubsweet-components-faraday/src/redux/recommendations'
import {
getUserToken,
canMakeRevision,
canMakeDecision,
canEditManuscript,

Alexandru Munteanu
committed
canInviteReviewers,
pendingHEInvitation,
currentUserIsReviewer,
canOverrideTechnicalChecks,

Alexandru Munteanu
committed
getInvitationsWithReviewersForFragment,
} from 'pubsweet-component-faraday-selectors'
import { RemoteOpener } from 'pubsweet-component-faraday-ui'
import ManuscriptLayout from './ManuscriptLayout'

Alexandru Munteanu
committed
import { parseEicDecision, parseSearchParams, redirectToError } from './utils'
import {
canAssignHE,
getHandlingEditors,
assignHandlingEditor,
revokeHandlingEditor,
selectHandlingEditors,
handlingEditorDecision,
} from '../redux/editors'

Alexandru Munteanu
committed
import {
createRecommendation,
recommendationsFetching,
} from '../redux/recommendations'
export default compose(
setDisplayName('ManuscriptPage'),
withState('editorInChief', 'setEiC', 'N/A'),
ConnectPage(({ match }) => [
actions.getCollection({ id: match.params.project }),
actions.getFragments({ id: match.params.project }),
actions.getUsers(),
]),
connect(
(state, { match }) => ({
currentUser: selectCurrentUser(state),
handlingEditors: selectHandlingEditors(state),
hasManuscriptFailure: hasManuscriptFailure(state),
fragment: selectFragment(state, match.params.version),
collection: parseCollectionDetails(
state,
selectCollection(state, match.params.project),
),
pendingHEInvitation: pendingHEInvitation(state, match.params.project),

Alexandru Munteanu
committed
editorialRecommendations: selectEditorialRecommendations(
state,
match.params.version,
),
{
replace,
getSignedUrl,
reviewerDecision,
assignHandlingEditor,

Alexandru Munteanu
committed
createRecommendation,
revokeHandlingEditor,

Alexandru Munteanu
committed
getUsers: actions.getUsers,
getFragment: actions.getFragment,
getCollection: actions.getCollection,
updateVersion: actions.updateFragment,
connect(
(
state,
{ pendingHEInvitation, currentUser, match, collection, fragment },
) => ({
currentUser: {
...currentUser,
token: getUserToken(state),
isEIC: currentUserIs(state, 'adminEiC'),
isHE: currentUserIs(state, 'isHE'),
isReviewer: currentUserIsReviewer(state),
isInvitedHE: !isUndefined(pendingHEInvitation),
permissions: {
canAssignHE: canAssignHE(state, match.params.project),

Alexandru Munteanu
committed
canInviteReviewers: canInviteReviewers(state, collection),
canMakeRevision: canMakeRevision(state, collection, fragment),
canMakeDecision: canMakeDecision(state, collection, fragment),
canEditManuscript: canEditManuscript(state, collection, fragment),
canOverrideTechChecks: canOverrideTechnicalChecks(state, collection),
canMakeRecommendation: canMakeRecommendation(
state,
collection,
fragment,
),
},
},
isFetching: {
editorsFetching: selectFetching(state),
recommendationsFetching: recommendationsFetching(state),
},
formValues: {
eicDecision: getFormValues('eic-decision')(state),
heInvitation: getFormValues('he-answer-invitation')(state),
},

Alexandru Munteanu
committed
invitationsWithReviewers: getInvitationsWithReviewersForFragment(
state,
get(fragment, 'id', ''),
),
}),
),
ConnectPage(({ currentUser }) => {
if (currentUser.isEIC) {
return [getHandlingEditors()]
}
return []
withHandlers({
fetchUpdatedCollection: ({
fragment,

Alexandru Munteanu
committed
getUsers,
collection,
getFragment,
getCollection,
}) => () => {
getCollection({ id: collection.id })
getFragment(collection, fragment)

Alexandru Munteanu
committed
getUsers()
},
}),
withHandlers({
updateManuscript: ({ updateVersion, collection, fragment }) => data =>
updateVersion(collection, {
id: fragment.id,
...data,
}),
setEditorInChief: ({ setEiC }) => eic => {
if (eic) {
const { firstName = '', lastName = '' } = eic
setEiC(`${firstName} ${lastName}`)
}
},
assignHE: ({
assignHandlingEditor,
fetchUpdatedCollection,
collection: { id: collectionId },
}) => (email, modalProps) =>
assignHandlingEditor({
email,
collectionId,
})
fetchUpdatedCollection()
modalProps.hideModal()
})
.catch(() => modalProps.setModalError('Oops! Something went wrong.')),
revokeHE: ({
getCollection,
revokeHandlingEditor,
collection: { id: collectionId },
}) => (invitationId, modalProps) =>
revokeHandlingEditor({
invitationId,
collectionId,
})
.then(() => {
getCollection({ id: collectionId })
modalProps.hideModal()
})
.catch(() => modalProps.setModalError('Oops! Something went wrong.')),

Alexandru Munteanu
committed
createRecommendation: ({
fragment,
collection,
fetchUpdatedCollection,

Alexandru Munteanu
committed
createRecommendation,
}) => (values, modalProps) => {
const recommendation = parseEicDecision(values)
createRecommendation({
recommendation,
fragmentId: fragment.id,
collectionId: collection.id,
})
.then(() => {
fetchUpdatedCollection()

Alexandru Munteanu
committed
modalProps.hideModal()
})
.catch(() => {
modalProps.setModalError('Oops! Something went wrong.')
})
},
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
onHEResponse: ({
history,
collection,
pendingHEInvitation,
fetchUpdatedCollection,
}) => (values, { hideModal, setModalError, setFetching }) => {
const isAccepted = get(values, 'decision', 'decline') === 'accept'
setFetching(true)
return handlingEditorDecision({
isAccepted,
collectionId: collection.id,
reason: get(values, 'reason', ''),
invitationId: pendingHEInvitation.id,
})
.then(() => {
setFetching(false)
hideModal()
if (isAccepted) {
fetchUpdatedCollection()
} else {
history.replace('/')
}
})
.catch(() => {
setFetching(false)
setModalError('Something went wrong...')
})
},
onInviteReviewer: ({ collection, fragment, fetchUpdatedCollection }) => (
values,
{ hideModal, setModalError, setFetching },
) => {
setFetching(true)
inviteReviewer({
reviewerData: values,
fragmentId: fragment.id,
collectionId: collection.id,
})
.then(() => {
setFetching(false)
hideModal()
fetchUpdatedCollection()
})
.catch(() => {
setFetching(false)
setModalError('Something went wrong...')
})
},

Alexandru Munteanu
committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
onResendReviewerInvite: ({
fragment,
collection,
fetchUpdatedCollection,
}) => (email, { hideModal, setFetching, setModalError }) => {
setFetching(true)
inviteReviewer({
reviewerData: {
email,
role: 'reviewer',
},
fragmentId: fragment.id,
collectionId: collection.id,
})
.then(() => {
setFetching(false)
hideModal()
fetchUpdatedCollection()
})
.catch(() => {
setFetching(false)
setModalError('Something went wrong...')
})
},
onRevokeReviewerInvite: ({
fragment,
collection,
fetchUpdatedCollection,
}) => (invitationId, { hideModal, setFetching, setModalError }) => {
setFetching(true)
revokeReviewer({
invitationId,
fragmentId: fragment.id,
collectionId: collection.id,
})
.then(() => {
setFetching(false)
hideModal()
fetchUpdatedCollection()
})
.catch(() => {
setFetching(false)
setModalError('Something went wrong...')
})
},
fromRenderProps(RemoteOpener, ({ toggle, expanded }) => ({
toggleAssignHE: toggle,
heExpanded: expanded,
})),
fromRenderProps(RemoteOpener, ({ toggle, expanded }) => ({
toggleHEResponse: toggle,
heResponseExpanded: expanded,
})),
lifecycle({
componentDidMount() {
const {
reviewerDecision,
setEditorInChief,
clearCustomError,
hasManuscriptFailure,
currentUser: { isInvitedHE },
if (hasManuscriptFailure) {
history.push('/not-found')
clearCustomError()
}
const collectionId = match.params.project
const fragmentId = match.params.version
const { agree, invitationId } = parseSearchParams(location.search)
if (agree === 'true') {
replace(location.pathname)
reviewerDecision(invitationId, collectionId, fragmentId, true)
.then(() => {
getCollection({ id: collectionId })
getFragment({ id: collectionId }, { id: fragmentId })
})
.catch(redirectToError(replace))
}
apiGet(`/users?editorInChief=true`).then(res =>
setEditorInChief(head(res.users)),
)
if (isInvitedHE) {
this.props.toggleHEResponse()
}
withProps(({ fragment }) => ({
hasResponseToReviewers:
!isEmpty(get(fragment, 'files.responseToReviewers')) ||
get(fragment, 'commentsToReviewers'),
)(ManuscriptLayout)