Skip to content
Snippets Groups Projects
withHandleRecommendation.js 1.67 KiB
Newer Older
import { pick } from 'lodash'
import { withHandlers, withProps, compose } from 'recompose'
import { handleError, withFetching } from 'pubsweet-component-faraday-ui'
import { parseEicDecision } from './utils'
import { createRecommendation } from './handleRecommendation.api'

export default compose(
  withFetching,
  withHandlers({
    createRecommendation: ({
      fragment,
      collection,
      fetchUpdatedCollection,
    }) => (values, modalProps) => {
      const recommendation = parseEicDecision(values)
      modalProps.setFetching(true)
      createRecommendation({
        recommendation,
        fragmentId: fragment.id,
        collectionId: collection.id,
      })
        .then(() => {
          modalProps.setFetching(false)
          modalProps.hideModal()
          fetchUpdatedCollection()
        })
        .catch(err => {
          modalProps.setFetching(false)
          handleError(modalProps.setModalError)(err)
        })
    },
    onEditorialRecommendation: ({
      fragment,
      collection,
      fetchUpdatedCollection,
    }) => (
      recommendation,
      { hideModal, setFetching, setModalError, reset },
    ) => {
      setFetching(true)
      createRecommendation({
        recommendation,
        fragmentId: fragment.id,
        collectionId: collection.id,
      })
        .then(r => {
          setFetching(false)
          hideModal()
          fetchUpdatedCollection()
        })
        .catch(e => {
          setFetching(false)
          handleError(setModalError)(e)
        })
    },
  }),
  withProps(props => ({
    recommendationHandler: {
      ...pick(props, ['createRecommendation', 'onEditorialRecommendation']),
    },
  })),
)