import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as actions from '../actions'
import { USES_DATASET, ADDRESS_TYPE_ENUM } from '../types'
import { Loader } from '../common'
class PaperManager extends Component {
componentDidMount() {
this.props.actions.getPaperInfo(this.props.match.params.key)
}
componentDidUpdate(oldProps) {
if (this.props.match.params.key !== oldProps.match.params.key) {
this.props.actions.getPaperInfo(this.props.match.params.key)
}
if (this.props.api.paperInfo.dataset !== oldProps.api.paperInfo.dataset && this.props.api.paperInfo.dataset && this.props.api.paperInfo.dataset.key) {
this.props.actions.getVerificationsDataset(this.props.api.paperInfo.dataset.key)
}
if (this.props.api.verifications !== oldProps.api.verifications && this.props.api.paperInfo.dataset) {
this.updateSortedCitations()
}
}
updateSortedCitations() {
const { api } = this.props
const { paperInfo, unknownCitations, verifications } = api
const { dataset } = paperInfo
if (!dataset || !paperInfo.citations || !unknownCitations.citations || !verifications[dataset.key]) {
this.props.actions.setSortedCitations([])
return
}
const citations = paperInfo.citations.concat(unknownCitations.citations)
let verifiedLookup = verifications[dataset.key] || {}
// first sort by verification status,
// then by a combination of its geolocated sources and paper count.
// penalize papers with no PDF
const sortedCitations = citations
.map(citation => [
citation.title,
verifiedLookup[citation.id]
? verifiedLookup[citation.id].uses_dataset
: USES_DATASET.NO_DATA,
(citation.addresses || [])
.map(address => (ADDRESS_TYPE_ENUM[address.type] || 0))
.reduce((a, b) => (a + b), 0)
+ (citation.pdf.length > 0
? Math.max(citation.pdf.length, 2)
: -2
),
citation
])
.sort((a, b) => (b[1] - a[1] || b[2] - a[2] || (a[0].localeCompare(b[0]))))
.map(tup => ({
...tup[3],
verified: tup[1],
}))
console.log('updated')
this.props.actions.setSortedCitations(sortedCitations)
}
render() {
if (!this.props.match.params.key) return null
if (this.props.api.paperInfo.loading) return