From 65e50dd59e6e089c193debee0773ee0609da3456 Mon Sep 17 00:00:00 2001
From: Anca Ursachi <anca.ursachi@thinslices.com>
Date: Tue, 11 Dec 2018 16:44:44 +0200
Subject: [PATCH] feat(manuscriptCard): We can see a modal after we press the
 archive button on manuscript.

---
 .../src/ManuscriptCard.js                     | 99 ++++++++++++++-----
 .../src/components/Dashboard/Dashboard.js     |  5 +-
 .../components/Dashboard/DashboardItems.js    |  4 +
 .../src/components/Dashboard/DashboardPage.js | 27 ++++-
 4 files changed, 106 insertions(+), 29 deletions(-)

diff --git a/packages/component-faraday-ui/src/ManuscriptCard.js b/packages/component-faraday-ui/src/ManuscriptCard.js
index 17e9e1763..8710911d6 100644
--- a/packages/component-faraday-ui/src/ManuscriptCard.js
+++ b/packages/component-faraday-ui/src/ManuscriptCard.js
@@ -1,11 +1,12 @@
 import React, { Fragment } from 'react'
 import { get } from 'lodash'
+import { reduxForm } from 'redux-form'
 import styled from 'styled-components'
 import { th } from '@pubsweet/ui-toolkit'
 import { withJournal } from 'xpub-journal'
-import { H3, H4, DateParser } from '@pubsweet/ui'
+import { H3, H4, DateParser, ValidatedField, TextArea } from '@pubsweet/ui'
 import { compose, withHandlers, setDisplayName, withProps } from 'recompose'
