-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '1062-check-integration-with-unpaywall-org' into 'master'
feat(ViewPaper): integrate unpaywall Closes #1062 See merge request TIBHannover/orkg/orkg-frontend!903
- Loading branch information
Showing
7 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ REACT_APP_GEONAMES_API_SEARCH_URL=https://secure.geonames.org/search | |
REACT_APP_GEONAMES_API_USERNAME=reddine | ||
REACT_APP_OLS_BASE_URL=https://service.tib.eu/ts4tib/api/ | ||
REACT_APP_WIKIDATA_URL=https://www.wikidata.org/w/api.php | ||
REACT_APP_UNPAYWALL_URL=https://api.unpaywall.org/v2/ | ||
REACT_APP_UNPAYWALL_EMAIL=[email protected] | ||
|
||
# Is testing server: true or false | ||
REACT_APP_IS_TESTING_SERVER=false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/components/ViewPaper/PaperHeaderBar/ViewPaperButton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { faSortDown } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon as Icon } from '@fortawesome/react-fontawesome'; | ||
import PropTypes from 'prop-types'; | ||
import { useEffect, useState } from 'react'; | ||
import ContentLoader from 'react-content-loader'; | ||
import { ButtonDropdown, DropdownItem, DropdownMenu, DropdownToggle } from 'reactstrap'; | ||
import { getLinksByDoi, getLinksByTitle } from 'services/unpaywall'; | ||
|
||
const ViewPaperButton = ({ paperLink = null, doi = null, title = null }) => { | ||
const [links, setLinks] = useState([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isMenuOpen, setIsMenuOpen] = useState(false); | ||
const [hasLoaded, setHasLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
const fetchLinks = async () => { | ||
setIsLoading(true); | ||
if (doi) { | ||
setLinks(await getLinksByDoi(doi)); | ||
} else if (title) { | ||
setLinks(await getLinksByTitle(title)); | ||
} | ||
setIsLoading(false); | ||
setHasLoaded(true); | ||
}; | ||
if (isMenuOpen && !hasLoaded) { | ||
fetchLinks(); | ||
} | ||
}, [doi, hasLoaded, isMenuOpen, title]); | ||
|
||
return ( | ||
<ButtonDropdown isOpen={isMenuOpen} toggle={() => setIsMenuOpen(v => !v)}> | ||
<DropdownToggle style={{ marginRight: 2 }} size="sm" color="secondary" className="px-3 d-flex align-items-center"> | ||
View paper <Icon icon={faSortDown} style={{ margin: '-4px 0 0 6px' }} /> | ||
</DropdownToggle> | ||
<DropdownMenu> | ||
{paperLink && ( | ||
<DropdownItem tag="a" href={paperLink} target="_blank" rel="noopener noreferrer"> | ||
Visit paper | ||
</DropdownItem> | ||
)} | ||
{isLoading && ( | ||
<DropdownItem disabled> | ||
<ContentLoader height="80" width="200" viewBox="0 0 200 50" speed={2} backgroundColor="#f3f3f3" foregroundColor="#ecebeb"> | ||
<rect x="0" y="0" rx="0" ry="0" width="100%" height="20" /> | ||
<rect x="0" y="30" rx="0" ry="0" width="100%" height="20" /> | ||
</ContentLoader> | ||
</DropdownItem> | ||
)} | ||
{!isLoading && links.length > 0 && ( | ||
<> | ||
{paperLink && <DropdownItem divider />} | ||
<DropdownItem tag="a" className="dropdown-header" href="https://unpaywall.org/" target="_blank" rel="noopener noreferrer"> | ||
Alternative sources by Unpaywall | ||
</DropdownItem> | ||
{links.map(link => ( | ||
<DropdownItem tag="a" href={link.url} target="_blank" rel="noopener noreferrer"> | ||
{link.name} | ||
</DropdownItem> | ||
))} | ||
</> | ||
)} | ||
{!isLoading && links.length === 0 && !paperLink && <DropdownItem header>No links found</DropdownItem>} | ||
</DropdownMenu> | ||
</ButtonDropdown> | ||
); | ||
}; | ||
|
||
ViewPaperButton.propTypes = { | ||
paperLink: PropTypes.string, | ||
doi: PropTypes.string, | ||
title: PropTypes.string, | ||
}; | ||
|
||
export default ViewPaperButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import env from '@beam-australia/react-env'; | ||
import { upperFirst } from 'lodash'; | ||
import { submitGetRequest } from 'network'; | ||
|
||
export const unpaywallUrl = env('UNPAYWALL_URL'); | ||
|
||
const mapLocations = locations => | ||
locations?.map(location => ({ | ||
name: location.repository_institution || upperFirst(location.host_type), | ||
url: location.url, | ||
})) || []; | ||
|
||
export const getLinksByDoi = async doi => { | ||
try { | ||
const result = await submitGetRequest(`${unpaywallUrl}${encodeURIComponent(doi)}?email=${env('UNPAYWALL_EMAIL')}`); | ||
return mapLocations(result?.oa_locations); | ||
} catch (e) { | ||
console.log(e); | ||
return []; | ||
} | ||
}; | ||
|
||
export const getLinksByTitle = async title => { | ||
try { | ||
const result = await submitGetRequest(`${unpaywallUrl}search?query=${encodeURIComponent(title)}&email=${env('UNPAYWALL_EMAIL')}`); | ||
return mapLocations(result.results?.[0]?.response?.oa_locations); | ||
} catch (e) { | ||
console.log(e); | ||
return []; | ||
} | ||
}; |