Skip to content
Snippets Groups Projects
ManuscriptPage.js 3.54 KiB
Newer Older
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'
  selectCollection,
  selectCurrentUser,
import { get as apiGet } from 'pubsweet-client/src/helpers/api'
import {
  compose,
  lifecycle,
  withState,
  withHandlers,
  setDisplayName,
} from 'recompose'
import { getSignedUrl } from 'pubsweet-components-faraday/src/redux/files'
import { reviewerDecision } from 'pubsweet-components-faraday/src/redux/reviewers'
import { selectEditorialRecommendations } from 'pubsweet-components-faraday/src/redux/recommendations'
import {
  getHandlingEditors,
  selectHandlingEditors,
} from 'pubsweet-components-faraday/src/redux/editors'

import ManuscriptLayout from './ManuscriptLayout'
import { parseSearchParams, redirectToError } from './utils'
import {
  getPubsweetError,
  canSeeReviewersReports,
  canSeeEditorialComments,
} from '../../../component-faraday-selectors'
  setDisplayName('ManuscriptPage'),
  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),
      pubsweetError: getPubsweetError(state),
      handlingEditors: selectHandlingEditors(state),
      version: selectFragment(state, match.params.version),
      project: selectCollection(state, match.params.project),
      editorialRecommendations: selectEditorialRecommendations(state),
      canSeeReviewersReports: canSeeReviewersReports(
        state,
        match.params.project,
      ),
      canSeeEditorialComments: canSeeEditorialComments(
        state,
        match.params.project,
      ),
      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}`)
      }
    },
  }),
  lifecycle({
    componentDidMount() {
      const {
        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)),
      )
    },