From 75a25d654dbf08c1908dcab5f6d96c79f852b4b4 Mon Sep 17 00:00:00 2001
From: john <johnbarlas39@gmail.com>
Date: Thu, 20 Apr 2017 01:04:49 +0300
Subject: [PATCH] set the production editor on the collection object

---
 app/components/BookBuilder/BookBuilder.jsx | 62 +++++++++++++---------
 config/universal.js                        |  3 ++
 2 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/app/components/BookBuilder/BookBuilder.jsx b/app/components/BookBuilder/BookBuilder.jsx
index 9c31179..e15c7de 100644
--- a/app/components/BookBuilder/BookBuilder.jsx
+++ b/app/components/BookBuilder/BookBuilder.jsx
@@ -8,53 +8,46 @@ import Division from './Division'
 import Modal from '../utils/Modal'
 import styles from './styles/bookBuilder.local.scss'
 
-// import { fragmentsOfCollection } from 'pubsweet-core/app/helpers/Utils'
-
 export class BookBuilder extends React.Component {
   constructor (props) {
     super(props)
 
     this._toggleTeamManager = this._toggleTeamManager.bind(this)
-    // this.enableActions = false
 
     this._getRoles = this._getRoles.bind(this)
     this._isProductionEditor = this._isProductionEditor.bind(this)
-    this._setProductionEditor = this._setProductionEditor.bind(this)
+    this.setProductionEditor = this.setProductionEditor.bind(this)
 
     this.state = {
       outerContainer: {},
-      productionEditor: null,
       showTeamManager: false
     }
   }
 
   componentWillMount () {
-    const { actions, book } = this.props
-    const { getCollections, getFragments, getTeams, getUsers } = actions
+    const {
+      getCollections,
+      getFragments,
+      getTeams,
+      getUsers
+    } = this.props.actions
 
     getUsers().then(
       () => getTeams()
     ).then(
       () => {
-        this._setProductionEditor()
         return getCollections()
       }
     ).then(
       () => {
-        if (book.id) getFragments(book)
+        const { book } = this.props
+
+        this.setProductionEditor()
+        getFragments(book)
       }
     )
   }
 
-  componentWillReceiveProps (nextProps) {
-    const { actions, book } = nextProps
-    const { getFragments } = actions
-
-    if (book.id && book.id !== this.props.book.id) {
-      getFragments(book)
-    }
-  }
-
   componentDidMount () {
     // I'm using the ref inside the render function it was created in
     // So it won't be available until didMount
@@ -62,9 +55,9 @@ export class BookBuilder extends React.Component {
     this.setState({ outerContainer: this.refs.outerContainer })
   }
 
-  // temporary HACK to add production editor to collection
-  _setProductionEditor () {
-    const { book, teams, users } = this.props
+  setProductionEditor () {
+    const { actions, book, teams, users } = this.props
+    const { updateCollection } = actions
 
     const productionEditorsTeam = _.find(teams, function (t) {
       return t.teamType.name === 'Production Editor' && t.object.id === book.id
@@ -76,9 +69,28 @@ export class BookBuilder extends React.Component {
       return _.includes(productionEditorsTeam.members, u.id)
     })
 
-    if (_.isEmpty(productionEditors)) return
+    let patch
+
+    if (_.isEmpty(productionEditors)) {
+      patch = {
+        id: book.id,
+        productionEditor: null
+      }
+
+      return updateCollection(patch)
+    }
+
+    const currentEditor = book.productionEditor
+    const foundEditor = productionEditors[0] || null
+
+    if (currentEditor === foundEditor) return
+
+    patch = {
+      id: book.id,
+      productionEditor: _.pick(foundEditor, ['id', 'username'])
+    }
 
-    this.setState({ productionEditor: productionEditors[0].username })
+    updateCollection(patch)
   }
 
   _toggleTeamManager () {
@@ -158,7 +170,7 @@ export class BookBuilder extends React.Component {
       )
     }
 
-    const productionEditor = this.state.productionEditor || 'unassigned'
+    const productionEditor = _.get(book, 'productionEditor.username') || 'unassigned'
 
     return (
       <div className='bootstrap modal pubsweet-component pubsweet-component-scroll'>
@@ -170,7 +182,7 @@ export class BookBuilder extends React.Component {
             <h1>{this.props.book.title}</h1>
 
             <div className={styles.productionEditorContainer}>
-              <span>Production Editor: &nbsp; {productionEditor} </span>
+              <span>Production Editor: &nbsp; { productionEditor } </span>
               {teamManagerButton}
               <div className={styles.separator} />
             </div>
diff --git a/config/universal.js b/config/universal.js
index b78bdb0..56887ff 100644
--- a/config/universal.js
+++ b/config/universal.js
@@ -30,6 +30,9 @@ module.exports = {
   },
   theme: 'ThemeEditoria',
   validations: {
+    collection: {
+      productionEditor: Joi.object().allow(null)
+    },
     fragment: {
       alignment: Joi.object(),
       author: Joi.string().allow(''),
-- 
GitLab