From 8919b18b7f41d892ce94782deed24bc69673b429 Mon Sep 17 00:00:00 2001
From: john <johnbarlas39@gmail.com>
Date: Sat, 15 Apr 2017 17:28:20 +0300
Subject: [PATCH] attempt to automate team creation

---
 app/components/Dashboard/Dashboard.jsx        | 66 ++++++++++++++++---
 .../track_change/TrackChangeComponent.js      |  3 +-
 app/components/utils/config.js                |  4 +-
 3 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/app/components/Dashboard/Dashboard.jsx b/app/components/Dashboard/Dashboard.jsx
index 42708c7..61f0ee5 100644
--- a/app/components/Dashboard/Dashboard.jsx
+++ b/app/components/Dashboard/Dashboard.jsx
@@ -1,3 +1,4 @@
+import { each, filter, isEmpty } from 'lodash'
 import Actions from 'pubsweet-client/src/actions'
 import React from 'react'
 import { bindActionCreators } from 'redux'
@@ -6,14 +7,16 @@ import { connect } from 'react-redux'
 import AddBook from './AddBook'
 import BookList from './BookList'
 import DashboardHeader from './DashboardHeader'
+import { teamTypes } from '../utils/config'
 import styles from './dashboard.local.scss'
 
 export class Dashboard extends React.Component {
   constructor (props) {
     super(props)
 
-    this.createCollection = this.createCollection.bind(this)
-    this.removeCollection = this.removeCollection.bind(this)
+    this.createBook = this.createBook.bind(this)
+    this.makeTeamsForBook = this.makeTeamsForBook.bind(this)
+    this.removeBook = this.removeBook.bind(this)
     this.toggleModal = this.toggleModal.bind(this)
 
     this.state = {
@@ -21,13 +24,35 @@ export class Dashboard extends React.Component {
     }
   }
 
+  componentWillMount () {
+    const { getTeams } = this.props.actions
+    getTeams()
+  }
+
+  /*
+    If a book has no teams, it is a new one.
+    Make the default teams for it automatically.
+  */
+  componentDidUpdate () {
+    // console.log('did')
+    const { books, teams } = this.props
+
+    each(books, book => {
+      const teamsForBook = filter(teams, t => {
+        return t.object.id === book.id
+      })
+
+      if (isEmpty(teamsForBook)) this.makeTeamsForBook(book)
+    })
+  }
+
   toggleModal () {
     this.setState({
       showModal: !this.state.showModal
     })
   }
 
-  createCollection (newTitle) {
+  createBook (newTitle) {
     const { createCollection } = this.props.actions
 
     const collection = {
@@ -37,11 +62,32 @@ export class Dashboard extends React.Component {
     createCollection(collection)
   }
 
-  removeCollection (collection) {
+  removeBook (collection) {
     const { deleteCollection } = this.props.actions
     deleteCollection(collection)
   }
 
+  makeTeamsForBook (book) {
+    const { createTeam } = this.props.actions
+
+    each(teamTypes, teamType => {
+      // TODO -- Review the idea that the name needs to be plural for some teams
+      // const name = teamType.name === 'Production Editor' ? teamType.name : teamType.name + 's'
+      //
+      // const newTeam = {
+      //   members: [],
+      //   name: name,
+      //   object: {
+      //     id: book.id,
+      //     type: 'collection'
+      //   },
+      //   teamType: teamType
+      // }
+      //
+      // createTeam(newTeam)
+    })
+  }
+
   render () {
     const { books } = this.props
     const { showModal } = this.state
@@ -58,13 +104,13 @@ export class Dashboard extends React.Component {
           <BookList
             books={books}
             container={this}
-            remove={this.removeCollection}
+            remove={this.removeBook}
           />
         </div>
 
         <AddBook
           container={this}
-          create={this.createCollection}
+          create={this.createBook}
           show={showModal}
           toggle={this.toggleModal}
         />
@@ -74,13 +120,15 @@ export class Dashboard extends React.Component {
 }
 
 Dashboard.propTypes = {
-  books: React.PropTypes.arrayOf(React.PropTypes.object),
-  actions: React.PropTypes.object.isRequired
+  actions: React.PropTypes.object.isRequired,
+  books: React.PropTypes.arrayOf(React.PropTypes.object),  // TODO -- ??
+  teams: React.PropTypes.arrayOf(React.PropTypes.object)
 }
 
 function mapStateToProps (state, { params }) {
   return {
-    books: state.collections
+    books: state.collections,
+    teams: state.teams
   }
 }
 
diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangeComponent.js b/app/components/SimpleEditor/elements/track_change/TrackChangeComponent.js
index 6a0d722..2abef42 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChangeComponent.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChangeComponent.js
@@ -3,7 +3,8 @@ import { AnnotationComponent } from 'substance'
 class TrackChangeComponent extends AnnotationComponent {
 
   didMount () {
-    this.context.editorSession.onUpdate('document', this.onTrackChangesUpdated, this)
+    const { editorSession } = this.context
+    editorSession.onUpdate('document', this.onTrackChangesUpdated, this)
   }
 
   render ($$) {
diff --git a/app/components/utils/config.js b/app/components/utils/config.js
index 59ce4b3..66b4205 100644
--- a/app/components/utils/config.js
+++ b/app/components/utils/config.js
@@ -1,3 +1,5 @@
+const teamTypes = CONFIG.authsome.teams
+
 const chapter = {
   dropdownValues: {
     front: [
@@ -23,4 +25,4 @@ const chapter = {
   }
 }
 
-export { chapter }
+export { chapter, teamTypes }
-- 
GitLab