From dd9d4c281c1e2baa0f3488407ef93e134dfb9de5 Mon Sep 17 00:00:00 2001
From: Alexandru Munteanu <alexandru.munteanu@thinslices.com>
Date: Thu, 22 Feb 2018 11:37:37 +0200
Subject: [PATCH] fix(author-list): fix isCorresponding checkbox

---
 .../src/components/SubmitPage.js              |  2 -
 .../src/components/AuthorList/AuthorEditor.js | 58 +++++++++++++++----
 .../src/components/AuthorList/AuthorList.js   | 13 +++--
 .../src/components/Files/FileSection.js       |  6 +-
 .../app/config/journal/submit-wizard.js       |  2 +-
 5 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/packages/component-submit/src/components/SubmitPage.js b/packages/component-submit/src/components/SubmitPage.js
index 9b75840b6..05ea2453e 100644
--- a/packages/component-submit/src/components/SubmitPage.js
+++ b/packages/component-submit/src/components/SubmitPage.js
@@ -9,8 +9,6 @@ import { selectCollection, selectFragment } from 'xpub-selectors'
 import Submit from './Submit'
 
 const onSubmit = (values, dispatch, { history, project, version }) =>
-  // console.log('submit', values)
-
   dispatch(
     actions.updateFragment(project, {
       id: version.id,
diff --git a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
index 558002b41..0c8807d06 100644
--- a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
+++ b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
@@ -1,9 +1,10 @@
-import React from 'react'
-import { compose } from 'recompose'
+import React, { Fragment } from 'react'
+import { pick } from 'lodash'
 import { Icon } from '@pubsweet/ui'
 import { connect } from 'react-redux'
 import styled from 'styled-components'
-import { reduxForm } from 'redux-form'
+import { compose, withHandlers, withProps } from 'recompose'
+import { reduxForm, Field, change as changeForm } from 'redux-form'
 
 import countries from './countries'
 import { Spinner } from '../UIComponents'
@@ -15,6 +16,10 @@ const emailRegex = new RegExp(/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/)
 const emailValidator = value =>
   emailRegex.test(value) ? undefined : 'Invalid email'
 
+const renderCheckbox = ({ input }) => (
+  <input checked={input.value} type="checkbox" {...input} />
+)
+
 const AuthorEdit = ({
   isFetching,
   setAuthorEdit,
@@ -23,19 +28,23 @@ const AuthorEdit = ({
   index,
   isSubmitting,
   isCorresponding,
-  setAsCorresponding,
   email,
+  changeCorresponding,
 }) => (
   <Root>
     <Header>
       <TitleContainer>
         <span>{parseAuthorType(isSubmitting, isCorresponding, index)}</span>
-        <input
-          checked={isCorresponding}
-          onChange={setAsCorresponding(email)}
-          type="checkbox"
-        />
-        <label>Corresponding</label>
+        {!isSubmitting && (
+          <Fragment>
+            <Field
+              component={renderCheckbox}
+              name="edit.isCorresponding"
+              onChange={changeCorresponding(email)}
+            />
+            <label>Corresponding</label>
+          </Fragment>
+        )}
       </TitleContainer>
 
       <ButtonsContainer>
@@ -76,9 +85,34 @@ const AuthorEdit = ({
 )
 
 export default compose(
-  connect(state => ({
-    isFetching: getAuthorFetching(state),
+  connect(
+    state => ({
+      isFetching: getAuthorFetching(state),
+    }),
+    { changeForm },
+  ),
+  withProps(props => ({
+    initialValues: {
+      edit: pick(props, [
+        'isCorresponding',
+        'firstName',
+        'lastName',
+        'middleName',
+        'email',
+        'affiliation',
+        'country',
+      ]),
+    },
   })),
+  withHandlers({
+    changeCorresponding: ({ changeForm, setAsCorresponding }) => email => (
+      evt,
+      newValue,
+    ) => {
+      setAsCorresponding(email)()
+      changeForm('edit', 'edit.isCorresponding', newValue)
+    },
+  }),
   reduxForm({
     form: 'edit',
     onSubmit: (
diff --git a/packages/components-faraday/src/components/AuthorList/AuthorList.js b/packages/components-faraday/src/components/AuthorList/AuthorList.js
index cb70a7af0..76d504e84 100644
--- a/packages/components-faraday/src/components/AuthorList/AuthorList.js
+++ b/packages/components-faraday/src/components/AuthorList/AuthorList.js
@@ -124,10 +124,15 @@ export default compose(
       setFormAuthors(newAuthors)
     },
     setAsCorresponding: ({ authors, setFormAuthors }) => authorEmail => () => {
-      const newAuthors = authors.map(a => ({
-        ...a,
-        isCorresponding: a.isSubmitting || a.email === authorEmail,
-      }))
+      const newAuthors = authors.map(
+        a =>
+          a.email === authorEmail
+            ? {
+                ...a,
+                isCorresponding: !a.isCorresponding,
+              }
+            : { ...a, isCorresponding: false },
+      )
       setFormAuthors(newAuthors)
     },
   }),
diff --git a/packages/components-faraday/src/components/Files/FileSection.js b/packages/components-faraday/src/components/Files/FileSection.js
index b885046f2..0e112c42a 100644
--- a/packages/components-faraday/src/components/Files/FileSection.js
+++ b/packages/components-faraday/src/components/Files/FileSection.js
@@ -174,11 +174,11 @@ export default compose(
 
         if (
           allowedFileExtensions &&
-          allowedFileExtensions.includes(fileExtention)
+          !allowedFileExtensions.includes(fileExtention)
         ) {
-          addFile(file)
-        } else {
           setError('Invalid file type.')
+        } else {
+          addFile(file)
         }
       },
     },
diff --git a/packages/xpub-faraday/app/config/journal/submit-wizard.js b/packages/xpub-faraday/app/config/journal/submit-wizard.js
index 82d222d3c..3a63214bb 100644
--- a/packages/xpub-faraday/app/config/journal/submit-wizard.js
+++ b/packages/xpub-faraday/app/config/journal/submit-wizard.js
@@ -92,7 +92,7 @@ export default {
           fieldId: 'declarations',
           renderComponent: CheckboxGroup,
           options: declarations.options,
-          // validate: [required, declarationsMinSize],
+          validate: [required, declarationsMinSize],
         },
       ],
     },
-- 
GitLab