BookBuilder.jsx 9.01 KB
Newer Older
Yannis Barlas's avatar
Yannis Barlas committed
1
2
3
4
5
6
7
import _ from 'lodash'
import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

// TODO -- clean up this import
import Actions from 'pubsweet-client/src/actions'
8

Yannis Barlas's avatar
Yannis Barlas committed
9
import Division from './Division'
10
import FileUploader from './FileUploader/FileUploader'
Yannis Barlas's avatar
Yannis Barlas committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import TeamManagerModal from './TeamManager/TeamManagerModal'

import styles from './styles/bookBuilder.local.scss'

// TODO -- this doesn't work if imported in the css files. why?
import './styles/fontAwesome.scss'

export class BookBuilder extends React.Component {
  constructor (props) {
    super(props)

    this._toggleTeamManager = this._toggleTeamManager.bind(this)

    this._getRoles = this._getRoles.bind(this)
    this._isProductionEditor = this._isProductionEditor.bind(this)
    this.setProductionEditor = this.setProductionEditor.bind(this)
27
    this.updateUploadStatus = this.updateUploadStatus.bind(this)
28
    this.handleHTMLToEpub = this.handleHTMLToEpub.bind(this)
Yannis Barlas's avatar
Yannis Barlas committed
29
30
31

    this.state = {
      outerContainer: {},
32
33
      showTeamManager: false,
      uploading: {}
Yannis Barlas's avatar
Yannis Barlas committed
34
35
36
37
    }
  }

  componentWillMount () {
38
39
40
41
42
43
    const { getCollections, getFragments, getTeams, getUsers } = this.props.actions

    getUsers()
      .then(() => getTeams())
      .then(() => getCollections())
      .then(() => {
Yannis Barlas's avatar
Yannis Barlas committed
44
45
46
47
        const { book } = this.props

        this.setProductionEditor()
        getFragments(book)
48
      })
Yannis Barlas's avatar
Yannis Barlas committed
49
50
51
52
53
54
55
56
57
58
59
60
61
  }

  componentDidMount () {
    // I'm using the ref inside the render function it was created in
    // So it won't be available until didMount
    // Pass it to the state and use it safely (no undefined scenarios)
    this.setState({ outerContainer: this.refs.outerContainer })
  }

  setProductionEditor () {
    const { actions, book, teams, users } = this.props
    const { updateCollection } = actions

62
63
64
65
    const productionEditorsTeam = _.find(
      teams,
      t => t.teamType.name === 'Production Editor' && t.object.id === book.id
    )
Yannis Barlas's avatar
Yannis Barlas committed
66
67
68

    if (!productionEditorsTeam) return

69
    const productionEditors = _.filter(users, u => _.includes(productionEditorsTeam.members, u.id))
Yannis Barlas's avatar
Yannis Barlas committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    let patch

    if (_.isEmpty(productionEditors)) {
      // production editor is already set to null
      if (book.productionEditor === null) return

      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'])
    }

    updateCollection(patch)
  }

  _toggleTeamManager () {
    this.setState({ showTeamManager: !this.state.showTeamManager })
  }

  _getRoles () {
    const { user, book } = this.props

105
    const teams = _.filter(user.teams, t => t.object.id === book.id)
Yannis Barlas's avatar
Yannis Barlas committed
106
107
108
109
110
111
112
113

    let roles = []
    if (user.admin) roles.push('admin')

    function addRole (role) {
      roles = _.union(roles, [role])
    }

114
    _.forEach(teams, (t) => {
Yannis Barlas's avatar
Yannis Barlas committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
      switch (t.teamType.name) {
        case 'Production Editor':
          addRole('production-editor')
          break
        case 'Copy Editor':
          addRole('copy-editor')
          break
        case 'Author':
          addRole('author')
          break
      }
    })

    return roles
  }

  _isProductionEditor () {
    const userRoles = this._getRoles()
    const accepted = ['production-editor', 'admin']
134
    const pass = _.some(accepted, role => _.includes(userRoles, role))
Yannis Barlas's avatar
Yannis Barlas committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    return pass
  }

