summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/paragraph/containers/paragraphList.container.js
blob: 059baffa8e572fe034d4e3b6ddfead6bb026befc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import actions from '../../../actions'
import { ParagraphElementLookup } from '../components/paragraph.types'

class ParagraphList extends Component {
  state = {
    paragraphs: [],
  }
  constructor(props) {
    super(props)
    this.onAnnotationClick = this.onAnnotationClick.bind(this)
    this.onParagraphDoubleClick = this.onParagraphDoubleClick.bind(this)
  }
  componentDidMount() {
    this.build()
  }
  build() {
    const { order: annotationOrder, lookup: annotationLookup } = this.props.annotation
    const { lookup: paragraphLookup } = this.props.paragraph
    let currentParagraph = {}
    const paragraphs = []
    annotationOrder.forEach((annotation_id, i) => {
      const annotation = annotationLookup[annotation_id]
      const paragraph = paragraphLookup[annotation.paragraph_id]
      if (annotation.paragraph_id !== currentParagraph.id) {
        const paragraph_type = getParagraphType(annotation, paragraph)
        currentParagraph = {
          id: annotation.paragraph_id || ('index_' + i),
          type: paragraph_type,
          annotations: [],
        }
        paragraphs.push(currentParagraph)
      }
      currentParagraph.annotations.push(annotation)
    })
    this.setState({ paragraphs })
  }
  onAnnotationClick(e, paragraph, annotation){
    //
  }
  onParagraphDoubleClick(e, paragraph) {
    //
  }
  render() {
    const { paragraphs } = this.state
    return (
      <div className='paragraphs'>
        <div className='content'>
          {paragraphs.map(paragraph => {
            if (paragraph.type in ParagraphElementLookup) {
              const ParagraphElement = ParagraphElementLookup[paragraph.type]
              return (
                <ParagraphElement
                  key={paragraph.id}
                  paragraph={paragraph}
                  selectedParagraph={false}
                  selectedAnnotation={-1}
                  onAnnotationClick={this.onAnnotationClick}
                  onDoubleClick={this.onParagraphDoubleClick}
                />
              )
            } else {
              return <div key={paragraph.id}>{'(empty)'}</div>
            }
          })}
        </div>
      </div>
    )
  }
}

const getParagraphType = (annotation, paragraph) => {
  if (!paragraph) {
    return annotation.type
  }
  return paragraph.type
}

const mapStateToProps = state => ({
  paragraph: state.paragraph.index,
  annotation: state.annotation.index,
  audio: state.audio,
})

const mapDispatchToProps = dispatch => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(ParagraphList)