Skip to content

Commit

Permalink
CORE: bring back tracking (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekachxaidze98 authored Nov 25, 2024
1 parent a4f7e5e commit 2b81dd0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 22 deletions.
10 changes: 8 additions & 2 deletions api/data-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ const API_V3_URL = process.env.API_URL.replace('/internal', '/v3')

const requestV3 = (url, ...args) => request(`${API_V3_URL}${url}`, ...args)

const fetchMetadata = async (id) => {
const { data } = await requestV3(`/data-providers/${id}`)
// eslint-disable-next-line camelcase
const fetchMetadata = async (id, search_id) => {
const split = search_id?.split('-')
const isUndefined = split?.some((item) => item === undefined)
const { data } = await requestV3(
// eslint-disable-next-line camelcase
`/data-providers/${id}${!isUndefined || search_id ? `?t=${search_id}` : ''}`
)
return data
}

Expand Down
8 changes: 7 additions & 1 deletion api/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import apiRequest from './index'
const FileDownload = require('js-file-download')

export const fetchWorks = async (body) => {
const url = new URL(`/v3/search/works`, process.env.API_URL).href
const { t } = body
const split = t?.split('-')
const isUndefined = split?.some((item) => item === undefined)
const url = new URL(
`/v3/search/works${!isUndefined || t ? `?t=${t}` : ''}`,
process.env.API_URL
).href
const { data: dataWorks } = await apiRequest(url, {
body,
method: 'POST',
Expand Down
11 changes: 9 additions & 2 deletions api/works.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import apiRequest from './index'

const fetchWork = async (id) => {
const url = new URL(`/v3/works/${id}`, process.env.API_URL).href
// eslint-disable-next-line camelcase
const fetchWork = async (id, search_id) => {
const split = search_id?.split('-')
const isUndefined = split?.some((item) => item === undefined)
const url = new URL(
// eslint-disable-next-line camelcase
`/v3/works/${id}${!isUndefined || search_id ? `?t=${search_id}` : ''}`,
process.env.API_URL
).href

const { data } = await apiRequest(url)
return data
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pages/data-providers/[id].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ export async function getServerSideProps({
query: searchParams,
}) {
const { id } = routeParams
const { q = '', offset = 0, limit = 10, sort = 'recency' } = searchParams

const { q = '', offset = 0, limit = 10, sort = 'recency', t } = searchParams
const data = {}
try {
const dataProvider = await fetchMetadata(id)
const dataProvider = await fetchMetadata(id, t)
const dataProviderStats = await fetchStats(id)

Object.assign(data, {
Expand Down
3 changes: 2 additions & 1 deletion pages/search/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getServerSideProps = async ({ query: searchParams }) => {
}
// TODO for nice response
// const { q, page = 1, limit = 10, sort = 'recency' } = searchParams
const { q, page = 1, limit = 10, sort = 'relevance' } = searchParams
const { q, page = 1, limit = 10, sort = 'relevance', t } = searchParams

const data = {
currentPage: +page,
Expand All @@ -40,6 +40,7 @@ export const getServerSideProps = async ({ query: searchParams }) => {
q,
offset,
limit,
t,
exclude: ['fullText'],
sort: sort === 'recent' ? 'recency' : sort,
}
Expand Down
9 changes: 7 additions & 2 deletions pages/works/[id].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import { checkLogo, checkMembership } from 'utils/data-providers-transform'
const LOCALE = 'en-GB'
const CITATION_STYLES = ['apa', 'bibtex']

export async function getServerSideProps({ params: routeParams, req }) {
export async function getServerSideProps({
params: routeParams,
query: searchParams,
req,
}) {
const { id } = routeParams
const { t } = searchParams

const data = {}

try {
const rawWork = await fetchWork(id)
const rawWork = await fetchWork(id, t)
const { fullText: _, ...work } = rawWork
const outputs = await fetchWorkOutputs(id)

Expand Down
2 changes: 1 addition & 1 deletion templates/search/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const SearchTemplate = observe(({ data }) => {
/>
)}
</div>
<Results works={data.results} />
<Results works={data.results} searchId={data.searchId} />
{data.currentPage === 1000 && (
<div className={styles.more}>
Our search interface allows you to see only the first 10.000
Expand Down
54 changes: 50 additions & 4 deletions templates/search/results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { checkType } from '../../utils/data-providers-transform'

import { formatDate } from 'utils/helpers'

const Results = ({ works }) =>
const Results = ({ works, searchId }) =>
works.map(
({
id,
Expand Down Expand Up @@ -35,22 +35,68 @@ const Results = ({ works }) =>
memberType?.billing_type === 'supporting' ||
memberType?.billing_type === 'sustaining'

const generateMetadataLink = (baseLink, propSearchId, propId) =>
`${baseLink}/?t=${propSearchId}-${propId}`

const modifiedReaderLink = readerLink
?.replace(/(https:\/\/)(core\.ac\.uk)/, '$1api.$2')
.replace('/reader/', '/reader-ui/')

const renderFullTextLink = ({
fullTextLink: innerFullTextLink,
downloadLink: innerDownloadLink,
modifiedReaderLink: innerModifiedReaderLink,
searchId: innerSearchId,
id: innerId,
}) => {
if (
innerFullTextLink == null &&
innerDownloadLink == null &&
innerModifiedReaderLink == null
)
return null
if (
(innerFullTextLink && innerFullTextLink.includes('core')) ||
(innerDownloadLink && innerDownloadLink.includes('core')) ||
(innerModifiedReaderLink &&
innerModifiedReaderLink.includes('api.core'))
) {
return generateMetadataLink(
innerModifiedReaderLink,
innerSearchId,
innerId
)
}
if (innerDownloadLink) return innerDownloadLink
return innerFullTextLink
}

return (
<SearchResult
id={`search-output-${id}`}
key={`search-result-${id}`}
variant="outlined"
className={styles.searchResults}
useLogo={!!checkBillingType()}
searchId={searchId}
renderRedirectLink
data={{
id,
workId: id,
title,
author: authors,
publicationVenue: publicationVenue || null,
publicationDate: publicationDate || null,
thumbnailUrl: thumbnailLink || `//core.ac.uk/image/${id}/medium`,
metadataLink: metadataLink || displayLink,
fullTextLink: fullTextLink || readerLink || downloadLink,
metadataLink:
generateMetadataLink(metadataLink, searchId, id) ||
generateMetadataLink(displayLink, searchId, id),
fullTextLink: renderFullTextLink({
fullTextLink,
downloadLink,
modifiedReaderLink,
searchId,
id,
}),
dataProviders: dataProviders || [],
isRecommended: memberType?.billing_type === 'sustaining',
}}
Expand Down

0 comments on commit 2b81dd0

Please sign in to comment.