Skip to content
Snippets Groups Projects
WizardAuthors.js 4.89 KiB
Newer Older
import React, { Fragment } from 'react'
import styled from 'styled-components'
import { th } from '@pubsweet/ui-toolkit'
import { omit, isBoolean, get } from 'lodash'
import { compose, withState, withHandlers } from 'recompose'

import {
  Row,
  Item,
  Label,
  DragHandle,
  AuthorCard,
  ActionLink,
  SortableList,
const castToBool = author => ({
  ...author,
  isCorresponding: isBoolean(author.isCorresponding) && author.isCorresponding,
})

const parseEditedAuthors = (editedAuthor, authors) => {
  const newAuthor = castToBool(editedAuthor)

  return authors.map(
    a =>
      a.id === newAuthor.id
        ? newAuthor
        : {
            ...a,
            isCorresponding: newAuthor.isCorresponding
              ? false
              : a.isCorresponding,
          },
  )
}

const WizardAuthors = ({
  addNewAuthor,
  setAuthorEdit,
  saveNewAuthor,
  editExistingAuthor,
  authorEditorSubmit,
    {isFetching && <h1>loading...</h1>}
    <Row alignItems="center" justify="flex-start">
      <Item>
        <Label>Authors</Label>
        <ActionLink icon="plus" onClick={addNewAuthor}>
          ADD AUTHOR
        </ActionLink>
      </Item>
      <Item justify="flex-end">
        <ActionLink
          icon="link"
          iconPosition="right"
            'submission.links.authorGuidelines',
            'https://www.hindawi.com/journals/',
          )}
        </ActionLink>
      </Item>
    </Row>
    {authors.length > 0 && (
      <SortableContainer>
        <SortableList
          authorEditorSubmit={authorEditorSubmit}
          dragHandle={DragHandle}
          editExistingAuthor={editExistingAuthor}
          isAuthorEdit={isAuthorEdit}
          isAuthorsFetching={isAuthorsFetching}
          itemKey="id"
          items={authors}
          listItem={AuthorCard}
          moveItem={moveAuthor}
          onEdit={setAuthorEdit}
          saveNewAuthor={saveNewAuthor}
        />
      </SortableContainer>
    )}
      <Row justify="flex-start" mb={1} mt={1}>
        <Text error>{error}</Text>
      </Row>
    )}
  </Fragment>
)

export default compose(
  withState('authors', 'setAuthors', ({ authors }) => authors),
  withHandlers({
    setFormAuthors: ({ changeForm, setAuthors, setFetching }) => authors => {
      changeForm('submission', 'authors', authors)
    },
  }),
    addNewAuthor: ({ authors = [], setAuthors }) => () => {
      if (authors.some(a => a.id === 'newAuthor')) {
        return
      }
      setAuthors([
        ...authors,
        { id: 'newAuthor', isCorresponding: false, isSubmitting: false },
      ])
    moveAuthor: ({ authors, setFormAuthors }) => (dragIndex, hoverIndex) => {
      setFormAuthors(SortableList.moveItem(authors, dragIndex, hoverIndex))
    setAuthorEdit: ({ authors, setAuthors, onAuthorEdit }) => index => {
      onAuthorEdit(index)
      setAuthors(authors.filter(a => a.id !== 'newAuthor'))
    },
    saveNewAuthor: ({ authors, setFormAuthors }) => author => {
      setFormAuthors(
        parseEditedAuthors(author, [
          ...authors.filter(a => a.id !== 'newAuthor'),
          author,
        ]),
      )
    editExistingAuthor: ({ authors, setFormAuthors }) => editedAuthor => {
      const newAuthors = parseEditedAuthors(editedAuthor, authors)
      setFormAuthors(newAuthors)
    deleteAuthor: ({
      authors,
      project,
      version,
      deleteAuthor,
      setFormAuthors,
    }) => author => ({ hideModal, setModalError }) =>
      deleteAuthor(project.id, version.id, author.id)
        .then(() => {
          setFormAuthors(authors.filter(a => a.id !== author.id))
          hideModal()
        })
        .catch(() => {
          setModalError('Something went wrong... Please try again.')
        }),
    authorEditorSubmit: ({
      authors,
      project,
      version,
      addAuthor,
      saveNewAuthor,
      editExistingAuthor,
    }) => (values, dispatch, { toggleEditMode }) => {
      if (values.id === 'newAuthor') {
          },
          project.id,
          version.id,
        ).then(saveNewAuthor)
      editExistingAuthor(values)
      setTimeout(toggleEditMode, 10)
    },
  }),
)(WizardAuthors)

// #region styles
const SortableContainer = styled.div`
  background-color: ${th('colorBackground')};
  border-radius: ${th('borderRadius')};
  padding: ${th('gridUnit')};