From 9dd5bf409cb16ecf0e62f4bf40b443a398aae781 Mon Sep 17 00:00:00 2001
From: Anca Ursachi <anca.ursachi@thinslices.com>
Date: Wed, 14 Nov 2018 16:25:09 +0200
Subject: [PATCH] fix(HERecommendation): On FE HE cannot have the option to
 publish if the collection doesn't have one

---
 .../component-faraday-selectors/src/index.js  | 25 ++++++-
 .../src/contextualBoxes/HERecommendation.js   | 35 +++-------
 .../src/components/ManuscriptLayout.js        | 67 ++++++++++---------
 .../src/components/ManuscriptPage.js          |  4 ++
 4 files changed, 71 insertions(+), 60 deletions(-)

diff --git a/packages/component-faraday-selectors/src/index.js b/packages/component-faraday-selectors/src/index.js
index 294058e27..bf878bdbe 100644
--- a/packages/component-faraday-selectors/src/index.js
+++ b/packages/component-faraday-selectors/src/index.js
@@ -1,5 +1,6 @@
 import { selectCurrentUser } from 'xpub-selectors'
-import { get, has, last, chain, some, isEmpty } from 'lodash'
+// eslint-disable-next-line no-unused-vars
+import { get, has, last, chain, some, isEmpty, flatten } from 'lodash'
 
 export const isHEToManuscript = (state, collectionId = '') => {
   const { id = '', isAccepted = false } = chain(state)
@@ -229,6 +230,28 @@ export const canMakeDecision = (state, collection = {}) => {
   return isEIC && canMakeDecisionStatuses.includes(status)
 }
 
+const collectionReviewerReports = state =>
+  chain(state)
+    .get('fragments', {})
+    .map(r => get(r, 'recommendations', []))
+    .flatten()
+    .find(r => r.recommendationType === 'review' && r.submittedOn)
+    .value()
+
+export const canHEMakeRecommendationToPublish = state =>
+  !!collectionReviewerReports(state)
+
+const canHEOnlyRejectStatuses = [
+  'reviewersInvited',
+  'underReview',
+  'revisionRequested',
+]
+
+export const canHEOnlyReject = (collection = {}) => {
+  const { status } = collection
+  return canHEOnlyRejectStatuses.includes(status)
+}
+
 const canEditManuscriptStatuses = ['draft', 'technicalChecks', 'inQA']
 export const canEditManuscript = (state, collection = {}, fragment = {}) => {
   const isAdmin = currentUserIs(state, 'isAdmin')
diff --git a/packages/component-faraday-ui/src/contextualBoxes/HERecommendation.js b/packages/component-faraday-ui/src/contextualBoxes/HERecommendation.js
index 69f2a7b7f..3eb14eb32 100644
--- a/packages/component-faraday-ui/src/contextualBoxes/HERecommendation.js
+++ b/packages/component-faraday-ui/src/contextualBoxes/HERecommendation.js
@@ -46,27 +46,14 @@ const options = [
   },
 ]
 
-const optionsWhereHECanOnlyReject = [
-  'reviewersInvited',
-  'underReview',
-  'revisionRequested',
-]
-
 const showHEOptions = ({
-  collection,
-  hasReviewerReports,
-  fragment,
-  options,
-  optionsWhereHECanOnlyReject,
+  canHEMakeRecommendationToPublish,
+  canHEOnlyReject,
 }) => {
-  const { status, fragments } = collection
-  const { invitations } = fragment
-  if (optionsWhereHECanOnlyReject.includes(status)) {
+  if (canHEOnlyReject) {
     return [options[1]]
-  } else if (!hasReviewerReports && fragments.length === 1) {
+  } else if (!canHEMakeRecommendationToPublish) {
     return tail(options)
-  } else if (invitations === []) {
-    return [options[1]]
   }
   return options
 }
@@ -86,12 +73,11 @@ const parseFormValues = ({ recommendation, ...rest }) => {
 }
 
 const HERecommendation = ({
-  formValues,
+  canHEMakeRecommendationToPublish,
+  canHEOnlyReject,
   handleSubmit,
-  hasReviewerReports,
+  formValues,
   highlight,
-  collection,
-  fragment,
 }) => (
   <ContextualBox
     highlight={highlight}
@@ -110,11 +96,8 @@ const HERecommendation = ({
             component={input => (
               <Menu
                 options={showHEOptions({
-                  collection,
-                  hasReviewerReports,
-                  fragment,
-                  options,
-                  optionsWhereHECanOnlyReject,
+                  canHEMakeRecommendationToPublish,
+                  canHEOnlyReject,
                 })}
                 {...input}
               />
diff --git a/packages/component-manuscript/src/components/ManuscriptLayout.js b/packages/component-manuscript/src/components/ManuscriptLayout.js
index f47d2b1db..0bdcf5242 100644
--- a/packages/component-manuscript/src/components/ManuscriptLayout.js
+++ b/packages/component-manuscript/src/components/ManuscriptLayout.js
@@ -30,51 +30,52 @@ const cannotViewReviewersDetails = ['revisionRequested', 'pendingApproval']
 
 const ManuscriptLayout = ({
   history,
-  currentUser,
-  getSignedUrl,
-  editorInChief,
-  handlingEditors,
-  editorialRecommendations,
   journal = {},
-  collection = {},
   fragment = {},
-  changeForm,
+  versions,
   isFetching,
-  isFetchingData,
-  publonsFetching,
-  fetchingError,
+  changeForm,
   formValues,
   heExpanded,
+  collection = {},
+  currentUser,
+  getSignedUrl,
+  shouldReview,
+  editorInChief,
+  fetchingError,
   toggleAssignHE,
+  isFetchingData,
+  submitRevision,
+  inviteReviewer,
+  isLatestVersion,
+  publonsFetching,
+  publonReviewers,
+  reviewerReports,
+  handlingEditors,
+  canHEOnlyReject,
   toggleHEResponse,
   heResponseExpanded,
+  inviteHandlingEditor,
+  toggleReviewerDetails,
+  recommendationHandler,
   toggleReviewerResponse,
+  reviewerDetailsExpanded,
+  toggleEditorialComments,
+  reviewerRecommendations,
   invitationsWithReviewers,
-  responseToRevisionRequest,
-  publonReviewers,
-  reviewerResponseExpanded,
   pendingOwnRecommendation,
+  reviewerResponseExpanded,
+  editorialRecommendations,
+  responseToRevisionRequest,
+  editorialCommentsExpanded,
+  submittedOwnRecommendation,
   toggleReviewerRecommendations,
   reviewerRecommendationExpanded,
   authorResponseToRevisonRequest,
   toggleResponeToRevisionRequest,
-  responseToRevisionRequestExpanded,
-  shouldReview,
-  submittedOwnRecommendation,
-  reviewerReports,
-  reviewerRecommendations,
-  toggleReviewerDetails,
-  reviewerDetailsExpanded,
   toggleResponseToRevisionRequest,
-  editorialCommentsExpanded,
-  toggleEditorialComments,
-  submitRevision,
-  inviteReviewer,
-  recommendationHandler,
-  inviteHandlingEditor,
-
-  versions,
-  isLatestVersion,
+  canHEMakeRecommendationToPublish,
+  responseToRevisionRequestExpanded,
 }) => (
   <Root pb={30}>
     {!isEmpty(collection) && !isEmpty(fragment) ? (
@@ -101,7 +102,6 @@ const ManuscriptLayout = ({
           revokeInvitation={inviteHandlingEditor.revokeHE}
           versions={versions}
         />
-
         <ManuscriptMetadata
           currentUser={currentUser}
           fragment={fragment}
@@ -228,10 +228,11 @@ const ManuscriptLayout = ({
         {isLatestVersion &&
           get(currentUser, 'permissions.canMakeHERecommendation', false) && (
             <HERecommendation
-              collection={collection}
+              canHEMakeRecommendationToPublish={
+                canHEMakeRecommendationToPublish
+              }
+              canHEOnlyReject={canHEOnlyReject}
               formValues={get(formValues, 'editorialRecommendation', {})}
-              fragment={fragment}
-              hasReviewerReports={reviewerRecommendations.length > 0}
               highlight={reviewerRecommendations.length > 0}
               modalKey="heRecommendation"
               onRecommendationSubmit={
diff --git a/packages/component-manuscript/src/components/ManuscriptPage.js b/packages/component-manuscript/src/components/ManuscriptPage.js
index 030805e20..89062c939 100644
--- a/packages/component-manuscript/src/components/ManuscriptPage.js
+++ b/packages/component-manuscript/src/components/ManuscriptPage.js
@@ -36,6 +36,7 @@ import {
   currentUserIs,
   canViewReports,
   canMakeRevision,
+  canHEOnlyReject,
   canMakeDecision,
   isHEToManuscript,
   canSubmitRevision,
@@ -56,6 +57,7 @@ import {
   getOwnPendingRecommendation,
   getOwnSubmittedRecommendation,
   canAuthorViewEditorialComments,
+  canHEMakeRecommendationToPublish,
   getFragmentReviewerRecommendations,
   getInvitationsWithReviewersForFragment,
 } from 'pubsweet-component-faraday-selectors'
@@ -206,6 +208,8 @@ export default compose(
           ),
         },
       },
+      canHEMakeRecommendationToPublish: canHEMakeRecommendationToPublish(state),
+      canHEOnlyReject: canHEOnlyReject(collection),
       isFetchingData: {
         editorsFetching: selectFetching(state),
         publonsFetching: isFetching,
-- 
GitLab