|
1 | | -import { graphql, useStaticQuery } from 'gatsby'; |
| 1 | +import { graphql, StaticQuery } from 'gatsby'; |
2 | 2 | import React from 'react'; |
3 | 3 | import PapersFilter from './filter'; |
4 | 4 | import Paper from './paper'; |
5 | 5 |
|
6 | | -const papersQuery = graphql` |
7 | | - query { |
8 | | - allRdf( |
9 | | - filter: { |
10 | | - data: { |
11 | | - rdf_type: { |
12 | | - elemMatch: { |
13 | | - id: { eq: "https://schema.dice-research.org/Publication" } |
| 6 | +const PapersList = ({ name, publicationTag, path }) => ( |
| 7 | + <StaticQuery |
| 8 | + query={graphql` |
| 9 | + query PersonPublications { |
| 10 | + allRdf( |
| 11 | + filter: { |
| 12 | + data: { |
| 13 | + rdf_type: { |
| 14 | + elemMatch: { |
| 15 | + id: { eq: "https://schema.dice-research.org/Publication" } |
| 16 | + } |
| 17 | + } |
14 | 18 | } |
15 | 19 | } |
16 | | - } |
17 | | - } |
18 | | - ) { |
19 | | - edges { |
20 | | - node { |
21 | | - data { |
22 | | - type |
23 | | - title |
24 | | - publicationType |
25 | | - year |
26 | | - source |
27 | | - url |
28 | | - tag |
29 | | - bibsonomyId |
30 | | - author { |
| 20 | + sort: { fields: [data___year], order: DESC } |
| 21 | + ) { |
| 22 | + edges { |
| 23 | + node { |
31 | 24 | id |
32 | | - path |
33 | 25 | data { |
34 | | - name |
| 26 | + type |
| 27 | + title |
| 28 | + publicationType |
| 29 | + year |
| 30 | + source |
| 31 | + tag |
| 32 | + url |
| 33 | + pdfUrl |
| 34 | + doi |
| 35 | + presentationUrl |
| 36 | + videoUrl |
| 37 | + bibsonomyId |
| 38 | + author { |
| 39 | + id |
| 40 | + path |
| 41 | + } |
| 42 | + authorName |
35 | 43 | } |
36 | 44 | } |
37 | | - authorName |
38 | 45 | } |
39 | | - id |
40 | 46 | } |
41 | 47 | } |
42 | | - } |
43 | | - } |
44 | | -`; |
| 48 | + `} |
| 49 | + render={({ allRdf: { edges } }) => { |
| 50 | + const normName = (name || '').trim().toLowerCase(); |
45 | 51 |
|
46 | | -export default ({ name, publicationTag, path }) => { |
47 | | - const { |
48 | | - allRdf: { edges: allPapers }, |
49 | | - } = useStaticQuery(papersQuery); |
| 52 | + const matches = edges.filter(({ node }) => { |
| 53 | + const d = node.data || {}; |
| 54 | + const byPath = (d.author || []).some(a => a.path === path); |
| 55 | + const byExactName = (d.authorName || []).some( |
| 56 | + n => (n || '').toLowerCase() === normName |
| 57 | + ); |
| 58 | + const byTag = publicationTag |
| 59 | + ? (d.tag || []).includes(publicationTag) |
| 60 | + : false; |
| 61 | + return byPath || byExactName || byTag; |
| 62 | + }); |
50 | 63 |
|
51 | | - const papers = allPapers.filter( |
52 | | - ({ |
53 | | - node: { |
54 | | - data: { tag, authorName, author }, |
55 | | - }, |
56 | | - }) => { |
57 | | - const hasTag = |
58 | | - publicationTag && |
59 | | - tag && |
60 | | - tag.length > 0 && |
61 | | - tag.find( |
62 | | - t => |
63 | | - t.localeCompare(publicationTag, 'en', { sensitivity: 'base' }) === 0 |
64 | | - ) !== undefined; |
65 | | - const hasAuthorByName = |
66 | | - name && |
67 | | - authorName && |
68 | | - authorName.length > 0 && |
69 | | - authorName.find( |
70 | | - n => n.localeCompare(name, 'en', { sensitivity: 'base' }) === 0 |
71 | | - ) !== undefined; |
72 | | - const hasAuthorByPath = author && author.find(a => a.path === path); |
73 | | - return hasTag || hasAuthorByName || hasAuthorByPath; |
74 | | - } |
75 | | - ); |
| 64 | + const uniqueEdges = Array.from( |
| 65 | + new Map(matches.map(e => [e.node.id, e])).values() |
| 66 | + ); |
76 | 67 |
|
77 | | - return ( |
78 | | - <PapersFilter limit={5} edges={papers}> |
79 | | - {papers => |
80 | | - papers.length === 0 ? ( |
81 | | - <div className="paper"> |
82 | | - <h3 className="paper-name">No papers found</h3> |
83 | | - </div> |
84 | | - ) : ( |
85 | | - papers.map(({ node }) => <Paper key={node.id} data={node.data} />) |
86 | | - ) |
87 | | - } |
88 | | - </PapersFilter> |
89 | | - ); |
90 | | -}; |
| 68 | + if (uniqueEdges.length === 0) return <p>No publications yet.</p>; |
| 69 | + |
| 70 | + return ( |
| 71 | + <PapersFilter edges={uniqueEdges} limit={5}> |
| 72 | + {papers => |
| 73 | + papers.map(({ node }) => <Paper key={node.id} data={node.data} />) |
| 74 | + } |
| 75 | + </PapersFilter> |
| 76 | + ); |
| 77 | + }} |
| 78 | + /> |
| 79 | +); |
| 80 | + |
| 81 | +export default PapersList; |
0 commit comments