Skip to content

Commit

Permalink
Merge pull request #274 from datacite/fair-vis-feature-2
Browse files Browse the repository at this point in the history
Fair vis feature 2
  • Loading branch information
jrhoads authored Aug 18, 2023
2 parents 8f3ee7f + 2846fc8 commit f15bbd7
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/react-html-parser": "^2.0.1",
"@types/testing-library__cypress": "^5.0.5",
"@zeit/next-source-maps": "0.0.4-canary.1",
"csv-stringify": "^6.4.0",
"date-fns": "^2.22.1",
"flagged": "^2.0.1",
"geolib": "^3.3.1",
Expand Down
72 changes: 72 additions & 0 deletions src/components/DownloadReports/DownloadReports.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react'
import { Row, Col } from 'react-bootstrap'
import HelpIcon from '../HelpIcon/HelpIcon'

type Props = {
url: string
title?: string
variables: {
id: string,
gridId: string,
crossrefFunderId: string,
cursor: string,
filterQuery: string,
published: string,
resourceTypeId: string,
fieldOfScience: string,
language: string,
license: string,
registrationAgency: string
}
}

const DownloadReports: React.FunctionComponent<Props> = ({ variables}) => {

const filteredVariables = Object.fromEntries(Object.entries(variables).filter(([, value]) => value))
const params = new URLSearchParams(filteredVariables).toString()

const apiurlBase = '/api/download-reports'


const downloadReports = () => {
return (
<div className="panel panel-transparent download-reports">
<div className="panel-body">
<Row>
<Col className="download-list" id="full-metadata" xs={12}>
<div id="download-related-works">
<a
rel="noreferrer"
href={`${apiurlBase}/related-works?${params}`}
download
>
Related Works (CSV)
</a>
<HelpIcon text='Includes descriptions and formatted citations in APA style for up to 200 DOIs associated with this organization.' size={20} position='inline' />
</div>
<div id="download-funders" className="download">
<a
rel="noreferrer"
href={`${apiurlBase}/funders?${params}`}
download
>
Funders (CSV)
</a>
<HelpIcon text='Includes up to 200 funders associated with related works.' size={20} position='inline' />
</div>
</Col>
</Row>
</div>
</div>
)
}

return (
<>
<h3 className="member-results">Download Reports</h3>
{downloadReports()}
</>
)
}

export default DownloadReports
74 changes: 74 additions & 0 deletions src/pages/api/download-reports/funders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { NextApiRequest, NextApiResponse } from "next"
import { gql } from '@apollo/client';
import apolloClient from '../../../utils/apolloClient'
import { stringify } from 'csv-stringify/sync'

const QUERY = gql`
query getOrganizationQuery(
$id: ID
$gridId: ID
$crossrefFunderId: ID
$cursor: String
$filterQuery: String
$published: String
$resourceTypeId: String
$fieldOfScience: String
$language: String
$license: String
$registrationAgency: String
) {
organization(
id: $id
gridId: $gridId
crossrefFunderId: $crossrefFunderId
) {
works(
first: 0
after: $cursor
query: $filterQuery
published: $published
resourceTypeId: $resourceTypeId
fieldOfScience: $fieldOfScience
language: $language
license: $license
registrationAgency: $registrationAgency
facetCount: 200
) {
funders {
id
title
count
}
}
}
}
`


export default async function downloadReportsHandler(
req: NextApiRequest,
res: NextApiResponse
) {
const variables = req.query

const { data } = await apolloClient.query({
query: QUERY,
variables: variables
})


const csv = stringify(data.organization.works.funders, {
header: true,
columns: [ { key: 'id', header: 'Funder ID' }, { key: 'title', header: 'Title' }, { key: 'count', header: 'Work Count' } ]
})


try {
res.status(200)
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', `attachment; filename="funders_${variables.id}.csv"`)
res.send(csv)
} catch (error) {
res.status(400).json({ error })
}
}
90 changes: 90 additions & 0 deletions src/pages/api/download-reports/related-works.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { NextApiRequest, NextApiResponse } from "next"
import { gql } from '@apollo/client';
import apolloClient from '../../../utils/apolloClient'
import { stringify } from 'csv-stringify/sync'

