From 2dd68e22318c89f3c2ef7f18d20f98d9446d6b00 Mon Sep 17 00:00:00 2001
From: Bogdan Cochior <bogdan.cochior@thinslices.com>
Date: Mon, 22 Jan 2018 11:10:25 +0200
Subject: [PATCH] Fix multiple requests on Author details form

---
 .../src/components/AuthorList.js              | 38 ++++++++++---------
 .../component-wizard/src/redux/authors.js     |  4 ++
 .../app/config/journal/submit-wizard.js       | 12 +++---
 .../app/config/journal/wizard-validators.js   | 16 ++++----
 4 files changed, 38 insertions(+), 32 deletions(-)

diff --git a/packages/component-wizard/src/components/AuthorList.js b/packages/component-wizard/src/components/AuthorList.js
index 9c0f19d8b..6959fc604 100644
--- a/packages/component-wizard/src/components/AuthorList.js
+++ b/packages/component-wizard/src/components/AuthorList.js
@@ -17,7 +17,12 @@ import {
 } from 'recompose'
 import { TextField, Menu, Icon, ValidatedField, Button } from '@pubsweet/ui'
 
-import { addAuthor, getFragmentAuthors, setAuthors } from '../redux/authors'
+import {
+  addAuthor,
+  getFragmentAuthors,
+  setAuthors,
+  moveAuthors,
+} from '../redux/authors'
 
 import classes from './AuthorList.local.scss'
 import SortableList from './SortableList'
@@ -246,6 +251,7 @@ export default compose(
     {
       addAuthor,
       setAuthors,
+      moveAuthors,
       updateFragment: actions.updateFragment,
     },
   ),
@@ -256,13 +262,15 @@ export default compose(
     },
   }),
   withHandlers({
-    dropItem: ({ updateFragment, authors, project, version }) =>
-      debounce(() => {
-        updateFragment(project, {
-          ...version,
-          authors,
-        })
-      }, 500),
+    dropItem: ({
+      updateFragment,
+      authors,
+      project,
+      version,
+      setAuthors,
+    }) => () => {
+      setAuthors(authors, version.id)
+    },
     countryParser: () => countryCode =>
       countries.find(c => c.value === countryCode).label,
     parseAuthorType: () => (isSubmitting, isCorresponding) => {
@@ -272,14 +280,14 @@ export default compose(
     },
     moveAuthor: ({
       authors,
-      setAuthors,
+      moveAuthors,
       project,
       version,
       updateFragment,
       match: { params },
     }) => (dragIndex, hoverIndex) => {
       const newAuthors = SortableList.moveItem(authors, dragIndex, hoverIndex)
-      setAuthors(newAuthors, params.version)
+      moveAuthors(newAuthors, params.version)
     },
     removeAuthor: ({
       authors,
@@ -289,10 +297,7 @@ export default compose(
       setAuthors,
     }) => authorEmail => () => {
       const newAuthors = authors.filter(a => a.email !== authorEmail)
-      updateFragment(project, {
-        ...version,
-        authors: newAuthors,
-      }).then(() => setAuthors(newAuthors, version.id))
+      setAuthors(newAuthors, version.id)
     },
     setAsCorresponding: ({
       authors,
@@ -305,10 +310,7 @@ export default compose(
         ...a,
         isCorresponding: a.isSubmitting || a.email === authorEmail,
       }))
-      updateFragment(project, {
-        ...version,
-        authors: newAuthors,
-      }).then(() => setAuthors(newAuthors, version.id))
+      setAuthors(newAuthors, version.id)
     },
   }),
 )(Authors)
diff --git a/packages/component-wizard/src/redux/authors.js b/packages/component-wizard/src/redux/authors.js
index 41d213512..18440cfd0 100644
--- a/packages/component-wizard/src/redux/authors.js
+++ b/packages/component-wizard/src/redux/authors.js
@@ -18,6 +18,10 @@ export const setAuthors = (authors, fragmentId) => dispatch => {
   dispatch(change('wizard', 'authors', authors))
 }
 
