Skip to content
Snippets Groups Projects
ManuscriptPage.js 4.7 KiB
Newer Older
import { connect } from 'react-redux'
import { actions } from 'pubsweet-client'
import { ConnectPage } from 'xpub-connect'
import { withJournal } from 'xpub-journal'
import { head, get, isEmpty } from 'lodash'
import { replace } from 'react-router-redux'
import { withRouter } from 'react-router-dom'
  selectCollection,
  selectCurrentUser,
import { get as apiGet } from 'pubsweet-client/src/helpers/api'
  withHandlers,
  setDisplayName,
} from 'recompose'
import { getSignedUrl } from 'pubsweet-components-faraday/src/redux/files'
import { 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 {
  getHandlingEditors,
  selectHandlingEditors,
} from 'pubsweet-components-faraday/src/redux/editors'
import {
  getUserToken,
  canMakeRevision,
  canMakeDecision,
  canEditManuscript,
  canMakeRecommendation,
  currentUserIsReviewer,
  canOverrideTechnicalChecks,
} from 'pubsweet-component-faraday-selectors'

import ManuscriptLayout from './ManuscriptLayout'
import { parseSearchParams, redirectToError } from './utils'
  setDisplayName('ManuscriptPage'),
  withState('editorInChief', 'setEiC', 'N/A'),
  ConnectPage(({ match }) => [
    actions.getCollection({ id: match.params.project }),
    actions.getFragments({ id: match.params.project }),
  ]),
  connect(
    (state, { match }) => ({
      currentUser: selectCurrentUser(state),
      handlingEditors: selectHandlingEditors(state),
      hasManuscriptFailure: hasManuscriptFailure(state),
      fragment: selectFragment(state, match.params.version),
      collection: selectCollection(state, match.params.project),
      editorialRecommendations: selectEditorialRecommendations(
        state,
        match.params.version,
      ),
      getFragment: actions.getFragment,
      getCollection: actions.getCollection,
      updateVersion: actions.updateFragment,
  connect((state, { currentUser, collection, fragment }) => ({
    currentUser: {
      ...currentUser,
      token: getUserToken(state),
      isReviewer: currentUserIsReviewer(state),
    },
    canMakeRevision: canMakeRevision(state, collection, fragment),
    permissions: {
      canMakeDecision: canMakeDecision(state, collection, fragment),
      canEditManuscript: canEditManuscript(state, collection, fragment),
      canOverrideTechChecks: canOverrideTechnicalChecks(state, collection),
      canMakeRecommendation: canMakeRecommendation(state, collection, fragment),
    },
  ConnectPage(({ currentUser, handlingEditors, collection }) => {
    const he = get(collection, 'handlingEditor')
    if (
      !he &&
      !handlingEditors.length &&
      (get(currentUser, 'admin') || get(currentUser, 'editorInChief'))
    ) {
      return [getHandlingEditors()]
    }
    return []
    updateManuscript: ({ updateVersion, collection, fragment }) => data =>
      updateVersion(collection, {
        id: fragment.id,
    setEditorInChief: ({ setEiC }) => eic => {
      if (eic) {
        const { firstName = '', lastName = '' } = eic
        setEiC(`${firstName} ${lastName}`)
      }
    },
  }),
  lifecycle({
    componentDidMount() {
      const {
        getCollection,
        reviewerDecision,
        setEditorInChief,
        clearCustomError,
        hasManuscriptFailure,
      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)),
      )
    },
  withProps(({ fragment }) => ({
    hasResponseToReviewers:
      !isEmpty(get(fragment, 'files.responseToReviewers')) ||
      get(fragment, 'commentsToReviewers'),