From 2de114fba35edd535a805ee1ae9807beb8b93566 Mon Sep 17 00:00:00 2001
From: Alexandru Munteanu <alexandru.munt@gmail.com>
Date: Thu, 23 Aug 2018 20:49:24 +0300
Subject: [PATCH] feat(submission-flow): correctly set corresponding author

---
 .../component-faraday-ui/src/PreviewFile.md   |  4 ++-
 .../component-faraday-ui/src/WizardAuthors.js | 28 ++++++++++++++++---
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/packages/component-faraday-ui/src/PreviewFile.md b/packages/component-faraday-ui/src/PreviewFile.md
index 4bf363a9d..163e3ea0f 100644
--- a/packages/component-faraday-ui/src/PreviewFile.md
+++ b/packages/component-faraday-ui/src/PreviewFile.md
@@ -1,7 +1,9 @@
 Preview file in browser if possible
 
 ```js
-const file = {};
+const file = {
+  name: 'cocojambo.pdf'
+};
 
 <PreviewFile file={file} />
 ```
diff --git a/packages/component-faraday-ui/src/WizardAuthors.js b/packages/component-faraday-ui/src/WizardAuthors.js
index b9fd96d6e..fbebcdbb3 100644
--- a/packages/component-faraday-ui/src/WizardAuthors.js
+++ b/packages/component-faraday-ui/src/WizardAuthors.js
@@ -1,6 +1,6 @@
 import React, { Fragment } from 'react'
-import { omit } from 'lodash'
 import styled from 'styled-components'
+import { omit, isBoolean } from 'lodash'
 import { th } from '@pubsweet/ui-toolkit'
 import { compose, withState, withHandlers } from 'recompose'
 
@@ -15,6 +15,27 @@ import {
   SortableList,
 } from './'
 
+const castToBool = author => ({
+  ...author,
+  isCorresponding: isBoolean(author.isCorresponding) && author.isCorresponding,
+})
+
+const parseEditedAuthors = (editedAuthor, authors) => {
+  const newAuthor = castToBool(editedAuthor)
+
+  return authors.map(
+    a =>
+      a.id === newAuthor.id
+        ? newAuthor
+        : {
+            ...a,
+            isCorresponding: newAuthor.isCorresponding
+              ? false
+              : a.isCorresponding,
+          },
+  )
+}
+
 const WizardAuthors = ({
   error,
   moveAuthor,
@@ -94,9 +115,8 @@ export default compose(
       setFormAuthors([...authors.filter(a => a.id !== 'newAuthor'), author])
     },
     editExistingAuthor: ({ authors, setFormAuthors }) => editedAuthor => {
-      setFormAuthors(
-        authors.map(a => (a.id === editedAuthor.id ? editedAuthor : a)),
-      )
+      const newAuthors = parseEditedAuthors(editedAuthor, authors)
+      setFormAuthors(newAuthors)
     },
   }),
   withHandlers({
-- 
GitLab