-
+import { ItemOverrideAlert } from 'pubsweet-component-faraday-ui'
 import {
   Tag,
   Text,
@@ -22,9 +23,11 @@ import {
 import { OpenModal } from './modals'
 
 const ManuscriptCard = ({
+  isAdmin,
+  isDraft,
   onDelete,
-  canDelete,
   isFetching,
+  deleteManuscript,
   onCardClick,
   canViewReports,
   fragment = {},
@@ -84,28 +87,38 @@ const ManuscriptCard = ({
               <ReviewerBreakdown fragment={fragment} label="Reviewer Reports" />
             </Fragment>
           )}
-          {canDelete && (
-            <Item justify="flex-end" onClick={e => e.stopPropagation()}>
-              <OpenModal
-                confirmText="Delete"
-                isFetching={isFetching}
-                modalKey={`delete-${collId}`}
-                onConfirm={onDelete}
-                title="Are you sure you want to delete this submission?"
-              >
-                {showModal => (
-                  <ActionLink
-                    height={16}
-                    icon="trash"
-                    onClick={showModal}
-                    size="small"
-                  >
-                    Delete
-                  </ActionLink>
-                )}
-              </OpenModal>
-            </Item>
-          )}
+          {isAdmin &&
+            isDraft && (
+              <Item justify="flex-end" onClick={e => e.stopPropagation()}>
+                <OpenModal
+                  confirmText="Delete"
+                  isFetching={isFetching}
+                  modalKey={`delete-${collId}`}
+                  onConfirm={onDelete}
+                  title="Are you sure you want to delete this submission?"
+                >
+                  {showModal => (
+                    <ActionLink
+                      height={16}
+                      icon="trash"
+                      onClick={showModal}
+                      size="small"
+                    >
+                      Delete
+                    </ActionLink>
+                  )}
+                </OpenModal>
+              </Item>
+            )}
+          {isAdmin &&
+            !isDraft && (
+              <Item justify="flex-end" onClick={e => e.stopPropagation()}>
+                <FormModal
+                  collectionId={collId}
+                  deleteManuscript={deleteManuscript}
+                />
+              </Item>
+            )}
         </Row>
       </MainContainer>
       <SideNavigation>
@@ -114,6 +127,40 @@ const ManuscriptCard = ({
     </Root>
   )
 }
+const Enhanced = () => (
+  <Row>
+    <ItemOverrideAlert data-test-id="manuscript-return-reason" vertical>
+      <Label required>
+        Reason for removing the manuscript from the platform:
+      </Label>
+      <ValidatedField component={TextArea} name="comments" />
+    </ItemOverrideAlert>
+  </Row>
+)
+
+const FormModal = reduxForm({
+  form: 'deleteManuscript',
+  onSubmit: (
+    { comments },
+    dispatch,
+    { deleteManuscript, collectionId },
+  ) => modalProps => deleteManuscript({ collectionId, comments, modalProps }),
+})(({ collectionId, isFetching, handleSubmit }) => (
+  <OpenModal
+    confirmText="Delete"
+    content={Enhanced}
+    isFetching={isFetching}
+    modalKey={`delete-${collectionId}`}
+    onConfirm={modalProps => handleSubmit()(modalProps)}
+    title="Are you sure you want to remove this manuscript?"
+  >
+    {showModal => (
+      <ActionLink height={16} icon="trash" onClick={showModal} size="small">
+        Delete
+      </ActionLink>
+    )}
+  </OpenModal>
+))
 
 export default compose(
   withJournal,
@@ -131,7 +178,7 @@ export default compose(
       manuscriptType: get(journal, 'manuscriptTypes', []).find(
         t => t.value === get(metadata, 'type', ''),
       ),
-      canDelete: status === 'draft',
+      isDraft: status === 'draft',
     }),
   ),
   setDisplayName('ManuscriptCard'),
diff --git a/packages/components-faraday/src/components/Dashboard/Dashboard.js b/packages/components-faraday/src/components/Dashboard/Dashboard.js
index a03dce504..bd217eb5c 100644
--- a/packages/components-faraday/src/components/Dashboard/Dashboard.js
+++ b/packages/components-faraday/src/components/Dashboard/Dashboard.js
@@ -1,13 +1,14 @@
 import React, { Fragment } from 'react'
 import { compose, withProps } from 'recompose'
-
 import { DashboardItems, DashboardFilters } from './'
 
 const Dashboard = ({
   journal,
+  isAdmin,
   isFetching,
   dashboardItems,
   deleteCollection,
+  deleteManuscript,
   getFilterOptions,
   changeFilterValue,
   getDefaultFilterValue,
@@ -20,6 +21,8 @@ const Dashboard = ({
     />
     <DashboardItems
       deleteCollection={deleteCollection}
+      deleteManuscript={deleteManuscript}
+      isAdmin={isAdmin}
       isFetching={isFetching}
       list={dashboardItems}
     />
diff --git a/packages/components-faraday/src/components/Dashboard/DashboardItems.js b/packages/components-faraday/src/components/Dashboard/DashboardItems.js
index 72d16891c..5b9c40593 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardItems.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardItems.js
@@ -21,8 +21,10 @@ const DashboardItem = compose(
 const DashboardItems = ({
   list,
   onClick,
+  isAdmin,
   isFetching,
   canViewReports,
+  deleteManuscript,
   deleteCollection,
 }) => (
   <Root data-test-id="dashboard-list-items">
@@ -35,6 +37,8 @@ const DashboardItems = ({
         <HideLoading key={collection.id}>
           <DashboardItem
             collection={collection}
+            deleteManuscript={deleteManuscript}
+            isAdmin={isAdmin}
             isFetching={isFetching}
             key={collection.id}
             onClick={onClick}
diff --git a/packages/components-faraday/src/components/Dashboard/DashboardPage.js b/packages/components-faraday/src/components/Dashboard/DashboardPage.js
index 7f96042f9..b95adcac7 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardPage.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardPage.js
@@ -5,9 +5,9 @@ import { withJournal } from 'xpub-journal'
 import { ConnectPage } from 'xpub-connect'
 import { withRouter } from 'react-router-dom'
 import { selectCurrentUser } from 'xpub-selectors'
-import { compose, withHandlers, withContext } from 'recompose'
 import { handleError, withFetching } from 'pubsweet-component-faraday-ui'
-
+import { compose, withHandlers, withContext, shouldUpdate } from 'recompose'
+import { update } from 'pubsweet-client/src/helpers/api'
 import {
   getUserPermissions,
   newestFirstParseDashboard,
@@ -16,6 +16,9 @@ import {
 import { Dashboard } from './'
 import { priorityFilter, orderFilter, withFiltersHOC } from '../Filters'
 
+const deleteManuscript = ({ collectionId, ...body }) =>
+  update(`/collections/${collectionId}/status`, body)
+
 export default compose(
   ConnectPage(() => [
     actions.getCollections(),
@@ -35,6 +38,7 @@ export default compose(
         collections,
         currentUser,
         userPermissions,
+        isAdmin: state.currentUser.user.admin,
       }
     },
     {
@@ -42,6 +46,10 @@ export default compose(
       deleteCollection: actions.deleteCollection,
     },
   ),
+  shouldUpdate(
+    (props, nextProps) =>
+      props.collections.length !== nextProps.collections.length,
+  ),
   withRouter,
   withJournal,
   withFetching,
@@ -76,5 +84,20 @@ export default compose(
           handleError(setModalError)(err)
         })
     },
+    deleteManuscript: ({ setFetching }) => ({
+      modalProps: { setModalError, hideModal },
+      ...decision
+    }) => {
+      setFetching(true)
+      return deleteManuscript(decision)
+        .then(() => {
+          setFetching(false)
+          hideModal()
+        })
+        .catch(err => {
+          setFetching(false)
+          handleError(setModalError)(err)
+        })
+    },
   }),
 )(Dashboard)
-- 
GitLab