summaryrefslogtreecommitdiff
path: root/scraper/client/paper/paper.address.js
blob: 0da353c9fd3e1fc681558ada9a4c2c485597ec6a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import * as actions from '../actions'

import { Loader, Autocomplete } from '../common'

const initialState = {
  citation: null,
  institution_1: '',
  institution_2: '',
  institution_3: '',
  institution_4: '',
}
class PaperAddress extends Component {
  state = {
    ...initialState
  }

  componentDidMount() {
    const { sha256 } = this.props.match.params
    this.props.actions.getAddress(sha256)
    const citation = this.getCitation(sha256)
    this.setState({ citation })
  }

  componentDidUpdate(oldProps) {
    const { sha256 } = this.props.match.params
    const { address } = this.props.api
    const { sha256: oldSha256 } = oldProps.match.params
    const { address: oldAddress } = oldProps.api
    const oldPaper = oldAddress ? oldAddress.paper : null
    const paper = address ? address.paper : null

    if (oldSha256 && sha256 !== oldSha256) {
      console.log('update address')
      this.props.actions.getAddress(sha256)
      const citation = this.getCitation(sha256)
      this.setState({
        ...initialState,
        citation
      })
    } else if (address && !address.loading && address.paper && (!oldPaper || oldPaper !== address.paper)) {
      if (paper.error) {
        const citation = this.getCitation(sha256)
        this.setState({
          ...initialState,
          citation,
        })
      } else {
        // console.log(paper)
        const citation = this.getCitation(sha256)
        this.setState({
          citation,
          institution_1: paper['Institution #1'],
          institution_2: paper['Institution #2'],
          institution_3: paper['Institution #3'],
          institution_4: paper['Institution #4'],
        })
      }
    } else if (oldProps.api.unknownCitations !== this.props.api.unknownCitations) {
      const citation = this.getCitation(sha256)
      this.setState({ citation })
    }
  }

  getCitation(sha256) {
    const { paperInfo, unknownCitations } = this.props.api
    let citation = (unknownCitations.citations || []).find(f => f.id === sha256)
    if (!citation) {
      citation = (paperInfo.citations || []).find(f => f.id === sha256)
    }
    // console.log(sha256, citation)
    return citation
  }

  save() {
    console.log(this.state)
  }

  render() {
    let { institutions, paperInfo, unknownCitations } = this.props.api
    if (institutions.loading || paperInfo.loading || unknownCitations.loading) return <Loader />
    console.log(this.state)
    console.log(institutions)
    const { citation } = this.state
    if (!citation) {
      return <div>Citation not found in this paper</div>
    }console.log(this)
    return (
      <div className='form'>
        <h3>{citation.title}</h3>
        <div className='gray'>{citation.id}</div>

        <div className='param'>
          <input
            className='vetting'
            type='text'
            placeholder='Paste Institution #1'
            onChange={e => this.setState({
              institution_1_vetting: e.target.value,
            })}
          />
          <Autocomplete
            autoFocus
            placeholder='Institution #1'
            value={this.state.institution_1}
            vetting={this.state.institution_1_vetting}
            onSelect={value => {
              this.setState({ institution_1: value })
              // this.institution_2._el.focus()
            }}
            ref={ref => this.institution_1 = ref}
          />
        </div>

        <div className='param'>
          <input
            className='vetting'
            type='text'
            placeholder='Paste Institution #2'
            onChange={e => this.setState({
              institution_2_vetting: e.target.value,
            })}
          />
          <Autocomplete
            placeholder='Institution #2'
            value={this.state.institution_2}
            onSelect={value => {
              this.setState({ institution_2: value })
            }}
            ref={ref => this.institution_2 = ref}
          />
        </div>

        <div className='param'>
          <input
            className='vetting'
            type='text'
            placeholder='Paste Institution #3'
            onChange={e => this.setState({
              institution_3_vetting: e.target.value,
            })}
          />
          <Autocomplete
            placeholder='Institution #3'
            value={this.state.institution_3}
            onSelect={value => {
              this.setState({ institution_3: value })
              // this.institution_4._el.focus()
            }}
            ref={ref => this.institution_3 = ref}
          />
        </div>

        <div className='param'>
          <input
            className='vetting'
            type='text'
            placeholder='Paste Institution #4'
            onChange={e => this.setState({
              institution_4_vetting: e.target.value,
            })}
          />
          <Autocomplete
            placeholder='Institution #4'
            value={this.state.institution_4}
            onSelect={value => {
              this.setState({ institution_4: value })
              this.button.focus()
            }}
            ref={ref => this.institution_4 = ref}
          />
        </div>

        <button
          className='btn btn-primary'
          onClick={this.save.bind(this)}
          ref={ref => this.button = ref}
        >Save Institutions</button>
        <iframe className='pdfViewer' src={citation.pdf} />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  api: state.api,
})
const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators({ ...actions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(PaperAddress)