diff --git a/packages/component-wizard/src/components/AuthorList.js b/packages/component-wizard/src/components/AuthorList.js index 879dba67eb9f3cb945d9c3d3575df5c35e7e30e6..9c0f19d8bf42934891e9c2f3d93554762490a0cf 100644 --- a/packages/component-wizard/src/components/AuthorList.js +++ b/packages/component-wizard/src/components/AuthorList.js @@ -7,7 +7,14 @@ import { reduxForm } from 'redux-form' import { actions } from 'pubsweet-client' import { required } from 'xpub-validators' import { withRouter } from 'react-router-dom' -import { compose, withHandlers, getContext, lifecycle } from 'recompose' +import { selectCurrentUser } from 'xpub-selectors' +import { + compose, + withHandlers, + withProps, + getContext, + lifecycle, +} from 'recompose' import { TextField, Menu, Icon, ValidatedField, Button } from '@pubsweet/ui' import { addAuthor, getFragmentAuthors, setAuthors } from '../redux/authors' @@ -48,7 +55,22 @@ const MenuItem = ({ label, name, options }) => ( </div> ) -const AuthorAdder = ({ authors, handleSubmit, ...rest }) => ( +const Label = ({ label, value }) => ( + <div className={classnames(classes['label-container'])}> + <span className={classnames(classes.label)}>{label}</span> + <span className={classnames(classes.value)}>{value}</span> + </div> +) + +const DragHandle = () => ( + <div className={classnames(classes['drag-handle'])}> + <Icon>chevron_up</Icon> + <Icon size={16}>menu</Icon> + <Icon>chevron_down</Icon> + </div> +) + +const AuthorAdder = ({ authors, handleSubmit }) => ( <div className={classnames(classes.adder)}> <Button onClick={handleSubmit} primary> {authors.length === 0 ? '+ Add submitting author' : '+ Add author'} @@ -83,20 +105,41 @@ const AuthorAdder = ({ authors, handleSubmit, ...rest }) => ( </div> ) -const Label = ({ label, value }) => ( - <div className={classnames(classes['label-container'])}> - <span className={classnames(classes.label)}>{label}</span> - <span className={classnames(classes.value)}>{value}</span> - </div> -) - -const DragHandle = () => ( - <div className={classnames(classes['drag-handle'])}> - <Icon>chevron_up</Icon> - <Icon size={16}>menu</Icon> - <Icon>chevron_down</Icon> - </div> -) +const Adder = compose( + connect(state => ({ + currentUser: selectCurrentUser(state), + })), + withProps(({ currentUser }) => { + const { admin, email, username } = currentUser + if (!admin) { + return { + initialValues: { + author: { + email, + firstName: username, + }, + }, + } + } + }), + reduxForm({ + form: 'author', + onSubmit: (values, dispatch, { authors, addAuthor, reset, match }) => { + const collectionId = get(match, 'params.project') + const fragmentId = get(match, 'params.version') + const isFirstAuthor = authors.length === 0 + addAuthor( + { + ...values.author, + isSubmitting: isFirstAuthor, + isCorresponding: isFirstAuthor, + }, + collectionId, + fragmentId, + ).then(reset) + }, + }), +)(AuthorAdder) const Author = ({ firstName, @@ -163,27 +206,6 @@ const Author = ({ </div> ) -const Adder = compose( - reduxForm({ - form: 'author', - destroyOnUnmount: false, - onSubmit: (values, dispatch, { authors, addAuthor, reset, match }) => { - const collectionId = get(match, 'params.project') - const fragmentId = get(match, 'params.version') - const isFirstAuthor = authors.length === 0 - addAuthor( - { - ...values.author, - isSubmitting: isFirstAuthor, - isCorresponding: isFirstAuthor, - }, - collectionId, - fragmentId, - ).then(reset) - }, - })(AuthorAdder), -) - const Authors = ({ author, authors, diff --git a/packages/component-wizard/src/redux/authors.js b/packages/component-wizard/src/redux/authors.js index 2a7fb7c28d265541ce0c92cb75898eb35d52be8e..41d213512b9a2325aeaf5cb911b5a1db80d2179b 100644 --- a/packages/component-wizard/src/redux/authors.js +++ b/packages/component-wizard/src/redux/authors.js @@ -1,17 +1,23 @@ import { get } from 'lodash' import { actions } from 'pubsweet-client' import * as api from 'pubsweet-client/src/helpers/api' +import { change } from 'redux-form' // constants export const SET_AUTHORS = 'authors/SET_AUTHORS' -// actions -export const setAuthors = (authors, fragmentId) => ({ +const _setAuthors = (authors, fragmentId) => ({ type: SET_AUTHORS, authors, fragmentId, }) +// actions +export const setAuthors = (authors, fragmentId) => dispatch => { + dispatch(_setAuthors(authors, fragmentId)) + dispatch(change('wizard', 'authors', authors)) +} + 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 6de83e0e56f36c8eb57dc1f728a7c1729619c247..52adaeb0e650510b4187970a7fa6cbcec277f3be 100644 --- a/packages/xpub-faraday/app/config/journal/submit-wizard.js +++ b/packages/xpub-faraday/app/config/journal/submit-wizard.js @@ -105,6 +105,7 @@ export default { { fieldId: 'authors', renderComponent: AuthorList, + validate: [required], }, { fieldId: 'conflicts.hasConflicts',