Skip to content
Snippets Groups Projects
Commit 8b1c11b7 authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

refactor(submition-flow): author list

N
parent c47c2073
No related branches found
No related tags found
2 merge requests!13Sprint #14,!11Submit revision
...@@ -21,25 +21,6 @@ class Fragment { ...@@ -21,25 +21,6 @@ class Fragment {
heRecommendation, heRecommendation,
} }
} }
async addAuthor({ user, isSubmitting, isCorresponding }) {
const { fragment } = this
fragment.authors = fragment.authors || []
const author = {
id: user.id,
firstName: user.firstName || '',
lastName: user.lastName || '',
email: user.email,
title: user.title || '',
affiliation: user.affiliation || '',
isSubmitting,
isCorresponding,
}
fragment.authors.push(author)
await fragment.save()
return author
}
} }
module.exports = Fragment module.exports = Fragment
...@@ -10,7 +10,6 @@ module.exports = models => async (req, res) => { ...@@ -10,7 +10,6 @@ module.exports = models => async (req, res) => {
return res.status(400).json({ return res.status(400).json({
error: `Fragment ${fragmentId} does not match collection ${collectionId}`, error: `Fragment ${fragmentId} does not match collection ${collectionId}`,
}) })
const fragment = await models.Fragment.find(fragmentId)
const teamHelper = new Team({ TeamModel: models.Team, fragmentId }) const teamHelper = new Team({ TeamModel: models.Team, fragmentId })
const team = await teamHelper.getTeam({ const team = await teamHelper.getTeam({
...@@ -18,8 +17,6 @@ module.exports = models => async (req, res) => { ...@@ -18,8 +17,6 @@ module.exports = models => async (req, res) => {
objectType: 'fragment', objectType: 'fragment',
}) })
fragment.authors = fragment.authors.filter(author => author.id !== userId)
await fragment.save()
await teamHelper.removeTeamMember({ teamId: team.id, userId }) await teamHelper.removeTeamMember({ teamId: team.id, userId })
user.teams = user.teams.filter(userTeamId => team.id !== userTeamId) user.teams = user.teams.filter(userTeamId => team.id !== userTeamId)
delete user.passwordResetToken delete user.passwordResetToken
......
const { pick } = require('lodash')
const mailService = require('pubsweet-component-mail-service') const mailService = require('pubsweet-component-mail-service')
const { const { User, Team, services } = require('pubsweet-component-helper-service')
User,
Team, const authorKeys = [
services, 'id',
Fragment, 'email',
} = require('pubsweet-component-helper-service') 'title',
'lastName',
'firstName',
'affiliation',
]
module.exports = models => async (req, res) => { module.exports = models => async (req, res) => {
const { email, role, isSubmitting, isCorresponding } = req.body const { email, role, isSubmitting, isCorresponding } = req.body
...@@ -36,7 +41,6 @@ module.exports = models => async (req, res) => { ...@@ -36,7 +41,6 @@ module.exports = models => async (req, res) => {
}) })
} }
const baseUrl = services.getBaseUrl(req) const baseUrl = services.getBaseUrl(req)
const fragmentHelper = new Fragment({ fragment })
const UserModel = models.User const UserModel = models.User
const teamHelper = new Team({ TeamModel: models.Team, fragmentId }) const teamHelper = new Team({ TeamModel: models.Team, fragmentId })
...@@ -56,13 +60,11 @@ module.exports = models => async (req, res) => { ...@@ -56,13 +60,11 @@ module.exports = models => async (req, res) => {
} }
} }
const fragmentAuthor = await fragmentHelper.addAuthor({ return res.status(200).json({
user, ...pick(user, authorKeys),
isSubmitting, isSubmitting,
isCorresponding, isCorresponding,
}) })
return res.status(200).json(fragmentAuthor)
} }
return res.status(400).json({ return res.status(400).json({
error: `${role} is not defined`, error: `${role} is not defined`,
...@@ -97,12 +99,6 @@ module.exports = models => async (req, res) => { ...@@ -97,12 +99,6 @@ module.exports = models => async (req, res) => {
return res.status(200).json(newUser) return res.status(200).json(newUser)
} }
const fragmentAuthor = await fragmentHelper.addAuthor({
user: newUser,
isSubmitting,
isCorresponding,
})
mailService.sendSimpleEmail({ mailService.sendSimpleEmail({
toEmail: newUser.email, toEmail: newUser.email,
user: newUser, user: newUser,
...@@ -110,7 +106,11 @@ module.exports = models => async (req, res) => { ...@@ -110,7 +106,11 @@ module.exports = models => async (req, res) => {
dashboardUrl: baseUrl, dashboardUrl: baseUrl,
}) })
return res.status(200).json(fragmentAuthor) return res.status(200).json({
...pick(newUser, authorKeys),
isSubmitting,
isCorresponding,
})
} }
return res.status(e.status).json({ return res.status(e.status).json({
error: `Something went wrong: ${e.name}`, error: `Something went wrong: ${e.name}`,
......
import React from 'react' import React from 'react'
import { get } from 'lodash' import { get } from 'lodash'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { reduxForm } from 'redux-form'
import styled from 'styled-components' import styled from 'styled-components'
import { Button, th } from '@pubsweet/ui' import { Button, th } from '@pubsweet/ui'
import { selectCurrentUser } from 'xpub-selectors' import { selectCurrentUser } from 'xpub-selectors'
import { reduxForm, change as changeForm } from 'redux-form'
import { compose, withProps, setDisplayName } from 'recompose' import { compose, withProps, setDisplayName } from 'recompose'
import { emailValidator } from '../utils' import { emailValidator } from '../utils'
...@@ -76,6 +76,7 @@ export default compose( ...@@ -76,6 +76,7 @@ export default compose(
currentUser: selectCurrentUser(state), currentUser: selectCurrentUser(state),
}), }),
{ {
changeForm,
authorSuccess, authorSuccess,
authorFailure, authorFailure,
}, },
...@@ -104,23 +105,27 @@ export default compose( ...@@ -104,23 +105,27 @@ export default compose(
onSubmit: ( onSubmit: (
values, values,
dispatch, dispatch,
{ authors = [], addAuthor, setEditMode, setFormAuthors, reset, match }, {
reset,
match,
changeForm,
addAuthor,
setEditMode,
setFormAuthors,
authors = [],
},
) => { ) => {
const collectionId = get(match, 'params.project') const collectionId = get(match, 'params.project')
const fragmentId = get(match, 'params.version') const fragmentId = get(match, 'params.version')
const isFirstAuthor = authors.length === 0 const isFirstAuthor = authors.length === 0
const newAuthor = {
addAuthor( ...values,
{ isSubmitting: isFirstAuthor,
...values, isCorresponding: isFirstAuthor,
isSubmitting: isFirstAuthor, }
isCorresponding: isFirstAuthor, addAuthor(newAuthor, collectionId, fragmentId).then(author => {
}, changeForm('wizard', 'authors', [...authors, author])
collectionId,
fragmentId,
).then(authors => {
setEditMode(false)() setEditMode(false)()
setFormAuthors(authors)
reset() reset()
}) })
}, },
......
...@@ -102,11 +102,8 @@ export default compose( ...@@ -102,11 +102,8 @@ export default compose(
})), })),
withHandlers({ withHandlers({
setFormAuthors: ({ setAuthors, changeForm }) => (authors = []) => { setFormAuthors: ({ setAuthors, changeForm }) => (authors = []) => {
const mappedAuthors = authors setAuthors(authors)
.filter(utils.filterEmptyAuthors) changeForm('wizard', 'authors', authors)
.map(utils.getAuthorProperties)
setAuthors(mappedAuthors)
changeForm('wizard', 'authors', mappedAuthors)
}, },
}), }),
withHandlers({ withHandlers({
...@@ -139,7 +136,11 @@ export default compose( ...@@ -139,7 +136,11 @@ export default compose(
setFormAuthors(newAuthors) setFormAuthors(newAuthors)
}) })
}, },
setAsCorresponding: ({ authors, setFormAuthors }) => id => () => { setAsCorresponding: ({
authors,
setAuthors,
setFormAuthors,
}) => id => () => {
const newAuthors = authors.map(utils.setCorresponding(id)) const newAuthors = authors.map(utils.setCorresponding(id))
setFormAuthors(newAuthors) setFormAuthors(newAuthors)
}, },
......
import { pick, isEmpty } from 'lodash'
export const authorKeys = [ export const authorKeys = [
'id', 'id',
'email', 'email',
...@@ -10,10 +8,6 @@ export const authorKeys = [ ...@@ -10,10 +8,6 @@ export const authorKeys = [
'isCorresponding', 'isCorresponding',
] ]
export const filterEmptyAuthors = author => !isEmpty(author)
export const getAuthorProperties = author => pick(author, authorKeys)
export const setCorresponding = id => author => export const setCorresponding = id => author =>
author.id === id author.id === id
? { ? {
......
...@@ -31,21 +31,19 @@ export const addAuthor = (author, collectionId, fragmentId) => dispatch => { ...@@ -31,21 +31,19 @@ export const addAuthor = (author, collectionId, fragmentId) => dispatch => {
email: author.email, email: author.email,
role: 'author', role: 'author',
...author, ...author,
}).then( }).then(author => {
() => dispatch(authorSuccess())
getAuthors(collectionId, fragmentId).then(data => { return author
dispatch(authorSuccess()) }, handleError(authorFailure, dispatch))
return data
}, handleError(authorFailure, dispatch)),
handleError(authorFailure, dispatch),
)
} }
export const deleteAuthor = (collectionId, fragmentId, userId) => dispatch => { export const deleteAuthor = (collectionId, fragmentId, userId) => dispatch => {
dispatch(authorRequest()) dispatch(authorRequest())
return remove( return remove(
`/collections/${collectionId}/fragments/${fragmentId}/users/${userId}`, `/collections/${collectionId}/fragments/${fragmentId}/users/${userId}`,
).catch(handleError(authorFailure, dispatch)) )
.then(() => dispatch(authorSuccess()))
.catch(handleError(authorFailure, dispatch))
} }
// selectors // selectors
......
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