From c47c2073ddd47e9ca936b31a2e6ef130c33db04c Mon Sep 17 00:00:00 2001
From: Alexandru Munteanu <alexandru.munt@gmail.com>
Date: Thu, 14 Jun 2018 16:07:23 +0300
Subject: [PATCH] refactor(submit-authors): remove useless code

---
 .../src/FragmentsUsers.js                     | 52 -----------------
 .../src/routes/fragmentsUsers/get.js          | 32 +----------
 .../src/routes/fragmentsUsers/patch.js        | 57 -------------------
 .../src/components/AuthorList/AuthorEditor.js | 22 ++-----
 .../components-faraday/src/redux/authors.js   | 28 +--------
 5 files changed, 11 insertions(+), 180 deletions(-)
 delete mode 100644 packages/component-user-manager/src/routes/fragmentsUsers/patch.js

diff --git a/packages/component-user-manager/src/FragmentsUsers.js b/packages/component-user-manager/src/FragmentsUsers.js
index 464e84743..d1137db20 100644
--- a/packages/component-user-manager/src/FragmentsUsers.js
+++ b/packages/component-user-manager/src/FragmentsUsers.js
@@ -110,58 +110,6 @@ const FragmentsUsers = app => {
    *    HTTP/1.1 404 Not Found
    */
   app.get(basePath, authBearer, require(`${routePath}/get`)(app.locals.models))
-  /**
-   * @api {patch} /api/collections/:collectionId/fragments/:fragmentId/users/:userId Update a user on a fragment
-   * @apiGroup FragmentsUsers
-   * @apiParam {collectionId} collectionId Collection id
-   * @apiParam {fragmentId} fragmentId Fragment id
-   * @apiParam {userId} userId User id
-   * @apiParamExample {json} Body
-   *  {
-   *    "isSubmitting": false,
-   *    "isCorresponding": true,
-   *    "firstName": "John",
-   *    "lastName": "Smith",
-   *    "affiliation": "UCLA"
-   *  }
-   * @apiSuccessExample {json} Success
-   *  HTTP/1.1 200 OK
-   *  {
-   *    "id": "c95a0f57-780d-4362-9964-7c1af566549b",
-   *    "type": "fragment",
-   *    "files": {},
-   *    "owners": [
-   *      {
-   *        "id": "4be1d28d-4973-47bc-8732-16d4a1a735ef",
-   *        "username": "admin"
-   *      }
-   *    ],
-   *    "created": "2018-05-04T10:50:19.053Z",
-   *    "version": 1,
-   *    "metadata": {
-   *      "title": "<p>Test Manuscript Title</p>",
-   *      "abstract": "<p>Test manuscript abstract</p>"
-   *    },
-   *    "fragmentType": "version",
-   *    "authors": [
-   *        {
-   *          "userId": "a6184463-b17a-42f8-b02b-ae1d755cdc6b",
-   *          "isSubmitting": false,
-   *          "isCorresponding": true
-   *        }
-   *      ],
-   *   }
-   * @apiErrorExample {json} Update invitations errors
-   *    HTTP/1.1 403 Forbidden
-   *    HTTP/1.1 400 Bad Request
-   *    HTTP/1.1 404 Not Found
-   *    HTTP/1.1 500 Internal Server Error
-   */
-  app.patch(
-    `${basePath}/:userId`,
-    authBearer,
-    require(`${routePath}/patch`)(app.locals.models),
-  )
 }
 
 module.exports = FragmentsUsers
diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/get.js b/packages/component-user-manager/src/routes/fragmentsUsers/get.js
index 75385a1b5..76ef60416 100644
--- a/packages/component-user-manager/src/routes/fragmentsUsers/get.js
+++ b/packages/component-user-manager/src/routes/fragmentsUsers/get.js
@@ -1,4 +1,4 @@
-const { Team, services } = require('pubsweet-component-helper-service')
+const { services } = require('pubsweet-component-helper-service')
 
 module.exports = models => async (req, res) => {
   // TO DO: add authsome
@@ -9,35 +9,9 @@ module.exports = models => async (req, res) => {
       return res.status(400).json({
         error: `Fragment ${fragmentId} does not match collection ${collectionId}`,
       })
-    const fragment = await models.Fragment.find(fragmentId)
-    if (fragment.authors === undefined) {
-      return res.status(200).json([])
-    }
-    const teamHelper = new Team({ TeamModel: models.Team, fragmentId })
-    const members = await teamHelper.getTeamMembers({
-      role: 'author',
-      objectType: 'fragment',
-    })
-
-    if (members === undefined)
-      return res.status(400).json({
-        error: 'The fragment does not have an author Team',
-      })
-
-    const membersData = members.map(async member => {
-      const user = await models.User.find(member)
-      const matchingAuthor = fragment.authors.find(
-        author => author.id === user.id,
-      )
-      return {
-        ...user,
-        isSubmitting: matchingAuthor.isSubmitting,
-        isCorresponding: matchingAuthor.isCorresponding,
-      }
-    })
 
-    const resBody = await Promise.all(membersData)
-    res.status(200).json(resBody)
+    const { authors = [] } = await models.Fragment.find(fragmentId)
+    return res.status(200).json(authors)
   } catch (e) {
     const notFoundError = await services.handleNotFoundError(e, 'item')
     return res.status(notFoundError.status).json({
diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/patch.js b/packages/component-user-manager/src/routes/fragmentsUsers/patch.js
deleted file mode 100644
index dbdb663f4..000000000
--- a/packages/component-user-manager/src/routes/fragmentsUsers/patch.js
+++ /dev/null
@@ -1,57 +0,0 @@
-const logger = require('@pubsweet/logger')
-
-const { services } = require('pubsweet-component-helper-service')
-
-module.exports = models => async (req, res) => {
-  // TO DO: add authsome
-  const { collectionId, userId, fragmentId } = req.params
-  const {
-    isSubmitting,
-    isCorresponding,
-    firstName,
-    lastName,
-    affiliation,
-  } = req.body
-
-  if (!services.checkForUndefinedParams(isSubmitting, isCorresponding)) {
-    res.status(400).json({ error: 'Missing parameters' })
-    logger.error('some parameters are missing')
-    return
-  }
-
-  try {
-    const collection = await models.Collection.find(collectionId)
-    if (!collection.fragments.includes(fragmentId))
-      return res.status(400).json({
-        error: `Fragment ${fragmentId} does not match collection ${collectionId}`,
-      })
-    let fragment = await models.Fragment.find(fragmentId)
-    if (fragment.authors === undefined) {
-      return res.status(400).json({
-        error: 'Collection does not have any authors',
-      })
-    }
-    const user = await models.User.find(userId)
-    const matchingAuthor = fragment.authors.find(
-      author => author.id === user.id,
-    )
-    if (matchingAuthor === undefined) {
-      return res.status(400).json({
-        error: 'Fragment and user do not match',
-      })
-    }
-    user.firstName = firstName
-    user.lastName = lastName
-    user.affiliation = affiliation
-    await user.save()
-    matchingAuthor.isSubmitting = isSubmitting
-    matchingAuthor.isCorresponding = isCorresponding
-    fragment = await fragment.save()
-    return res.status(200).json(fragment)
-  } catch (e) {
-    const notFoundError = await services.handleNotFoundError(e, 'item')
-    return res.status(notFoundError.status).json({
-      error: notFoundError.message,
-    })
-  }
-}
diff --git a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
index dc60ebd8b..5b6e09a19 100644
--- a/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
+++ b/packages/components-faraday/src/components/AuthorList/AuthorEditor.js
@@ -108,25 +108,15 @@ export default compose(
   reduxForm({
     form: 'edit',
     onSubmit: (
-      values,
+      { edit: newAuthor },
       dispatch,
-      {
-        index,
-        project,
-        version,
-        changeForm,
-        editAuthor,
-        setAuthors,
-        setAuthorEdit,
-      },
+      { version: { authors = [] }, changeForm, setAuthorEdit },
     ) => {
-      const newAuthor = values.edit
-      editAuthor(project.id, version.id, newAuthor.id, newAuthor).then(
-        authors => {
-          setAuthorEdit(-1)()
-          setAuthors(authors)
-        },
+      const newAuthors = authors.map(
+        a => (a.id === newAuthor.id ? newAuthor : a),
       )
+      changeForm('wizard', 'authors', newAuthors)
+      setAuthorEdit(-1)()
     },
   }),
 )(AuthorEdit)
diff --git a/packages/components-faraday/src/redux/authors.js b/packages/components-faraday/src/redux/authors.js
index 34bfe9ce0..d50c86c9d 100644
--- a/packages/components-faraday/src/redux/authors.js
+++ b/packages/components-faraday/src/redux/authors.js
@@ -1,10 +1,6 @@
 import { get } from 'lodash'
-import {
-  create,
-  remove,
-  update,
-  get as apiGet,
-} from 'pubsweet-client/src/helpers/api'
+import { create, remove, get as apiGet } from 'pubsweet-client/src/helpers/api'
+
 import { handleError } from './utils'
 
 // constants
@@ -29,26 +25,6 @@ export const authorSuccess = () => ({
 export const getAuthors = (collectionId, fragmentId) =>
   apiGet(`/collections/${collectionId}/fragments/${fragmentId}/users`)
 
-export const editAuthor = (
-  collectionId,
-  fragmentId,
-  userId,
-  body,
-) => dispatch => {
-  dispatch(authorRequest())
-  return update(
-    `/collections/${collectionId}/fragments/${fragmentId}/users/${userId}`,
-    body,
-  ).then(
-    () =>
-      getAuthors(collectionId, fragmentId).then(authors => {
-        dispatch(authorSuccess())
-        return authors
-      }, handleError(authorFailure, dispatch)),
-    handleError(authorFailure, dispatch),
-  )
-}
-
 export const addAuthor = (author, collectionId, fragmentId) => dispatch => {
   dispatch(authorRequest())
   return create(`/collections/${collectionId}/fragments/${fragmentId}/users`, {
-- 
GitLab