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

refactor(authors): move author to new endpoints

parent b647b344
No related branches found
No related tags found
1 merge request!5Refactor component invite
const logger = require('@pubsweet/logger') const logger = require('@pubsweet/logger')
const get = require('lodash/get') // const get = require('lodash/get')
const createNewTeam = async (collectionId, role, userId, TeamModel) => { const createNewTeam = async (collectionId, role, userId, TeamModel) => {
let permissions, group, name let permissions, group, name
......
...@@ -9,6 +9,7 @@ import { reduxForm, Field, change as changeForm } from 'redux-form' ...@@ -9,6 +9,7 @@ import { reduxForm, Field, change as changeForm } from 'redux-form'
import { Spinner } from '../UIComponents' import { Spinner } from '../UIComponents'
import { import {
getAuthors, getAuthors,
editAuthor,
getAuthorFetching, getAuthorFetching,
authorSuccess, authorSuccess,
authorFailure, authorFailure,
...@@ -103,6 +104,7 @@ export default compose( ...@@ -103,6 +104,7 @@ export default compose(
withProps(props => ({ withProps(props => ({
initialValues: { initialValues: {
edit: pick(props, [ edit: pick(props, [
'id',
'isCorresponding', 'isCorresponding',
'isSubmitting', 'isSubmitting',
'firstName', 'firstName',
...@@ -128,16 +130,17 @@ export default compose( ...@@ -128,16 +130,17 @@ export default compose(
dispatch, dispatch,
{ setAuthorEdit, setAuthors, authors, index, changeForm, project }, { setAuthorEdit, setAuthors, authors, index, changeForm, project },
) => { ) => {
// const newAuthors = [ const newAuthor = values.edit
// ...authors.slice(0, index), editAuthor(project.id, newAuthor.id, newAuthor).then(
// values.edit, () => {
// ...authors.slice(index + 1), getAuthors(project.id).then(
// ] data => {
getAuthors(project.id).then( dispatch(authorSuccess())
data => { setAuthorEdit(-1)()
dispatch(authorSuccess()) setAuthors(data)
setAuthorEdit(-1)() },
setAuthors(data) err => dispatch(authorFailure(err)),
)
}, },
err => dispatch(authorFailure(err)), err => dispatch(authorFailure(err)),
) )
......
...@@ -19,6 +19,9 @@ import { ...@@ -19,6 +19,9 @@ import {
deleteAuthor, deleteAuthor,
getAuthors, getAuthors,
authorFailure, authorFailure,
getAuthorsTeam,
getAuthorError,
updateAuthorsTeam,
} from '../../redux/authors' } from '../../redux/authors'
import Author from './Author' import Author from './Author'
...@@ -39,6 +42,7 @@ const Authors = ({ ...@@ -39,6 +42,7 @@ const Authors = ({
setEditMode, setEditMode,
editedAuthor, editedAuthor,
setFormAuthors, setFormAuthors,
error,
...rest ...rest
}) => ( }) => (
<Root> <Root>
...@@ -71,6 +75,7 @@ const Authors = ({ ...@@ -71,6 +75,7 @@ const Authors = ({
{...rest} {...rest}
/> />
)} )}
{error && <ErrorMessage>{error}</ErrorMessage>}
</Root> </Root>
) )
...@@ -80,6 +85,7 @@ export default compose( ...@@ -80,6 +85,7 @@ export default compose(
connect( connect(
state => ({ state => ({
currentUser: state.currentUser.user, currentUser: state.currentUser.user,
error: getAuthorError(state),
}), }),
{ {
addAuthor, addAuthor,
...@@ -108,8 +114,20 @@ export default compose( ...@@ -108,8 +114,20 @@ export default compose(
changeForm('wizard', 'editMode', mode) changeForm('wizard', 'editMode', mode)
setEditMode(v => mode) setEditMode(v => mode)
}, },
dropItem: ({ authors, setFormAuthors }) => () => { dropItem: ({ authors, setFormAuthors, project, authorFailure }) => () => {
setFormAuthors(authors) 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) => { parseAuthorType: () => (isSubmitting, isCorresponding, index) => {
if (isSubmitting) return `#${index + 1} Submitting author` if (isSubmitting) return `#${index + 1} Submitting author`
...@@ -164,4 +182,7 @@ const Root = styled.div` ...@@ -164,4 +182,7 @@ const Root = styled.div`
border: ${th('borderDefault')}; border: ${th('borderDefault')};
padding: ${th('subGridUnit')}; padding: ${th('subGridUnit')};
` `
const ErrorMessage = styled.div`
color: ${th('colorError')};
`
// #endregion // #endregion
import { get } from 'lodash' import { get, head } from 'lodash'
import { create, get as apiGet, remove } from 'pubsweet-client/src/helpers/api' import {
create,
get as apiGet,
remove,
update,
} from 'pubsweet-client/src/helpers/api'
// constants // constants
const REQUEST = 'authors/REQUEST' const REQUEST = 'authors/REQUEST'
...@@ -29,12 +34,25 @@ export const addAuthor = (author, collectionId) => dispatch => { ...@@ -29,12 +34,25 @@ export const addAuthor = (author, collectionId) => dispatch => {
}) })
} }
export const deleteAuthor = (collectionId, userId) => dispatch => export const deleteAuthor = (collectionId, userId) => dispatch => {
remove(`/collections/${collectionId}/users/${userId}`) dispatch(authorRequest())
return remove(`/collections/${collectionId}/users/${userId}`)
}
export const editAuthor = (collectionId, userId, body) =>
update(`/collections/${collectionId}/users/${userId}`, body)
export const getAuthors = collectionId => export const getAuthors = collectionId =>
apiGet(`/collections/${collectionId}/users`) 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 // selectors
export const getFragmentAuthors = (state, fragmentId) => export const getFragmentAuthors = (state, fragmentId) =>
get(state, `authors.${fragmentId}`) || [] get(state, `authors.${fragmentId}`) || []
...@@ -56,7 +74,7 @@ export default (state = initialState, action) => { ...@@ -56,7 +74,7 @@ export default (state = initialState, action) => {
case FAILURE: case FAILURE:
return { return {
...initialState, ...initialState,
error: action.error, error: get(JSON.parse(get(action.error, 'response') || {}), 'error'),
isFetching: false, isFetching: false,
} }
case 'UPDATE_FRAGMENT_SUCCESS': case 'UPDATE_FRAGMENT_SUCCESS':
......
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