  renderTeamManagerModal () {
    if (!this._isProductionEditor()) return null

    const { outerContainer, showTeamManager } = this.state

    if (!showTeamManager) return null
    if (_.isEmpty(outerContainer)) return null

    const { actions, teams, users } = this.props
    const { updateTeam } = actions

    return (
      <TeamManagerModal
        container={outerContainer}
        show={showTeamManager}
        teams={teams}
        toggle={this._toggleTeamManager}
        updateTeam={updateTeam}
        users={users}
      />
    )
  }

161
162
163
164
  updateUploadStatus (status) {
    this.setState({ uploading: status })
  }

165
166
  handleHTMLToEpub (bookId) {
    const { book } = this.props
167
168
    const { htmlToEpub } = this.props.actions

169
    htmlToEpub(book.id).then((res) => {
170
171
172
173
174
      const path = res.extractedEpubPath
      const url = `
        /vivliostyle/viewer/vivliostyle-viewer.html#b=/uploads/${path}
      `
      window.open(url, '_blank')
175
176
177
    })
  }

Yannis Barlas's avatar
Yannis Barlas committed
178
179
180
181
182
183
  render () {
    const { book, chapters } = this.props
    const { createFragment, deleteFragment, ink, updateFragment } = this.props.actions
    const { outerContainer } = this.state
    const roles = this._getRoles()

184
185
186
    const frontChapters = []
    const bodyChapters = []
    const backChapters = []
Yannis Barlas's avatar
Yannis Barlas committed
187

188
    _.forEach(chapters, (c) => {
Yannis Barlas's avatar
Yannis Barlas committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
      switch (c.division) {
        case 'front':
          frontChapters.push(c)
          break
        case 'body':
          bodyChapters.push(c)
          break
        case 'back':
          backChapters.push(c)
          break
      }
    })

    const isProductionEditor = this._isProductionEditor()
    let teamManagerButton = ''
    if (isProductionEditor) {
      teamManagerButton = (
chris's avatar
chris committed
206
207
208
209
210
211
        <span onClick={this._toggleTeamManager}>
          <div className={styles.teamManagerIcon} />
          <div className={styles.teamManagerBtn}>
            <a>team manager</a>
          </div>
        </span>
Yannis Barlas's avatar
Yannis Barlas committed
212
213
214
215
216
217
      )
    }

    const productionEditor = _.get(book, 'productionEditor.username') || 'unassigned'
    const teamManagerModal = this.renderTeamManagerModal()

218
219
    // console.log('render bb')

Yannis Barlas's avatar
Yannis Barlas committed
220
221
222
    return (
      <div className='bootstrap modal pubsweet-component pubsweet-component-scroll'>
        <div className={styles.bookBuilder}>
Yannis Barlas's avatar
Yannis Barlas committed
223
          <button onClick={this.handleHTMLToEpub}> Do it </button>
Yannis Barlas's avatar
Yannis Barlas committed
224
225
          <div
            className='col-lg-offset-2 col-lg-8 col-md-8 col-sm-12 col-xs-12'
226
227
            ref='outerContainer'
          >
Yannis Barlas's avatar
Yannis Barlas committed
228
            <div className={styles.productionEditorContainer}>
229
              <span>Production Editor: &nbsp; {productionEditor} </span>
chris's avatar
chris committed
230
231
232
              {teamManagerButton}
              <div className={styles.separator} />
            </div>
Yannis Barlas's avatar
Yannis Barlas committed
233

chris's avatar
chris committed
234
            <h1 className={styles.bookTitle}>{this.props.book.title}</h1>
chris's avatar
chris committed
235

236
237
238
239
240
241
242
243
244
245
            <FileUploader
              backChapters={backChapters}
              bodyChapters={bodyChapters}
              book={book}
              convert={ink}
              create={createFragment}
              frontChapters={frontChapters}
              update={updateFragment}
              updateUploadStatus={this.updateUploadStatus}
            />
Yannis Barlas's avatar
Yannis Barlas committed
246
247
248
249
250
251
252
253
254

            <Division
              add={createFragment}
              book={book}
              chapters={frontChapters}
              ink={ink}
              outerContainer={outerContainer}
              remove={deleteFragment}
              roles={roles}
chris's avatar
chris committed
255
              title='Frontmatter'
Yannis Barlas's avatar
Yannis Barlas committed
256
257
              type='front'
              update={updateFragment}
258
              uploadStatus={this.state.uploading}
Yannis Barlas's avatar
Yannis Barlas committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
            />

            <div className={styles.sectionDivider} />

            <Division
              add={createFragment}
              book={book}
              chapters={bodyChapters}
              ink={ink}
              outerContainer={outerContainer}
              remove={deleteFragment}
              roles={roles}
              title='Body'
              type='body'
              update={updateFragment}
274
              uploadStatus={this.state.uploading}
Yannis Barlas's avatar
Yannis Barlas committed
275
276
277
278
279
280
281
282
283
284
285
286
            />

            <div className={styles.sectionDivider} />

            <Division
              add={createFragment}
              book={book}
              chapters={backChapters}
              ink={ink}
              outerContainer={outerContainer}
              remove={deleteFragment}
              roles={roles}
chris's avatar
chris committed
287
              title='Backmatter'
Yannis Barlas's avatar
Yannis Barlas committed
288
289
              type='back'
              update={updateFragment}
290
              uploadStatus={this.state.uploading}
Yannis Barlas's avatar
Yannis Barlas committed
291
292
293
294
            />
          </div>
        </div>

295
        {teamManagerModal}
Yannis Barlas's avatar
Yannis Barlas committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
      </div>
    )
  }
}

BookBuilder.propTypes = {
  actions: React.PropTypes.object.isRequired,
  book: React.PropTypes.object.isRequired,
  chapters: React.PropTypes.array.isRequired,
  // error: React.PropTypes.string,
  teams: React.PropTypes.array,
  users: React.PropTypes.array,
  user: React.PropTypes.object
  // userRoles: React.PropTypes.array,
}

function mapStateToProps (state, ownProps) {
313
  const book = _.find(state.collections, c => c.id === ownProps.params.id)
Yannis Barlas's avatar
Yannis Barlas committed
314

315
316
317
318
  const chapters = _.sortBy(
    _.filter(state.fragments, f => f.book === book.id && f.id && !f.deleted),
    'index'
  )
Yannis Barlas's avatar
Yannis Barlas committed
319

320
321
  // console.log(chapters)

322
323
324
  const teams = state.teams
  const users = state.users.users
  const user = state.currentUser.user
Yannis Barlas's avatar
Yannis Barlas committed
325

326
  const error = state.error
Yannis Barlas's avatar
Yannis Barlas committed
327
328
329

  return {
    book: book || {},
330
331
332
333
    chapters,
    teams,
    users,
    user,
Yannis Barlas's avatar
Yannis Barlas committed
334
335
336
337
338
339
340
341
342
343
344
    // userRoles: state.auth.roles,
    errorMessage: error
  }
}

function mapDispatchToProps (dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch)
  }
}

345
export default connect(mapStateToProps, mapDispatchToProps)(BookBuilder)