Skip to content
Snippets Groups Projects
Commit 42721619 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

refactor(authors): wip: move adding authors to new endpoints

parent 9f783b80
No related branches found
No related tags found
1 merge request!5Refactor component invite
......@@ -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) => (
......
......@@ -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>
......
......@@ -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)
......
......@@ -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)
......
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(
......
......@@ -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}
......
......@@ -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) =>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment