From d85fdfbc055948b9b1e389dacf7d7aa96ccdd8db Mon Sep 17 00:00:00 2001
From: Alexandru Munteanu <alexandru.munt@gmail.com>
Date: Thu, 19 Apr 2018 17:30:34 +0300
Subject: [PATCH] feat(agree-as-reviewer): agree to work as a reviewer

---
 .../src/components/Dashboard/DashboardCard.js | 22 +++++++--
 .../components/Dashboard/ReviewerDecision.js  | 48 +++++++++++++------
 .../components-faraday/src/redux/reviewers.js | 10 ++++
 3 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/packages/components-faraday/src/components/Dashboard/DashboardCard.js b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
index d291f8814..f266dd027 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardCard.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
@@ -1,5 +1,6 @@
 import React from 'react'
 import { get } from 'lodash'
+import { connect } from 'react-redux'
 import PropTypes from 'prop-types'
 import { Button, Icon, th } from '@pubsweet/ui'
 import styled, { css, withTheme } from 'styled-components'
@@ -11,8 +12,9 @@ import {
 
 import ZipFiles from './ZipFiles'
 import { InviteReviewers } from '../Reviewers/'
-import { parseVersion, parseJournalIssue, mapStatusToLabel } from './../utils'
+import { selectInvitation } from '../../redux/reviewers'
 import { AuthorTooltip, ReviewerDecision, HandlingEditorSection } from './'
+import { parseVersion, parseJournalIssue, mapStatusToLabel } from './../utils'
 
 const DashboardCard = ({
   deleteProject,
@@ -25,6 +27,7 @@ const DashboardCard = ({
   theme,
   currentUser,
   canInviteReviewers,
+  invitation,
   ...rest
 }) => {
   const { submitted, title, type } = parseVersion(version)
@@ -156,10 +159,16 @@ const DashboardCard = ({
               />
             )}
           </Bottom>
-          <Bottom>
-            <LeftDetails flex="5" />
-            <ReviewerDecision />
-          </Bottom>
+          {invitation && (
+            <Bottom>
+              <LeftDetails flex="5" />
+              <ReviewerDecision
+                invitation={invitation}
+                modalKey={`reviewer-decision-${project.id}`}
+                project={project}
+              />
+            </Bottom>
+          )}
         </DetailsView>
       )}
     </Card>
@@ -173,6 +182,9 @@ export default compose(
     modalKey: 'cancelManuscript',
     modalComponent: ConfirmationModal,
   }),
+  connect((state, { project }) => ({
+    invitation: selectInvitation(state, project.id),
+  })),
   withHandlers({
     canInviteReviewers: ({ currentUser, project }) => () => {
       const handlingEditor = get(project, 'handlingEditor')
diff --git a/packages/components-faraday/src/components/Dashboard/ReviewerDecision.js b/packages/components-faraday/src/components/Dashboard/ReviewerDecision.js
index c4d191194..882d92faf 100644
--- a/packages/components-faraday/src/components/Dashboard/ReviewerDecision.js
+++ b/packages/components-faraday/src/components/Dashboard/ReviewerDecision.js
@@ -1,19 +1,16 @@
 import React from 'react'
 import { connect } from 'react-redux'
 import { Button, th } from '@pubsweet/ui'
+import { actions } from 'pubsweet-client'
 import styled, { css } from 'styled-components'
 import { compose, withHandlers } from 'recompose'
 import {
-  withModal,
+  withModal2,
   ConfirmationModal,
 } from 'pubsweet-component-modal/src/components'
-import {
-  selectFetchingDecision,
-  reviewerAccept,
-  reviewerDecline,
-} from '../../redux/reviewers'
+import { selectFetchingDecision, reviewerDecision } from '../../redux/reviewers'
 
-const ReviewerDecision = ({ showAcceptModal, showDeclineModal }) => (
+const ReviewerDecision = ({ showAcceptModal, showDeclineModal, ...rest }) => (
   <div>
     <DecisionButton onClick={showDeclineModal}>Decline</DecisionButton>
     <DecisionButton onClick={showAcceptModal} primary>
@@ -30,27 +27,48 @@ const ModalComponent = connect(state => ({
 
 export default compose(
   connect(null, {
-    reviewerAccept,
-    reviewerDecline,
+    reviewerDecision,
+    getCollections: actions.getCollections,
   }),
-  withModal({
-    modalKey: 'reviewer-decision',
+  withModal2(props => ({
     modalComponent: ModalComponent,
+  })),
+  withHandlers({
+    decisionSuccess: ({ getCollections, hideModal }) => () => {
+      getCollections()
+      hideModal()
+    },
   }),
   withHandlers({
-    showAcceptModal: ({ showModal, hideModal, reviewerAccept }) => () => {
+    showAcceptModal: ({
+      project,
+      showModal,
+      invitation,
+      decisionSuccess,
+      reviewerDecision,
+    }) => () => {
       showModal({
         title: 'Agree to review Manuscript?',
         onConfirm: () => {
-          reviewerAccept().then(hideModal)
+          reviewerDecision(invitation.id, project.id, true).then(
+            decisionSuccess,
+          )
         },
       })
     },
-    showDeclineModal: ({ showModal, hideModal, reviewerDecline }) => () => {
+    showDeclineModal: ({
+      project,
+      showModal,
+      invitation,
+      decisionSuccess,
+      reviewerDecision,
+    }) => () => {
       showModal({
         title: 'Decline to review Manuscript?',
         onConfirm: () => {
-          reviewerDecline().then(hideModal)
+          reviewerDecision(invitation.id, project.id, false).then(
+            decisionSuccess,
+          )
         },
       })
     },
diff --git a/packages/components-faraday/src/redux/reviewers.js b/packages/components-faraday/src/redux/reviewers.js
index a54bd5ab1..1be269eac 100644
--- a/packages/components-faraday/src/redux/reviewers.js
+++ b/packages/components-faraday/src/redux/reviewers.js
@@ -1,4 +1,5 @@
 import { get } from 'lodash'
+import { selectCurrentUser } from 'xpub-selectors'
 import {
   get as apiGet,
   create,
@@ -73,6 +74,15 @@ export const selectFechingInvite = state =>
 export const selectFetchingDecision = state =>
   get(state, 'reviewers.fetching.decision') || false
 
+export const selectInvitation = (state, collectionId) => {
+  const currentUser = selectCurrentUser(state)
+  const collection = state.collections.find(c => c.id === collectionId)
+  const invitations = get(collection, 'invitations') || []
+  return invitations.find(
+    i => i.userId === currentUser.id && i.role === 'reviewer' && !i.hasAnswer,
+  )
+}
+
 export const getCollectionReviewers = collectionId => dispatch => {
   dispatch(getReviewersRequest())
   return apiGet(`/collections/${collectionId}/invitations?role=reviewer`).then(
-- 
GitLab