From c2d9daad57a3025d50cc74fbe2f389bbc83286b5 Mon Sep 17 00:00:00 2001
From: Alexandru Munteanu <alexandru.munt@gmail.com>
Date: Tue, 5 Jun 2018 17:32:32 +0300
Subject: [PATCH] fix(manuscript-details): redirect on forbidden

---
 .../src/components/ManuscriptLayout.js        |  8 +-----
 .../src/components/ManuscriptPage.js          | 25 +++++++++++--------
 .../src/components/ReviewsAndReports.js       | 12 ++++-----
 packages/components-faraday/src/index.js      |  3 ++-
 .../components-faraday/src/redux/errors.js    | 17 +++++++++++++
 .../components-faraday/src/redux/index.js     |  3 ++-
 6 files changed, 43 insertions(+), 25 deletions(-)
 create mode 100644 packages/components-faraday/src/redux/errors.js

diff --git a/packages/component-manuscript/src/components/ManuscriptLayout.js b/packages/component-manuscript/src/components/ManuscriptLayout.js
index ad31c596c..ea6b9532e 100644
--- a/packages/component-manuscript/src/components/ManuscriptLayout.js
+++ b/packages/component-manuscript/src/components/ManuscriptLayout.js
@@ -1,6 +1,5 @@
 import React, { Fragment } from 'react'
 import { isEmpty } from 'lodash'
