summaryrefslogtreecommitdiff
path: root/client/table/citations.table.js
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-04-01 19:50:48 +0200
committeradamhrv <adam@ahprojects.com>2019-04-01 19:50:48 +0200
commit54a9853e9dce4a2c00208c80e49b6d6497d50886 (patch)
treef589646658c17ff6c2870fe008d508daf6b04118 /client/table/citations.table.js
parent79ca5c75243e4d94a7924d1bda8666123f398d9c (diff)
parent83858f95425278d44e7f39177e141e7b82c0022c (diff)
Merge branch 'master' of github.com:adamhrv/megapixels_dev
Diffstat (limited to 'client/table/citations.table.js')
-rw-r--r--client/table/citations.table.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/client/table/citations.table.js b/client/table/citations.table.js
new file mode 100644
index 00000000..f9599f5d
--- /dev/null
+++ b/client/table/citations.table.js
@@ -0,0 +1,107 @@
+import React, { Component } from 'react'
+import { bindActionCreators } from 'redux'
+import { connect } from 'react-redux'
+import { ReactTabulator } from 'react-tabulator'
+import MultiValueFormatter from "react-tabulator/lib/formatters/MultiValueFormatter"
+
+import { Loader } from '../common'
+import { toArray, toTuples } from '../util'
+
+export const citationsColumns = [
+ { title: 'Title', field: 'title', sorter: 'string' },
+ { title: 'Institution', field: 'institution', sorter: 'string', },
+ { title: 'Country', field: 'country', sorter: 'string', width: 140, },
+ { title: 'Year', field: 'year', sorter: 'number', width: 70 },
+ { title: 'PDF', field: 'pdf_text', formatter: 'link',
+ formatterParams: { target: "_blank", urlField: 'pdf_link', },
+ sorter: 'string', width: 100 },
+]
+
+class CitationsTable extends Component {
+ state = {
+ q: '',
+ formattedCitations: [],
+ filteredCitations: [],
+ }
+
+ componentDidMount(){
+ this.updateCitations()
+ }
+ componentDidUpdate(oldProps){
+ if (this.props.payload.data.citations !== oldProps.payload.data.citations) {
+ this.updateCitations()
+ }
+ }
+ updateCitations(){
+ const { paper, citations } = this.props.payload.data
+ if (!citations.length) this.setState({ formattedCitations: [] })
+ console.log(citations.filter(a => a.title.match('Coarse')))
+ const formattedCitations = citations.sort((a,b) => a.title.localeCompare(b.title)).map(citation => {
+ const pdf_link = (citation.pdf && citation.pdf.length)
+ ? citation.pdf[0]
+ : (citation.doi && citation.doi.length)
+ ? citation.doi[0]
+ : 'https://www.semanticscholar.org/paper/' + citation.id
+ let pdf_text
+ const pdf_partz = pdf_link.split('/')[2].split('.')
+ if (pdf_partz.length > 2 && pdf_partz[pdf_partz.length - 2].length == 2) {
+ pdf_text = pdf_partz.slice(-3).join('.')
+ } else {
+ pdf_text = pdf_partz.slice(-2).join('.')
+ }
+ return {
+ title: citation.title,
+ institution: citation.addresses.map(a => a.name).sort().join('; '),
+ country: Array.from(new Set(citation.addresses.map(a => a.country))).sort().join('; '),
+ year: citation.year,
+ pdf_link, pdf_text,
+ }
+ })
+ this.setState({
+ formattedCitations,
+ filteredCitations: formattedCitations,
+ })
+ }
+
+ updateFilter(q) {
+ const { formattedCitations } = this.state
+ if (!q.length) {
+ this.setState({ q, filteredCitations: formattedCitations })
+ } else {
+ let q_re = new RegExp('(' + q.replace(/\s+/g, ' ').trim().replace(' ', '|') + ')', 'gi')
+ let filteredCitations = formattedCitations.filter(citation => (
+ citation.title.match(q_re) ||
+ citation.institution.match(q_re) ||
+ citation.country.match(q_re)
+ ))
+ this.setState({ q, filteredCitations })
+ }
+ }
+
+ render() {
+ const { formattedCitations, filteredCitations } = this.state
+ if (!formattedCitations.length) return <Loader />
+ return (
+ <div className='citationBrowser'>
+ <input
+ type="text"
+ value={this.state.q}
+ onChange={e => this.updateFilter(e.target.value)}
+ className='q'
+ placeholder='Enter text to search citations...'
+ />
+ <ReactTabulator
+ columns={citationsColumns}
+ data={filteredCitations}
+ options={{
+ height: 311,
+ layout: 'fitColumns',
+ placeholder: formattedCitations.length ? '' : 'Nothing matches your query',
+ }}
+ />
+ </div>
+ )
+ }
+}
+
+export default CitationsTable