From 4272161941b3f590dfdcdee21cf44b8facfbb227 Mon Sep 17 00:00:00 2001 From: Bogdan Cochior <bogdan.cochior@thinslices.com> Date: Wed, 4 Apr 2018 13:23:56 +0300 Subject: [PATCH] refactor(authors): wip: move adding authors to new endpoints --- .../component-wizard/src/components/Wizard.js | 2 +- .../src/components/AuthorList/Author.js | 5 -- .../src/components/AuthorList/AuthorAdder.js | 50 ++++++++++++------- .../src/components/AuthorList/AuthorEditor.js | 39 +++++++++------ .../src/components/AuthorList/AuthorList.js | 34 ++++++++----- .../src/components/AuthorList/StaticList.js | 6 +-- .../components-faraday/src/redux/authors.js | 9 ++-- 7 files changed, 84 insertions(+), 61 deletions(-) diff --git a/packages/component-wizard/src/components/Wizard.js b/packages/component-wizard/src/components/Wizard.js index 3ef3ff583..6b3444159 100644 --- a/packages/component-wizard/src/components/Wizard.js +++ b/packages/component-wizard/src/components/Wizard.js @@ -13,7 +13,7 @@ export default ({ step, project, }) => ( - <Root data-test={`submission-${project.customId}`}> + <Root data-test-submission={project.customId}> {showProgress && ( <Steps currentStep={step} margin="0 20px 60px 0"> {getSteps().map((step, index) => ( diff --git a/packages/components-faraday/src/components/AuthorList/Author.js b/packages/components-faraday/src/components/AuthorList/Author.js index a0092ad7d..b18d8ed60 100644 --- a/packages/components-faraday/src/components/AuthorList/Author.js +++ b/packages/components-faraday/src/components/AuthorList/Author.js @@ -7,14 +7,11 @@ import { Label } from './FormItems' export default ({ id, firstName, - middleName, lastName, email, affiliation, - country, dragHandle, isOver, - countryParser, removeAuthor, isSubmitting, isCorresponding, @@ -46,13 +43,11 @@ export default ({ </Header> <Row> <Label label="First name" value={firstName} /> - <Label label="Middle name" value={middleName} /> <Label label="Last name" value={lastName} /> </Row> <Row> <Label label="Email" value={email} /> <Label label="Affiliation" value={affiliation} /> - <Label label="Country" value={countryParser(country)} /> </Row> </AuthorContainer> </Root> diff --git a/packages/components-faraday/src/components/AuthorList/AuthorAdder.js b/packages/components-faraday/src/components/AuthorList/AuthorAdder.js index 5bb4d5917..876ec1f9d 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorAdder.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorAdder.js @@ -7,10 +7,14 @@ import styled from 'styled-components' import { compose, withProps } from 'recompose' import { selectCurrentUser } from 'xpub-selectors' -import countries from './countries' import { Spinner } from '../UIComponents/' -import { getAuthorFetching } from '../../redux/authors' -import { MenuItem, ValidatedTextField } from './FormItems' +import { + getAuthorFetching, + getAuthors, + authorSuccess, + authorFailure, +} from '../../redux/authors' +import { ValidatedTextField } from './FormItems' const emailRegex = new RegExp( /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i, //eslint-disable-line @@ -35,7 +39,6 @@ const AuthorAdder = ({ <Title>{authors.length === 0 ? 'Submitting author' : 'Author'}</Title> <Row> <ValidatedTextField isRequired label="First name*" name="firstName" /> - <ValidatedTextField label="Middle name" name="middleName" /> <ValidatedTextField isRequired label="Last name*" name="lastName" /> </Row> @@ -51,7 +54,6 @@ const AuthorAdder = ({ label="Affiliation*" name="affiliation" /> - <MenuItem label="Country*" name="country" options={countries} /> </Row> <ButtonsContainer> <Button data-test="button-cancel-author" onClick={setEditMode(false)}> @@ -75,10 +77,16 @@ const AuthorAdder = ({ ) export default compose( - connect(state => ({ - currentUser: selectCurrentUser(state), - isFetching: getAuthorFetching(state), - })), + connect( + state => ({ + currentUser: selectCurrentUser(state), + isFetching: getAuthorFetching(state), + }), + { + authorSuccess, + authorFailure, + }, + ), withProps(({ currentUser: { admin, username, email }, authors }) => { if (!admin && authors.length === 0) { return { @@ -106,14 +114,22 @@ export default compose( isCorresponding: isFirstAuthor, }, collectionId, - ).then(author => { - const newAuthors = [...authors, author] - setEditMode(false)() - setTimeout(() => { - setFormAuthors(newAuthors) - }, 1000) - reset() - }) + ).then( + () => { + setEditMode(false)() + setTimeout(() => { + getAuthors(collectionId).then( + data => { + dispatch(authorSuccess()) + setFormAuthors(data) + }, + err => dispatch(authorFailure(err)), + ) + }, 10) + reset() + }, + err => dispatch(authorFailure(err)), + ) }, }), )(AuthorAdder) diff --git a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js index 879ff26a3..66ce610d5 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js @@ -6,10 +6,14 @@ import styled, { css } from 'styled-components' import { compose, withHandlers, withProps } from 'recompose' import { reduxForm, Field, change as changeForm } from 'redux-form' -import countries from './countries' import { Spinner } from '../UIComponents' -import { getAuthorFetching } from '../../redux/authors' -import { ValidatedTextField, MenuItem } from './FormItems' +import { + getAuthors, + getAuthorFetching, + authorSuccess, + authorFailure, +} from '../../redux/authors' +import { ValidatedTextField } from './FormItems' const emailRegex = new RegExp( /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i, //eslint-disable-line @@ -32,6 +36,7 @@ const AuthorEdit = ({ isCorresponding, email, changeCorresponding, + ...rest }) => ( <Root> <Header> @@ -69,7 +74,6 @@ const AuthorEdit = ({ label="First name*" name="edit.firstName" /> - <ValidatedTextField label="Middle name" name="edit.middleName" /> <ValidatedTextField isRequired label="Last name*" name="edit.lastName" /> </Row> @@ -85,7 +89,6 @@ const AuthorEdit = ({ label="Affiliation*" name="edit.affiliation" /> - <MenuItem label="Country*" name="edit.country" options={countries} /> </Row> </Root> ) @@ -95,7 +98,7 @@ export default compose( state => ({ isFetching: getAuthorFetching(state), }), - { changeForm }, + { changeForm, authorSuccess, authorFailure }, ), withProps(props => ({ initialValues: { @@ -104,10 +107,8 @@ export default compose( 'isSubmitting', 'firstName', 'lastName', - 'middleName', 'email', 'affiliation', - 'country', ]), }, })), @@ -125,15 +126,21 @@ export default compose( onSubmit: ( values, dispatch, - { setAuthorEdit, setAuthors, authors, index, changeForm }, + { setAuthorEdit, setAuthors, authors, index, changeForm, project }, ) => { - const newAuthors = [ - ...authors.slice(0, index), - values.edit, - ...authors.slice(index + 1), - ] - setAuthorEdit(-1)() - setAuthors(newAuthors) + // const newAuthors = [ + // ...authors.slice(0, index), + // values.edit, + // ...authors.slice(index + 1), + // ] + getAuthors(project.id).then( + data => { + dispatch(authorSuccess()) + setAuthorEdit(-1)() + setAuthors(data) + }, + err => dispatch(authorFailure(err)), + ) }, }), )(AuthorEdit) diff --git a/packages/components-faraday/src/components/AuthorList/AuthorList.js b/packages/components-faraday/src/components/AuthorList/AuthorList.js index c3505142b..8cc1b595e 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorList.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorList.js @@ -1,5 +1,4 @@ import React from 'react' -import { get } from 'lodash' import { th } from '@pubsweet/ui' import PropTypes from 'prop-types' import { connect } from 'react-redux' @@ -15,10 +14,14 @@ import { import { change as changeForm } from 'redux-form' import { SortableList } from 'pubsweet-component-sortable-list/src/components' -import { addAuthor, deleteAuthor, getAuthors } from '../../redux/authors' +import { + addAuthor, + deleteAuthor, + getAuthors, + authorFailure, +} from '../../redux/authors' import Author from './Author' -import countries from './countries' import StaticList from './StaticList' import AuthorAdder from './AuthorAdder' import AuthorEditor from './AuthorEditor' @@ -82,6 +85,7 @@ export default compose( addAuthor, changeForm, deleteAuthor, + authorFailure, }, ), withState('authors', 'setAuthors', []), @@ -107,8 +111,6 @@ export default compose( dropItem: ({ authors, setFormAuthors }) => () => { setFormAuthors(authors) }, - countryParser: () => countryCode => - get(countries.find(c => c.value === countryCode), 'label'), parseAuthorType: () => (isSubmitting, isCorresponding, index) => { if (isSubmitting) return `#${index + 1} Submitting author` if (isCorresponding) return `#${index + 1} Corresponding author` @@ -121,14 +123,20 @@ export default compose( const newAuthors = SortableList.moveItem(authors, dragIndex, hoverIndex) setFormAuthors(newAuthors) }, - removeAuthor: ({ authors, setFormAuthors, deleteAuthor, project }) => ( - id, - authorEmail, - ) => () => { - deleteAuthor(project.id, id).then(() => { - const newAuthors = authors.filter(a => a.id !== id) - setFormAuthors(newAuthors) - }) + removeAuthor: ({ + authors, + setFormAuthors, + deleteAuthor, + authorFailure, + project, + }) => (id, authorEmail) => () => { + deleteAuthor(project.id, id).then( + () => { + const newAuthors = authors.filter(a => a.id !== id) + setFormAuthors(newAuthors) + }, + err => authorFailure(err), + ) }, setAsCorresponding: ({ authors, setFormAuthors }) => authorEmail => () => { const newAuthors = authors.map( diff --git a/packages/components-faraday/src/components/AuthorList/StaticList.js b/packages/components-faraday/src/components/AuthorList/StaticList.js index b8b45d89a..6e00a7a45 100644 --- a/packages/components-faraday/src/components/AuthorList/StaticList.js +++ b/packages/components-faraday/src/components/AuthorList/StaticList.js @@ -7,7 +7,6 @@ export default ({ editIndex, setFormAuthors, removeAuthor, - countryParser, editComponent, setAuthorEdit, parseAuthorType, @@ -27,16 +26,15 @@ export default ({ }, setAuthors: setFormAuthors, setAuthorEdit, - countryParser, parseAuthorType, setAsCorresponding, ...author, + ...rest, }) ) : ( <Author - key={author.firstName} + key={author.id} {...author} - countryParser={countryParser} index={index} parseAuthorType={parseAuthorType} removeAuthor={removeAuthor} diff --git a/packages/components-faraday/src/redux/authors.js b/packages/components-faraday/src/redux/authors.js index 10f6cc645..38a994b2f 100644 --- a/packages/components-faraday/src/redux/authors.js +++ b/packages/components-faraday/src/redux/authors.js @@ -11,7 +11,7 @@ export const authorRequest = () => ({ type: REQUEST, }) -export const authorFaiure = error => ({ +export const authorFailure = error => ({ type: FAILURE, error, }) @@ -22,7 +22,7 @@ export const authorSuccess = () => ({ export const addAuthor = (author, collectionId) => dispatch => { dispatch(authorRequest()) - return create(`/users/invite/${collectionId}`, { + return create(`/collections/${collectionId}/users`, { email: author.email, role: 'author', ...author, @@ -30,11 +30,10 @@ export const addAuthor = (author, collectionId) => dispatch => { } export const deleteAuthor = (collectionId, userId) => dispatch => - remove(`/collections/${collectionId}/users/${userId}?role=author`) -// Promise.resolve(author) + remove(`/collections/${collectionId}/users/${userId}`) export const getAuthors = collectionId => - apiGet(`/collections/${collectionId}/users?role=author`) + apiGet(`/collections/${collectionId}/users`) // selectors export const getFragmentAuthors = (state, fragmentId) => -- GitLab