+export const moveAuthors = (authors, fragmentId) => dispatch => {
+  dispatch(_setAuthors(authors, fragmentId))
+}
+
 export const addAuthor = (author, collectionId, fragmentId) => dispatch =>
   api
     .create(`/fragments/${fragmentId}/authors`, author)
diff --git a/packages/xpub-faraday/app/config/journal/submit-wizard.js b/packages/xpub-faraday/app/config/journal/submit-wizard.js
index 3d48e222a..23eb82399 100644
--- a/packages/xpub-faraday/app/config/journal/submit-wizard.js
+++ b/packages/xpub-faraday/app/config/journal/submit-wizard.js
@@ -15,7 +15,7 @@ import { declarations } from './'
 import issueTypes from './issues-types'
 import manuscriptTypes from './manuscript-types'
 
-import { requiredBasedOnType, parseAbstract } from './wizard-validators'
+import { requiredBasedOnType } from './wizard-validators'
 
 const min3Chars = minChars(3)
 const declarationsMinSize = minSize(declarations.options.length)
@@ -51,7 +51,7 @@ export default {
   steps: [
     {
       label: 'Journal details',
-      title: 'Journal & Field Selection',
+      title: '1. Journal & Field Selection',
       children: [
         {
           fieldId: 'label-Journal',
@@ -80,7 +80,7 @@ export default {
     },
     {
       label: 'Pre-submission checklist',
-      title: 'Pre-submission Checklist',
+      title: '2. Pre-submission Checklist',
       subtitle:
         'Before moving forward make sure you have all the needed details prepared by reviewing and checking off the items on this list.',
       children: [
@@ -94,7 +94,7 @@ export default {
     },
     {
       label: 'Manuscript & Authors Details',
-      title: 'Manuscript & Authors Details',
+      title: '3. Manuscript & Authors Details',
       subtitle:
         'Please provide the details of all the authors of this manuscript....',
       children: [
@@ -128,7 +128,7 @@ export default {
           renderComponent: AbstractEditor,
           title: 'Abstract',
           placeholder: 'Write an abstract',
-          validate: [requiredBasedOnType, parseAbstract],
+          validate: [requiredBasedOnType],
         },
         {
           fieldId: 'spacing-abstract',
@@ -163,7 +163,7 @@ export default {
     },
     {
       label: 'Files upload',
-      title: 'Manuscript Files Upload',
+      title: '4. Manuscript Files Upload',
       children: [
         {
           fieldId: 'label-manuscript',
diff --git a/packages/xpub-faraday/app/config/journal/wizard-validators.js b/packages/xpub-faraday/app/config/journal/wizard-validators.js
index 743578994..59ea36b4c 100644
--- a/packages/xpub-faraday/app/config/journal/wizard-validators.js
+++ b/packages/xpub-faraday/app/config/journal/wizard-validators.js
@@ -6,19 +6,19 @@ const requiredTypes = manuscriptTypes
   .filter(t => t.abstractRequired)
   .map(t => t.value)
 
+const parseAbstract = value => {
+  if (value && value.replace('<p></p>', '').replace('<h1></h1>', '')) {
+    return undefined
+  }
+  return 'Required'
+}
+
 export const requiredBasedOnType = (value, formValues) => {
   if (
     requiredTypes.includes(get(formValues, 'metadata.type')) &&
-    isEmpty(get(formValues, 'metadata.abstract'))
+    (isEmpty(get(formValues, 'metadata.abstract')) || parseAbstract(value))
   ) {
     return 'Required'
   }
   return undefined
 }
-
-export const parseAbstract = value => {
-  if (value && value.replace('<p></p>', '').replace('<h1></h1>', '')) {
-    return undefined
-  }
-  return 'Required'
-}
-- 
GitLab