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, withState, 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 { canMakeRevision } from 'pubsweet-component-faraday-selectors/src' import ManuscriptLayout from './ManuscriptLayout' import { parseSearchParams, redirectToError } from './utils' export default compose( setDisplayName('ManuscriptPage'), withJournal, withRouter, 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), version: selectFragment(state, match.params.version), project: selectCollection(state, match.params.project), editorialRecommendations: selectEditorialRecommendations( state, match.params.version, ), }), { replace, getSignedUrl, clearCustomError, reviewerDecision, getFragment: actions.getFragment, getCollection: actions.getCollection, updateVersion: actions.updateFragment, }, ), connect((state, { project, version }) => ({ canMakeRevision: canMakeRevision(state, project, version), })), 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 { match, replace, history, location, getFragment, getCollection, reviewerDecision, setEditorInChief, clearCustomError, hasManuscriptFailure, } = this.props 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)), ) }, }), )(ManuscriptLayout)