const QUERY = gql`
query getOrganizationQuery(
$id: ID
$gridId: ID
$crossrefFunderId: ID
$cursor: String
$filterQuery: String
$published: String
$resourceTypeId: String
$fieldOfScience: String
$language: String
$license: String
$registrationAgency: String
) {
organization(
id: $id
gridId: $gridId
crossrefFunderId: $crossrefFunderId
) {
works(
first: 200
after: $cursor
query: $filterQuery
published: $published
resourceTypeId: $resourceTypeId
fieldOfScience: $fieldOfScience
language: $language
license: $license
registrationAgency: $registrationAgency
) {
nodes {
...WorkFragment
}
}
}
}
fragment WorkFragment on Work {
titles {
title
}
descriptions {
description
descriptionType
}
doi
formattedCitation(style: "apa", locale: "en-US", format: text)
publicationYear
}
`


export default async function downloadReportsHandler(
req: NextApiRequest,
res: NextApiResponse
) {
const variables = req.query

const { data } = await apolloClient.query({
query: QUERY,
variables: variables
})

const sortedData = [...data.organization.works.nodes].sort((a, b) => b.publicationYear - a.publicationYear)

const csv = stringify(sortedData, {
header: true,
columns: [
{ key: 'titles[0].title', header: 'Title' },
{ key: 'publicationYear', header: 'Publication Year' },
{ key: 'doi', header: 'DOI' },
{ key: 'descriptions[0].description', header: 'Description' },
{ key: 'formattedCitation', header: 'Formatted Citation' }
]
})

try {
res.status(200)
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', `attachment; filename="related-works_${variables.id}.csv"`)
res.send(csv)
} catch (error) {
res.status(400).json({ error })
}

}
16 changes: 15 additions & 1 deletion src/pages/ror.org/[rorid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { rorFromUrl, pluralize } from '../../utils/helpers'
import ShareLinks from '../../components/ShareLinks/ShareLinks'
import { Title } from 'src/components/Title/Title'
import DownloadReports from 'src/components/DownloadReports/DownloadReports'
import OrganizationDashboard from 'src/components/OrganizationDashboard/OrganizationDashboard'

type Props = {
Expand Down Expand Up @@ -237,7 +238,20 @@ const OrganizationPage: React.FunctionComponent<Props> = ({
const content = () => {
return (
<>
<Col md={3} id="side-bar">
<Col md={3} className="panel-list" id="side-bar">
<DownloadReports url={'ror.org' + rorFromUrl(organization.id)} title={organization.name} variables={{
id: rorId,
gridId: gridId,
crossrefFunderId: crossrefFunderId,
cursor: cursor,
filterQuery: filterQuery,
published: published,
resourceTypeId: resourceType,
fieldOfScience: fieldOfScience,
language: language,
license: license,
registrationAgency: registrationAgency
}} />
<ShareLinks url={'ror.org' + rorFromUrl(organization.id)} title={organization.name} />
</Col>
<Col md={9}>
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4346,6 +4346,11 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==

csv-stringify@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/csv-stringify/-/csv-stringify-6.4.0.tgz#6d006dca9194700e44f9fbc541bee8bbbd4f459c"
integrity sha512-HQsw0QXiN5fdlO+R8/JzCZnR3Fqp8E87YVnhHlaPtNGJjt6ffbV0LpOkieIb1x6V1+xt878IYq77SpXHWAqKkA==

cypress-dotenv-flow@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/cypress-dotenv-flow/-/cypress-dotenv-flow-1.2.2.tgz#3ca9e9a0a9fecd7205ba9e87c0c929527518e53e"
Expand Down

0 comments on commit f15bbd7

Please sign in to comment.