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 get = require('lodash/get')
// const get = require('lodash/get')
const createNewTeam = async (collectionId, role, userId, TeamModel) => {
let permissions, group, name
......
......@@ -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)),
)
......
......@@ -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
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':
......
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