From acf62aa47afe9928d454f8fe94e11fb716c502b0 Mon Sep 17 00:00:00 2001
From: Bogdan Cochior <bogdan.cochior@thinslices.com>
Date: Tue, 7 Aug 2018 15:06:08 +0300
Subject: [PATCH] feat(reject): show reject button on manuscript status
 submitted

---
 .../component-faraday-selectors/src/index.js  |  6 +-
 .../notifications/notifications.js            | 49 ++++++++--------
 .../src/components/Dashboard/DashboardCard.js |  1 +
 .../src/components/MakeDecision/Decision.js   | 58 ++++++++++++++++---
 .../components/MakeDecision/DecisionForm.js   |  1 -
 5 files changed, 78 insertions(+), 37 deletions(-)

diff --git a/packages/component-faraday-selectors/src/index.js b/packages/component-faraday-selectors/src/index.js
index 65af2dd10..c5ce6d09d 100644
--- a/packages/component-faraday-selectors/src/index.js
+++ b/packages/component-faraday-selectors/src/index.js
@@ -60,15 +60,13 @@ export const getHERecommendation = (state, collectionId, fragmentId) => {
   )
 }
 
-const cantMakeDecisionStatuses = ['rejected', 'published', 'draft']
+const canMakeDecisionStatuses = ['submitted', 'pendingApproval']
 export const canMakeDecision = (state, collection, fragment = {}) => {
   if (fragment.id !== last(collection.fragments)) return false
   const status = get(collection, 'status')
 
-  if (!status || cantMakeDecisionStatuses.includes(status)) return false
-
   const isEIC = currentUserIs(state, 'adminEiC')
-  return isEIC && status
+  return isEIC && canMakeDecisionStatuses.includes(status)
 }
 
 export const canSeeReviewersReports = (state, collectionId) => {
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
index f99bbd3b5..3f46997b9 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/notifications/notifications.js
@@ -64,8 +64,10 @@ module.exports = {
 
       comments = eicComments
     }
-
-    if (isEditorInChief || newRecommendation.recommendationType === 'review') {
+    if (
+      (isEditorInChief || newRecommendation.recommendationType === 'review') &&
+      collection.status !== 'rejected'
+    ) {
       // the request came from either the Editor in Chief or a reviewer, so the HE needs to be notified
       sendHandlingEditorEmail({
         email,
@@ -95,28 +97,29 @@ module.exports = {
         subjectBaseText,
         newRecommendation,
       })
+      if (collection.status !== 'rejected') {
+        sendReviewersEmail({
+          email,
+          baseUrl,
+          UserModel,
+          titleText,
+          fragmentHelper,
+          isEditorInChief,
+          subjectBaseText,
+          recommendation: newRecommendation.recommendation,
+          handlingEditorName: get(collection, 'handlingEditor.name', 'N/A'),
+        })
 
-      sendReviewersEmail({
-        email,
-        baseUrl,
-        UserModel,
-        titleText,
-        fragmentHelper,
-        isEditorInChief,
-        subjectBaseText,
-        recommendation: newRecommendation.recommendation,
-        handlingEditorName: get(collection, 'handlingEditor.name', 'N/A'),
-      })
-
-      sendEiCEmail({
-        email,
-        baseUrl,
-        eicName,
-        eicEmail,
-        titleText,
-        subjectBaseText,
-        recommendation: newRecommendation,
-      })
+        sendEiCEmail({
+          email,
+          baseUrl,
+          eicName,
+          eicEmail,
+          titleText,
+          subjectBaseText,
+          recommendation: newRecommendation,
+        })
+      }
     }
   },
 }
diff --git a/packages/components-faraday/src/components/Dashboard/DashboardCard.js b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
index 2d6ec271f..d0b8f03d4 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardCard.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
@@ -66,6 +66,7 @@ const DashboardCard = ({
                   collectionId={project.id}
                   fragmentId={version.id}
                   modalKey={`decide-${version.id}`}
+                  status={project.status}
                 />
               )}
               {canMakeRecommendation && (
diff --git a/packages/components-faraday/src/components/MakeDecision/Decision.js b/packages/components-faraday/src/components/MakeDecision/Decision.js
index cf4279e16..be2b86e66 100644
--- a/packages/components-faraday/src/components/MakeDecision/Decision.js
+++ b/packages/components-faraday/src/components/MakeDecision/Decision.js
@@ -1,17 +1,21 @@
 import React from 'react'
 import { th } from '@pubsweet/ui'
+import { connect } from 'react-redux'
 import styled from 'styled-components'
-import { compose, withHandlers, setDisplayName } from 'recompose'
+import { actions } from 'pubsweet-client'
+import { compose, withHandlers, setDisplayName, withProps } from 'recompose'
 
 import {
   ConfirmationModal,
   withModal,
 } from 'pubsweet-component-modal/src/components'
+import { handleError } from '../utils'
+import { createRecommendation } from '../../redux/recommendations'
 
 import { DecisionForm } from './'
 
-const Decision = ({ showDecisionModal }) => (
-  <Root onClick={showDecisionModal}>Make decision</Root>
+const Decision = ({ showDecisionModal, buttonText }) => (
+  <Root onClick={showDecisionModal}>{buttonText}</Root>
 )
 
 const ModalComponent = ({ type, ...rest }) => {
@@ -28,19 +32,55 @@ export default compose(
   withModal(() => ({
     modalComponent: ModalComponent,
   })),
+  connect(null, {
+    createRecommendation,
+    getFragments: actions.getFragments,
+    getCollections: actions.getCollections,
+  }),
+  withProps(({ status }) => ({
+    buttonText: status === 'submitted' ? 'Reject' : 'Make Decision',
+  })),
   withHandlers({
     showDecisionModal: ({
+      status,
       showModal,
       hideModal,
       fragmentId,
       collectionId,
+      getFragments,
+      setModalError,
+      getCollections,
+      createRecommendation,
     }) => () => {
-      showModal({
-        type: 'decision',
-        hideModal,
-        fragmentId,
-        collectionId,
-      })
+      status !== 'submitted'
+        ? showModal({
+            type: 'decision',
+            hideModal,
+            fragmentId,
+            collectionId,
+          })
+        : showModal({
+            hideModal,
+            fragmentId,
+            collectionId,
+            title: 'Reject Manuscript?',
+            confirmText: 'Reject',
+            onConfirm: () => {
+              const recommendation = {
+                recommendation: 'reject',
+                recommendationType: 'editorRecommendation',
+              }
+              createRecommendation(
+                collectionId,
+                fragmentId,
+                recommendation,
+              ).then(() => {
+                getCollections()
+                getFragments()
+                hideModal()
+              }, handleError(setModalError))
+            },
+          })
     },
   }),
 )(Decision)
diff --git a/packages/components-faraday/src/components/MakeDecision/DecisionForm.js b/packages/components-faraday/src/components/MakeDecision/DecisionForm.js
index 0a53c9423..3fab58f21 100644
--- a/packages/components-faraday/src/components/MakeDecision/DecisionForm.js
+++ b/packages/components-faraday/src/components/MakeDecision/DecisionForm.js
@@ -27,7 +27,6 @@ const {
 const Form = RootContainer.withComponent(FormContainer)
 
 const DecisionForm = ({
-  aHERec,
   decision,
   hideModal,
   handleSubmit,
-- 
GitLab