import { get } from 'lodash' import { get as apiGet, create, remove, update, } from 'pubsweet-client/src/helpers/api' const EDITORS_REQUEST = 'EDITORS_REQUEST' const EDITORS_DONE = 'EDITORS_DONE' const SET_HANDLING_EDITORS = 'SET_HANDLING_EDITORS' const setHandlingEditors = editors => ({ type: SET_HANDLING_EDITORS, payload: { editors }, }) export const selectFetching = state => get(state, 'editors.isFetching') export const selectHandlingEditors = state => get(state, 'editors.editors') const editorsRequest = () => ({ type: EDITORS_REQUEST }) const editorsDone = () => ({ type: EDITORS_DONE }) export const getHandlingEditors = () => dispatch => apiGet(`/users?handlingEditor=true`).then(res => dispatch(setHandlingEditors(res.users)), ) export const assignHandlingEditor = (email, collectionId) => dispatch => { dispatch(editorsRequest()) return create(`/collections/${collectionId}/invitations`, { email, role: 'handlingEditor', }).then( res => { dispatch(editorsDone()) return res }, err => { dispatch(editorsDone()) throw err }, ) } export const revokeHandlingEditor = ( invitationId, collectionId, ) => dispatch => { dispatch(editorsRequest()) return remove( `/collections/${collectionId}/invitations/${invitationId}`, ).then( res => { dispatch(editorsDone()) return res }, err => { dispatch(editorsDone()) throw err }, ) } export const handlingEditorDecision = ( invitationId, collectionId, isAccepted, reason, ) => update(`/collections/${collectionId}/invitations/${invitationId}`, { isAccepted, reason, }) const initialState = { isFetching: false, editors: [], } export default (state = initialState, action = {}) => { switch (action.type) { case EDITORS_REQUEST: return { ...state, isFetching: true, } case EDITORS_DONE: return { ...state, isFetching: false, } case SET_HANDLING_EDITORS: return { ...state, editors: action.payload.editors, } default: return state } }