From 68ee50b8c968db1ecf33e9be7cdda4331143347f Mon Sep 17 00:00:00 2001
From: Bogdan Cochior <bogdan.cochior@thinslices.com>
Date: Fri, 9 Mar 2018 15:07:00 +0200
Subject: [PATCH] feat(dashboard): show confirmation modal on delete

---
 .../src/components/ConfirmationModal.js       | 82 +++++++++++++++++++
 .../component-modal/src/components/index.js   |  1 +
 .../src/components/withModal.js               |  2 +-
 .../src/components/Dashboard/DashboardCard.js | 37 +++++++--
 4 files changed, 116 insertions(+), 6 deletions(-)
 create mode 100644 packages/component-modal/src/components/ConfirmationModal.js

diff --git a/packages/component-modal/src/components/ConfirmationModal.js b/packages/component-modal/src/components/ConfirmationModal.js
new file mode 100644
index 000000000..5068762db
--- /dev/null
+++ b/packages/component-modal/src/components/ConfirmationModal.js
@@ -0,0 +1,82 @@
+import React from 'react'
+import { Icon, Button } from '@pubsweet/ui'
+import styled, { css, withTheme } from 'styled-components'
+
+const ConfirmationModal = ({
+  title,
+  subtitle,
+  content,
+  onConfirm,
+  confirmText = 'OK',
+  cancelText = 'Cancel',
+  hideModal,
+  theme,
+}) => (
+  <Root>
+    <CloseIcon onClick={hideModal}>
+      <Icon color={theme.colorPrimary}>x</Icon>
+    </CloseIcon>
+    {title && <Title dangerouslySetInnerHTML={{ __html: title }} />}
+    {subtitle && <Subtitle dangerouslySetInnerHTML={{ __html: subtitle }} />}
+    {content && <Content dangerouslySetInnerHTML={{ __html: content }} />}
+    <ButtonsContainer>
+      <Button onClick={hideModal}>{cancelText}</Button>
+      <Button onClick={onConfirm} primary>
+        {confirmText}
+      </Button>
+    </ButtonsContainer>
+  </Root>
+)
+
+export default withTheme(ConfirmationModal)
+
+// #region styled-components
+const defaultText = css`
+  color: ${({ theme }) => theme.colorText};
+  font-family: ${({ theme }) => theme.fontReading};
+  font-size: ${({ theme }) => theme.fontSizeBaseSmall};
+`
+
+const Root = styled.div`
+  background-color: ${({ theme }) => theme.backgroundColor};
+  padding: 50px 32px 32px 32px;
+  border: ${({ theme }) => theme.borderDefault};
+  position: relative;
+  width: 600px;
+  max-height: 500px;
+  overflow-y: scroll;
+`
+
+const Title = styled.div`
+  ${defaultText};
+  font-size: ${({ theme }) => theme.fontSizeBase};
+  text-align: center;
+  margin-bottom: 20px;
+`
+
+const Subtitle = styled.div`
+  ${defaultText};
+  font-weight: bold;
+  line-height: 1.57;
+  margin-bottom: 15px;
+  text-align: center;
+`
+
+const Content = styled.div`
+  ${defaultText};
+  line-height: 1.57;
+  margin-top: 10px;
+  text-align: left;
+`
+const ButtonsContainer = styled.div`
+  display: flex;
+  justify-content: space-evenly;
+  margin: 30px auto 0;
+`
+const CloseIcon = styled.div`
+  cursor: pointer;
+  position: absolute;
+  top: 5px;
+  right: 5px;
+`
+// #endregion
diff --git a/packages/component-modal/src/components/index.js b/packages/component-modal/src/components/index.js
index fca40a67d..22f1e90fc 100644
--- a/packages/component-modal/src/components/index.js
+++ b/packages/component-modal/src/components/index.js
@@ -1 +1,2 @@
 export { default as withModal } from './withModal'
+export { default as ConfirmationModal } from './ConfirmationModal'
diff --git a/packages/component-modal/src/components/withModal.js b/packages/component-modal/src/components/withModal.js
index 4506ade0c..1fb9a2261 100644
--- a/packages/component-modal/src/components/withModal.js
+++ b/packages/component-modal/src/components/withModal.js
@@ -30,7 +30,7 @@ const withModal = ({
             overlayColor={overlayColor}
           />
         )}
-        <WrappedComponent {...rest} />
+        <WrappedComponent hideModal={hideModal} {...rest} />
       </div>
     ),
   )
diff --git a/packages/components-faraday/src/components/Dashboard/DashboardCard.js b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
index a95e649f2..b5e6db6fd 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardCard.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardCard.js
@@ -3,7 +3,11 @@ import { get } from 'lodash'
 import PropTypes from 'prop-types'
 import { Button, Icon } from '@pubsweet/ui'
 import styled, { css } from 'styled-components'
-import { compose, getContext } from 'recompose'
+import { compose, getContext, withHandlers } from 'recompose'
+import {
+  withModal,
+  ConfirmationModal,
+} from 'pubsweet-component-modal/src/components'
 
 import { parseVersion, parseJournalIssue } from './utils'
 
@@ -16,6 +20,7 @@ const DashboardCard = ({
   version,
   showAbstractModal,
   journal,
+  cancelSubmission,
   ...rest
 }) => {
   const { submitted, title, type } = parseVersion(version)
@@ -77,9 +82,7 @@ const DashboardCard = ({
                 <Icon color="#667080">chevron-right</Icon>
               </Details>
             ) : (
-              <Details onClick={() => deleteProject(project)}>
-                Cancel submission
-              </Details>
+              <Details onClick={cancelSubmission}>Cancel submission</Details>
             )}
           </RightDetails>
         </Bottom>
@@ -120,7 +123,31 @@ const DashboardCard = ({
   ) : null
 }
 
-export default compose(getContext({ journal: PropTypes.object }))(DashboardCard)
+export default compose(
+  getContext({ journal: PropTypes.object }),
+  withModal({
+    modalComponent: ConfirmationModal,
+  }),
+  withHandlers({
+    cancelSubmission: ({
+      showModal,
+      deleteProject,
+      project,
+      hideModal,
+    }) => () => {
+      const modalConfig = {
+        onConfirm: () => {
+          deleteProject(project)
+          hideModal()
+        },
+        dismissable: false,
+        title: 'Are you sure you want to delete the manuscript?',
+        subtitle: 'This operation cannot be undone!',
+      }
+      showModal(modalConfig)
+    },
+  }),
+)(DashboardCard)
 
 // #region styled-components
 const defaultText = css`
-- 
GitLab