From ba2edb43f8d1b354dd16b51b2010620c6612dafb Mon Sep 17 00:00:00 2001 From: Alexandru Munteanu <alexandru.munt@gmail.com> Date: Thu, 14 Jun 2018 18:08:03 +0300 Subject: [PATCH] refactor(author-editor): fix checkbox as corresponding flicker --- .../src/components/AuthorList/AuthorEditor.js | 29 ++++--------------- .../src/components/AuthorList/AuthorList.js | 10 +------ .../src/components/AuthorList/StaticList.js | 3 +- .../src/components/AuthorList/utils.js | 23 +++++++++++++++ 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js index 5b6e09a19..185296d68 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js @@ -3,20 +3,19 @@ import { pick } from 'lodash' import { connect } from 'react-redux' import styled, { css } from 'styled-components' import { Icon, Checkbox, th } from '@pubsweet/ui' -import { compose, withHandlers, withProps } from 'recompose' +import { compose, withProps } from 'recompose' import { reduxForm, Field, change as changeForm } from 'redux-form' import { Spinner } from '../UIComponents' import { - editAuthor, authorSuccess, authorFailure, getAuthorFetching, } from '../../redux/authors' import { ValidatedTextField, Label } from './FormItems' -import { authorKeys } from './utils' +import { authorKeys, parseEditedAuthors } from './utils' const renderCheckbox = ({ input }) => ( <Checkbox checked={input.value} type="checkbox" {...input} /> @@ -32,7 +31,6 @@ const AuthorEdit = ({ setAuthorEdit, parseAuthorType, isCorresponding, - changeCorresponding, }) => ( <Root> <Header> @@ -40,11 +38,7 @@ const AuthorEdit = ({ <span>{parseAuthorType(isSubmitting, isCorresponding, index)}</span> {!isSubmitting && ( <Fragment> - <Field - component={renderCheckbox} - name="edit.isCorresponding" - onChange={changeCorresponding(id)} - /> + <Field component={renderCheckbox} name="edit.isCorresponding" /> <label>Corresponding</label> </Fragment> )} @@ -89,32 +83,21 @@ export default compose( state => ({ isFetching: getAuthorFetching(state), }), - { changeForm, authorSuccess, authorFailure, editAuthor }, + { changeForm, authorSuccess, authorFailure }, ), withProps(props => ({ initialValues: { edit: pick(props, authorKeys), }, })), - withHandlers({ - changeCorresponding: ({ changeForm, setAsCorresponding }) => id => ( - evt, - newValue, - ) => { - setAsCorresponding(id)() - changeForm('edit', 'edit.isCorresponding', newValue) - }, - }), reduxForm({ form: 'edit', onSubmit: ( { edit: newAuthor }, dispatch, - { version: { authors = [] }, changeForm, setAuthorEdit }, + { authors, changeForm, setAuthorEdit }, ) => { - const newAuthors = authors.map( - a => (a.id === newAuthor.id ? newAuthor : a), - ) + const newAuthors = parseEditedAuthors(newAuthor, authors) changeForm('wizard', 'authors', newAuthors) setAuthorEdit(-1)() }, diff --git a/packages/components-faraday/src/components/AuthorList/AuthorList.js b/packages/components-faraday/src/components/AuthorList/AuthorList.js index 5045a0615..5fd5b2708 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorList.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorList.js @@ -24,7 +24,7 @@ import { } from '../../redux/authors' import { DragHandle } from './FormItems' -import { Author, StaticList, AuthorAdder, AuthorEditor, utils } from './' +import { Author, StaticList, AuthorAdder, AuthorEditor } from './' const wizardSelector = formValueSelector('wizard') @@ -136,14 +136,6 @@ export default compose( setFormAuthors(newAuthors) }) }, - setAsCorresponding: ({ - authors, - setAuthors, - setFormAuthors, - }) => id => () => { - const newAuthors = authors.map(utils.setCorresponding(id)) - setFormAuthors(newAuthors) - }, }), setDisplayName('AuthorList'), )(Authors) diff --git a/packages/components-faraday/src/components/AuthorList/StaticList.js b/packages/components-faraday/src/components/AuthorList/StaticList.js index 79d6d2fec..f6f82fc5b 100644 --- a/packages/components-faraday/src/components/AuthorList/StaticList.js +++ b/packages/components-faraday/src/components/AuthorList/StaticList.js @@ -12,7 +12,6 @@ export default ({ setAuthorEdit, setFormAuthors, parseAuthorType, - setAsCorresponding, ...rest }) => ( <div> @@ -25,10 +24,10 @@ export default ({ initialValues: { edit: author, }, + authors, setAuthors: setFormAuthors, setAuthorEdit, parseAuthorType, - setAsCorresponding, project, version, ...author, diff --git a/packages/components-faraday/src/components/AuthorList/utils.js b/packages/components-faraday/src/components/AuthorList/utils.js index ea68ddb2b..e58c4abec 100644 --- a/packages/components-faraday/src/components/AuthorList/utils.js +++ b/packages/components-faraday/src/components/AuthorList/utils.js @@ -1,3 +1,5 @@ +import { isBoolean } from 'lodash' + export const authorKeys = [ 'id', 'email', @@ -15,3 +17,24 @@ export const setCorresponding = id => author => isCorresponding: true, } : { ...author, isCorresponding: false } + +export const castToBool = author => ({ + ...author, + isCorresponding: isBoolean(author.isCorresponding) && author.isCorresponding, +}) + +export const parseEditedAuthors = (editedAuthor, authors) => { + const newAuthor = castToBool(editedAuthor) + + return authors.map( + a => + a.id === newAuthor.id + ? newAuthor + : { + ...a, + isCorresponding: newAuthor.isCorresponding + ? false + : a.isCorresponding, + }, + ) +} -- GitLab