Skip to content
Snippets Groups Projects
ManuscriptPage.js 3.36 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 } from 'lodash'
import {
  selectCurrentUser,
  selectCollection,
  selectFragment,
} from 'xpub-selectors'
import { get as apiGet } from 'pubsweet-client/src/helpers/api'
import { replace } from 'react-router-redux'
import { compose, lifecycle, withHandlers, withState } from 'recompose'
import { reviewerDecision } from 'pubsweet-components-faraday/src/redux/reviewers'
import { getSignedUrl } from 'pubsweet-components-faraday/src/redux/files'
import {
  getHandlingEditors,
  selectHandlingEditors,
} from 'pubsweet-components-faraday/src/redux/editors'

import ManuscriptLayout from './ManuscriptLayout'
import { parseSearchParams, redirectToError } from './utils'
  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),
      project: selectCollection(state, match.params.project),
      version: selectFragment(state, match.params.version),
    }),
    {
      reviewerDecision,
      replace,
      updateVersion: actions.updateFragment,
      getSignedUrl,
    },
  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,
      }),
    previewFile: ({ getSignedUrl }) => fileId => e => {
      e.preventDefault()
      getSignedUrl(fileId).then(({ signedUrl }) => {
        const windowReference = window.open()
        windowReference.location = signedUrl
      })
    },
    downloadFile: ({ getSignedUrl }) => (fileId, fileName) => e => {
      e.preventDefault()
      getSignedUrl(fileId).then(({ signedUrl }) => {
        const a = document.createElement('a')
        a.href = `${signedUrl}`
        a.download = fileName
        a.target = '_blank'
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      })
    },
    setEditorInChief: ({ setEiC }) => eic => {
      if (eic) {
        const { firstName = '', lastName = '' } = eic
        setEiC(`${firstName} ${lastName}`)
      }
    },
  }),
  lifecycle({
    componentDidMount() {
      const {
        reviewerDecision,
        replace,
        location,
        match,
        setEditorInChief,
      } = this.props
      const collectionId = match.params.project
      const { agree, invitationId } = parseSearchParams(location.search)
      if (agree === 'true') {
        replace(location.pathname)
        reviewerDecision(invitationId, collectionId, true).catch(
          redirectToError(replace),
        )
      }

      apiGet(`/users?editorInChief=true`).then(res =>
        setEditorInChief(head(res.users)),
      )
    },