Skip to content
Snippets Groups Projects
Commit df064f27 authored by john's avatar john
Browse files

automate team creation and deletion for books

parent 8919b18b
No related branches found
No related tags found
No related merge requests found
......@@ -15,8 +15,10 @@ export class Dashboard extends React.Component {
super(props)
this.createBook = this.createBook.bind(this)
this.makeTeamsForBook = this.makeTeamsForBook.bind(this)
this.createTeamsForBook = this.createTeamsForBook.bind(this)
this.findBooksWithNoTeams = this.findBooksWithNoTeams.bind(this)
this.removeBook = this.removeBook.bind(this)
this.removeTeamsForBook = this.removeTeamsForBook.bind(this)
this.toggleModal = this.toggleModal.bind(this)
this.state = {
......@@ -24,67 +26,123 @@ 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.
Get books and teams.
Make sure all books have teams associated with them.
*/
componentDidUpdate () {
// console.log('did')
const { books, teams } = this.props
each(books, book => {
const teamsForBook = filter(teams, t => {
return t.object.id === book.id
})
componentWillMount () {
const { actions } = this.props
const { getCollections, getTeams } = actions
if (isEmpty(teamsForBook)) this.makeTeamsForBook(book)
})
getCollections().then(
() => getTeams()
).then(
() => this.findBooksWithNoTeams()
)
}
/*
Toggle showing 'add book' modal
*/
toggleModal () {
this.setState({
showModal: !this.state.showModal
})
}
/*
Create a new book with the given title.
Once you have the new book's db id, make the teams for it as well.
*/
createBook (newTitle) {
const { createCollection } = this.props.actions
const collection = {
const book = {
title: newTitle || 'Untitled'
}
createCollection(collection)
createCollection(book).then(res => {
const createdBook = res.collection
this.createTeamsForBook(createdBook)
})
}
removeBook (collection) {
/*
Remove the given book.
Also remove all teams associated with it.
*/
removeBook (book) {
const { deleteCollection } = this.props.actions
deleteCollection(collection)
deleteCollection(book).then(res => {
this.removeTeamsForBook(book)
})
}
makeTeamsForBook (book) {
/*
Find all books that have no teams associated with them.
If one is found, make the teams for it.
This will most likely only happen once:
# The first time the app is run, on the collection created by
# the command 'pubsweet setupdb'.
*/
// TODO -- refactor so that less operations run most of the time
findBooksWithNoTeams () {
const { books, teams } = this.props
each(books, book => {
const teamsForBook = filter(teams, t => {
return t.object.id === book.id
})
if (isEmpty(teamsForBook)) {
this.createTeamsForBook(book)
}
})
}
/*
Create the teams found in the config file for the given book.
This should run either when a new book is created,
or when a book with no teams associated with it is found.
*/
createTeamsForBook (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)
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)
})
}
/*
Delete all teams associated with the given book.
This should only run after a book is deleted.
*/
removeTeamsForBook (book) {
const { actions, teams } = this.props
const { deleteTeam } = actions
const teamsToDelete = filter(teams, team => {
return team.object.id === book.id
})
each(teamsToDelete, team => {
deleteTeam(team)
})
}
......
/* global CONFIG */
const teamTypes = CONFIG.authsome.teams
const chapter = {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment