From 8b1c11b79b48208c91044616db4daf61d88a49dd Mon Sep 17 00:00:00 2001 From: Alexandru Munteanu <alexandru.munt@gmail.com> Date: Thu, 14 Jun 2018 16:57:16 +0300 Subject: [PATCH] refactor(submition-flow): author list N --- .../src/services/Fragment.js | 19 ---------- .../src/routes/fragmentsUsers/delete.js | 3 -- .../src/routes/fragmentsUsers/post.js | 36 +++++++++---------- .../src/components/AuthorList/AuthorAdder.js | 31 +++++++++------- .../src/components/AuthorList/AuthorList.js | 13 +++---- .../src/components/AuthorList/utils.js | 6 ---- .../components-faraday/src/redux/authors.js | 16 ++++----- 7 files changed, 50 insertions(+), 74 deletions(-) diff --git a/packages/component-helper-service/src/services/Fragment.js b/packages/component-helper-service/src/services/Fragment.js index f07543c77..d4be48eec 100644 --- a/packages/component-helper-service/src/services/Fragment.js +++ b/packages/component-helper-service/src/services/Fragment.js @@ -21,25 +21,6 @@ class Fragment { 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 diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/delete.js b/packages/component-user-manager/src/routes/fragmentsUsers/delete.js index 907b689de..86073f5d5 100644 --- a/packages/component-user-manager/src/routes/fragmentsUsers/delete.js +++ b/packages/component-user-manager/src/routes/fragmentsUsers/delete.js @@ -10,7 +10,6 @@ module.exports = models => async (req, res) => { return res.status(400).json({ error: `Fragment ${fragmentId} does not match collection ${collectionId}`, }) - const fragment = await models.Fragment.find(fragmentId) const teamHelper = new Team({ TeamModel: models.Team, fragmentId }) const team = await teamHelper.getTeam({ @@ -18,8 +17,6 @@ module.exports = models => async (req, res) => { objectType: 'fragment', }) - fragment.authors = fragment.authors.filter(author => author.id !== userId) - await fragment.save() await teamHelper.removeTeamMember({ teamId: team.id, userId }) user.teams = user.teams.filter(userTeamId => team.id !== userTeamId) delete user.passwordResetToken diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/post.js b/packages/component-user-manager/src/routes/fragmentsUsers/post.js index 9a752bfb7..e298dbebc 100644 --- a/packages/component-user-manager/src/routes/fragmentsUsers/post.js +++ b/packages/component-user-manager/src/routes/fragmentsUsers/post.js @@ -1,11 +1,16 @@ +const { pick } = require('lodash') const mailService = require('pubsweet-component-mail-service') -const { - User, - Team, - services, - Fragment, -} = require('pubsweet-component-helper-service') +const { User, Team, services } = require('pubsweet-component-helper-service') + +const authorKeys = [ + 'id', + 'email', + 'title', + 'lastName', + 'firstName', + 'affiliation', +] module.exports = models => async (req, res) => { const { email, role, isSubmitting, isCorresponding } = req.body @@ -36,7 +41,6 @@ module.exports = models => async (req, res) => { }) } const baseUrl = services.getBaseUrl(req) - const fragmentHelper = new Fragment({ fragment }) const UserModel = models.User const teamHelper = new Team({ TeamModel: models.Team, fragmentId }) @@ -56,13 +60,11 @@ module.exports = models => async (req, res) => { } } - const fragmentAuthor = await fragmentHelper.addAuthor({ - user, + return res.status(200).json({ + ...pick(user, authorKeys), isSubmitting, isCorresponding, }) - - return res.status(200).json(fragmentAuthor) } return res.status(400).json({ error: `${role} is not defined`, @@ -97,12 +99,6 @@ module.exports = models => async (req, res) => { return res.status(200).json(newUser) } - const fragmentAuthor = await fragmentHelper.addAuthor({ - user: newUser, - isSubmitting, - isCorresponding, - }) - mailService.sendSimpleEmail({ toEmail: newUser.email, user: newUser, @@ -110,7 +106,11 @@ module.exports = models => async (req, res) => { dashboardUrl: baseUrl, }) - return res.status(200).json(fragmentAuthor) + return res.status(200).json({ + ...pick(newUser, authorKeys), + isSubmitting, + isCorresponding, + }) } return res.status(e.status).json({ error: `Something went wrong: ${e.name}`, diff --git a/packages/components-faraday/src/components/AuthorList/AuthorAdder.js b/packages/components-faraday/src/components/AuthorList/AuthorAdder.js index 8ebff1e86..82f730556 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorAdder.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorAdder.js @@ -1,10 +1,10 @@ import React from 'react' import { get } from 'lodash' import { connect } from 'react-redux' -import { reduxForm } from 'redux-form' import styled from 'styled-components' import { Button, th } from '@pubsweet/ui' import { selectCurrentUser } from 'xpub-selectors' +import { reduxForm, change as changeForm } from 'redux-form' import { compose, withProps, setDisplayName } from 'recompose' import { emailValidator } from '../utils' @@ -76,6 +76,7 @@ export default compose( currentUser: selectCurrentUser(state), }), { + changeForm, authorSuccess, authorFailure, }, @@ -104,23 +105,27 @@ export default compose( onSubmit: ( values, dispatch, - { authors = [], addAuthor, setEditMode, setFormAuthors, reset, match }, + { + reset, + match, + changeForm, + addAuthor, + setEditMode, + setFormAuthors, + authors = [], + }, ) => { const collectionId = get(match, 'params.project') const fragmentId = get(match, 'params.version') const isFirstAuthor = authors.length === 0 - - addAuthor( - { - ...values, - isSubmitting: isFirstAuthor, - isCorresponding: isFirstAuthor, - }, - collectionId, - fragmentId, - ).then(authors => { + const newAuthor = { + ...values, + isSubmitting: isFirstAuthor, + isCorresponding: isFirstAuthor, + } + addAuthor(newAuthor, collectionId, fragmentId).then(author => { + changeForm('wizard', 'authors', [...authors, author]) setEditMode(false)() - setFormAuthors(authors) reset() }) }, diff --git a/packages/components-faraday/src/components/AuthorList/AuthorList.js b/packages/components-faraday/src/components/AuthorList/AuthorList.js index fde894862..5045a0615 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorList.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorList.js @@ -102,11 +102,8 @@ export default compose( })), withHandlers({ setFormAuthors: ({ setAuthors, changeForm }) => (authors = []) => { - const mappedAuthors = authors - .filter(utils.filterEmptyAuthors) - .map(utils.getAuthorProperties) - setAuthors(mappedAuthors) - changeForm('wizard', 'authors', mappedAuthors) + setAuthors(authors) + changeForm('wizard', 'authors', authors) }, }), withHandlers({ @@ -139,7 +136,11 @@ export default compose( setFormAuthors(newAuthors) }) }, - setAsCorresponding: ({ authors, setFormAuthors }) => id => () => { + setAsCorresponding: ({ + authors, + setAuthors, + setFormAuthors, + }) => id => () => { const newAuthors = authors.map(utils.setCorresponding(id)) setFormAuthors(newAuthors) }, diff --git a/packages/components-faraday/src/components/AuthorList/utils.js b/packages/components-faraday/src/components/AuthorList/utils.js index 2127775a2..ea68ddb2b 100644 --- a/packages/components-faraday/src/components/AuthorList/utils.js +++ b/packages/components-faraday/src/components/AuthorList/utils.js @@ -1,5 +1,3 @@ -import { pick, isEmpty } from 'lodash' - export const authorKeys = [ 'id', 'email', @@ -10,10 +8,6 @@ export const authorKeys = [ 'isCorresponding', ] -export const filterEmptyAuthors = author => !isEmpty(author) - -export const getAuthorProperties = author => pick(author, authorKeys) - export const setCorresponding = id => author => author.id === id ? { diff --git a/packages/components-faraday/src/redux/authors.js b/packages/components-faraday/src/redux/authors.js index d50c86c9d..324cc26ad 100644 --- a/packages/components-faraday/src/redux/authors.js +++ b/packages/components-faraday/src/redux/authors.js @@ -31,21 +31,19 @@ export const addAuthor = (author, collectionId, fragmentId) => dispatch => { email: author.email, role: 'author', ...author, - }).then( - () => - getAuthors(collectionId, fragmentId).then(data => { - dispatch(authorSuccess()) - return data - }, handleError(authorFailure, dispatch)), - handleError(authorFailure, dispatch), - ) + }).then(author => { + dispatch(authorSuccess()) + return author + }, handleError(authorFailure, dispatch)) } export const deleteAuthor = (collectionId, fragmentId, userId) => dispatch => { dispatch(authorRequest()) return remove( `/collections/${collectionId}/fragments/${fragmentId}/users/${userId}`, - ).catch(handleError(authorFailure, dispatch)) + ) + .then(() => dispatch(authorSuccess())) + .catch(handleError(authorFailure, dispatch)) } // selectors -- GitLab