-import { Redirect } from 'react-router-dom'
 
 import {
   Root,
@@ -27,16 +26,13 @@ const ManuscriptLayout = ({
   history,
   currentUser,
   editorInChief,
-  pubsweetError,
   updateManuscript,
-  canSeeReviewersReports,
   canSeeEditorialComments,
   editorialRecommendations,
   project = {},
   version = {},
 }) => (
   <Root>
-    {pubsweetError && <Redirect to="not-found" />}
     {!isEmpty(project) && !isEmpty(version) ? (
       <Fragment>
         <Container flex={3}>
@@ -58,9 +54,7 @@ const ManuscriptLayout = ({
             version={version}
           />
           <ManuscriptDetails collection={project} fragment={version} />
-          {canSeeReviewersReports && (
-            <ReviewsAndReports project={project} version={version} />
-          )}
+          <ReviewsAndReports project={project} version={version} />
           {canSeeEditorialComments &&
             editorialRecommendations.length > 0 && (
               <EditorialComments
diff --git a/packages/component-manuscript/src/components/ManuscriptPage.js b/packages/component-manuscript/src/components/ManuscriptPage.js
index 6825aef0a..df4b1eaac 100644
--- a/packages/component-manuscript/src/components/ManuscriptPage.js
+++ b/packages/component-manuscript/src/components/ManuscriptPage.js
@@ -20,6 +20,10 @@ import {
 } 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,
@@ -28,11 +32,7 @@ import {
 
 import ManuscriptLayout from './ManuscriptLayout'
 import { parseSearchParams, redirectToError } from './utils'
-import {
-  getPubsweetError,
-  canSeeReviewersReports,
-  canSeeEditorialComments,
-} from '../../../component-faraday-selectors'
+import { canSeeEditorialComments } from '../../../component-faraday-selectors'
 
 export default compose(
   setDisplayName('ManuscriptPage'),
@@ -49,15 +49,11 @@ export default compose(
   connect(
     (state, { match }) => ({
       currentUser: selectCurrentUser(state),
-      pubsweetError: getPubsweetError(state),
       handlingEditors: selectHandlingEditors(state),
+      hasManuscriptFailure: hasManuscriptFailure(state),
       version: selectFragment(state, match.params.version),
       project: selectCollection(state, match.params.project),
       editorialRecommendations: selectEditorialRecommendations(state),
-      canSeeReviewersReports: canSeeReviewersReports(
-        state,
-        match.params.project,
-      ),
       canSeeEditorialComments: canSeeEditorialComments(
         state,
         match.params.project,
@@ -66,6 +62,7 @@ export default compose(
     {
       replace,
       getSignedUrl,
+      clearCustomError,
       reviewerDecision,
       getCollection: actions.getCollection,
       updateVersion: actions.updateFragment,
@@ -100,11 +97,19 @@ export default compose(
       const {
         match,
         replace,
+        history,
         location,
         getCollection,
         reviewerDecision,
         setEditorInChief,
+        clearCustomError,
+        hasManuscriptFailure,
       } = this.props
+      if (hasManuscriptFailure) {
+        history.push('/not-found')
+        clearCustomError()
+      }
+
       const collectionId = match.params.project
       const { agree, invitationId } = parseSearchParams(location.search)
       if (agree === 'true') {
diff --git a/packages/component-manuscript/src/components/ReviewsAndReports.js b/packages/component-manuscript/src/components/ReviewsAndReports.js
index 133f314ff..b498bf92a 100644
--- a/packages/component-manuscript/src/components/ReviewsAndReports.js
+++ b/packages/component-manuscript/src/components/ReviewsAndReports.js
@@ -16,7 +16,7 @@ import { selectRecommendations } from 'pubsweet-components-faraday/src/redux/rec
 
 import { Tabs, Expandable } from '../molecules'
 import { ReviewReportCard, ReviewerReportForm } from './'
-import { currentUserIs } from '../../../component-faraday-selectors'
+import { canSeeReviewersReports } from '../../../component-faraday-selectors'
 
 const getTabSections = (collectionId, reviewers, recommendations = []) => [
   {
@@ -47,16 +47,16 @@ const ReviewsAndReports = ({
   report,
   project,
   version,
-  isStaff,
   isReviewer,
   mappedReviewers,
   mappedRecommendations,
+  canSeeReviewersReports,
   review = {},
   reviewers = [],
   recommendations = [],
 }) => (
   <Fragment>
-    {isStaff && (
+    {canSeeReviewersReports && (
       <Root>
         <Expandable
           label="Reviewers & Reports"
@@ -102,10 +102,10 @@ export default compose(
   connect(
     (state, { project }) => ({
       reviewers: selectReviewers(state),
-      isStaff: currentUserIs(state, 'staff'),
       recommendations: selectRecommendations(state),
       fetchingReviewers: selectFetchingReviewers(state),
       isReviewer: currentUserIsReviewer(state, project.id),
+      canSeeReviewersReports: canSeeReviewersReports(state, project.id),
     }),
     { getCollectionReviewers },
   ),
@@ -130,8 +130,8 @@ export default compose(
   })),
   lifecycle({
     componentDidMount() {
-      const { getReviewers } = this.props
-      getReviewers()
+      const { getReviewers, canSeeReviewersReports } = this.props
+      canSeeReviewersReports && getReviewers()
     },
   }),
 )(ReviewsAndReports)
diff --git a/packages/components-faraday/src/index.js b/packages/components-faraday/src/index.js
index d21969e7b..0eb9c3b47 100644
--- a/packages/components-faraday/src/index.js
+++ b/packages/components-faraday/src/index.js
@@ -3,8 +3,9 @@ module.exports = {
     components: [() => require('./components')],
     reducers: {
       authors: () => require('./redux/authors').default,
-      files: () => require('./redux/files').default,
+      customError: () => require('./redux/errors').default,
       editors: () => require('./redux/editors').default,
+      files: () => require('./redux/files').default,
       reviewers: () => require('./redux/reviewers').default,
       recommendations: () => require('./redux/recommendations').default,
     },
diff --git a/packages/components-faraday/src/redux/errors.js b/packages/components-faraday/src/redux/errors.js
new file mode 100644
index 000000000..00a4efa1c
--- /dev/null
+++ b/packages/components-faraday/src/redux/errors.js
@@ -0,0 +1,17 @@
+export const hasManuscriptFailure = state =>
+  state.customError === 'manuscriptFailure'
+
+export const clearCustomError = () => ({
+  type: 'CLEAR_CUSTOM_ERROR',
+})
+
+export default (state = '', action) => {
+  switch (action.type) {
+    case 'CLEAR_CUSTOM_ERROR':
+      return ''
+    case 'GET_FRAGMENT_FAILURE':
+      return 'manuscriptFailure'
+    default:
+      return state
+  }
+}
diff --git a/packages/components-faraday/src/redux/index.js b/packages/components-faraday/src/redux/index.js
index 12a9544f0..9e0c549f2 100644
--- a/packages/components-faraday/src/redux/index.js
+++ b/packages/components-faraday/src/redux/index.js
@@ -1,5 +1,6 @@
+export { default as files } from './files'
+export { default as errors } from './errors'
 export { default as authors } from './authors'
 export { default as editors } from './editors'
-export { default as files } from './files'
 export { default as reviewers } from './reviewers'
 export { default as recommendations } from './recommendations'
-- 
GitLab