diff --git a/packages/component-user-manager/src/helpers/Team.js b/packages/component-user-manager/src/helpers/Team.js index dc3990cb2d173f804fba18a0640dd1bf42f3426c..3b700c4436f1306a7b41b07ab4b9ded4aa566944 100644 --- a/packages/component-user-manager/src/helpers/Team.js +++ b/packages/component-user-manager/src/helpers/Team.js @@ -1,5 +1,5 @@ const logger = require('@pubsweet/logger') -const get = require('lodash/get') +// const get = require('lodash/get') const createNewTeam = async (collectionId, role, userId, TeamModel) => { let permissions, group, name diff --git a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js index 66ce610d5261336bb0f9045c2b1e0d0ed24da3c6..53f69eecc5e7fc0e87c570a54b93930133eb768f 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js @@ -9,6 +9,7 @@ import { reduxForm, Field, change as changeForm } from 'redux-form' import { Spinner } from '../UIComponents' import { getAuthors, + editAuthor, getAuthorFetching, authorSuccess, authorFailure, @@ -103,6 +104,7 @@ export default compose( withProps(props => ({ initialValues: { edit: pick(props, [ + 'id', 'isCorresponding', 'isSubmitting', 'firstName', @@ -128,16 +130,17 @@ export default compose( dispatch, { setAuthorEdit, setAuthors, authors, index, changeForm, project }, ) => { - // const newAuthors = [ - // ...authors.slice(0, index), - // values.edit, - // ...authors.slice(index + 1), - // ] - getAuthors(project.id).then( - data => { - dispatch(authorSuccess()) - setAuthorEdit(-1)() - setAuthors(data) + const newAuthor = values.edit + editAuthor(project.id, newAuthor.id, newAuthor).then( + () => { + getAuthors(project.id).then( + data => { + dispatch(authorSuccess()) + setAuthorEdit(-1)() + setAuthors(data) + }, + err => dispatch(authorFailure(err)), + ) }, err => dispatch(authorFailure(err)), ) diff --git a/packages/components-faraday/src/components/AuthorList/AuthorList.js b/packages/components-faraday/src/components/AuthorList/AuthorList.js index 8cc1b595e8819db1dc5e75e7751f426cfc486ed2..6e5f0e2a78e3cda041c505de7426a741bb8ec09d 100644 --- a/packages/components-faraday/src/components/AuthorList/AuthorList.js +++ b/packages/components-faraday/src/components/AuthorList/AuthorList.js @@ -19,6 +19,9 @@ import { deleteAuthor, getAuthors, authorFailure, + getAuthorsTeam, + getAuthorError, + updateAuthorsTeam, } from '../../redux/authors' import Author from './Author' @@ -39,6 +42,7 @@ const Authors = ({ setEditMode, editedAuthor, setFormAuthors, + error, ...rest }) => ( <Root> @@ -71,6 +75,7 @@ const Authors = ({ {...rest} /> )} + {error && <ErrorMessage>{error}</ErrorMessage>} </Root> ) @@ -80,6 +85,7 @@ export default compose( connect( state => ({ currentUser: state.currentUser.user, + error: getAuthorError(state), }), { addAuthor, @@ -108,8 +114,20 @@ export default compose( changeForm('wizard', 'editMode', mode) setEditMode(v => mode) }, - dropItem: ({ authors, setFormAuthors }) => () => { + dropItem: ({ authors, setFormAuthors, project, authorFailure }) => () => { setFormAuthors(authors) + getAuthorsTeam(project.id) + .then(team => { + const members = authors.map(a => a.id) + updateAuthorsTeam(team.id, { members }).catch(err => { + authorFailure(err) + getAuthors(project.id).then(setFormAuthors) + }) + }) + .catch(err => { + authorFailure(err) + getAuthors(project.id).then(setFormAuthors) + }) }, parseAuthorType: () => (isSubmitting, isCorresponding, index) => { if (isSubmitting) return `#${index + 1} Submitting author` @@ -164,4 +182,7 @@ const Root = styled.div` border: ${th('borderDefault')}; padding: ${th('subGridUnit')}; ` +const ErrorMessage = styled.div` + color: ${th('colorError')}; +` // #endregion diff --git a/packages/components-faraday/src/redux/authors.js b/packages/components-faraday/src/redux/authors.js index 38a994b2f68c6b4b3347cb52b36c9ca0f372cc72..e7767063fce3ad2c52194935e28b5875a5d60d95 100644 --- a/packages/components-faraday/src/redux/authors.js +++ b/packages/components-faraday/src/redux/authors.js @@ -1,5 +1,10 @@ -import { get } from 'lodash' -import { create, get as apiGet, remove } from 'pubsweet-client/src/helpers/api' +import { get, head } from 'lodash' +import { + create, + get as apiGet, + remove, + update, +} from 'pubsweet-client/src/helpers/api' // constants const REQUEST = 'authors/REQUEST' @@ -29,12 +34,25 @@ export const addAuthor = (author, collectionId) => dispatch => { }) } -export const deleteAuthor = (collectionId, userId) => dispatch => - remove(`/collections/${collectionId}/users/${userId}`) +export const deleteAuthor = (collectionId, userId) => dispatch => { + dispatch(authorRequest()) + return remove(`/collections/${collectionId}/users/${userId}`) +} + +export const editAuthor = (collectionId, userId, body) => + update(`/collections/${collectionId}/users/${userId}`, body) export const getAuthors = collectionId => apiGet(`/collections/${collectionId}/users`) +export const getAuthorsTeam = collectionId => + apiGet(`/teams?object.id=${collectionId}&group=author`).then(teams => + head(teams), + ) + +export const updateAuthorsTeam = (teamId, body) => + update(`/teams/${teamId}s`, body) + // selectors export const getFragmentAuthors = (state, fragmentId) => get(state, `authors.${fragmentId}`) || [] @@ -56,7 +74,7 @@ export default (state = initialState, action) => { case FAILURE: return { ...initialState, - error: action.error, + error: get(JSON.parse(get(action.error, 'response') || {}), 'error'), isFetching: false, } case 'UPDATE_FRAGMENT_SUCCESS':