Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render Json-LD in Dataset Page #412

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from
Draft
2 changes: 2 additions & 0 deletions src/sections/dataset/Dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { BreadcrumbsGenerator } from '../shared/hierarchy/BreadcrumbsGenerator'
import { useAlertContext } from '../alerts/AlertContext'
import { AlertMessageKey } from '../../alert/domain/models/Alert'
import { useAddDatasetJsonLd } from './useAddDatasetJsonLd'

interface DatasetProps {
fileRepository: FileRepository
Expand All @@ -32,6 +33,7 @@ export function Dataset({ fileRepository, created }: DatasetProps) {
const { t } = useTranslation('dataset')
const { hideModal, isModalOpen } = useNotImplementedModal()
const { addDatasetAlert } = useAlertContext()
useAddDatasetJsonLd({ persistentId: dataset?.persistentId })

if (created) {
addDatasetAlert({ messageKey: AlertMessageKey.DATASET_CREATED, variant: 'success' })
Expand Down
46 changes: 46 additions & 0 deletions src/sections/dataset/useAddDatasetJsonLd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useRef } from 'react'
interface DatasetJsonLdProps {
persistentId: string | undefined
}

export const useAddDatasetJsonLd = ({ persistentId }: DatasetJsonLdProps) => {
const scriptRef = useRef<HTMLScriptElement | null>(null)

const addJsonLdScript = (persistentId: string) => {
const jsonLdData = {
'@context': 'http://schema.org',
'@type': 'Dataset',
'@id': persistentId,
identifier: persistentId,
name: 'test',
creator: [
{
'@type': 'Person',
givenName: 'Guillermo',
familyName: 'Portas',
name: 'Portas, Guillermo'
}
]
}
//TODO: replace string with data from server
const script = document.createElement('script')
scriptRef.current = script
//persistentIdRef.current = persistentId
script.type = 'application/ld+json'
script.text = JSON.stringify(jsonLdData)
document.head.append(script)
}
useEffect(() => {
if (persistentId && scriptRef.current === null) {
console.log('%c Adding script', 'color: white; background: green; padding: 4px;')
addJsonLdScript(persistentId)
}

return () => {
if (scriptRef.current !== null) {
console.log('%c Removing script', 'color: white; background: orange; padding: 4px;')
scriptRef.current.remove()
}
}
}, [persistentId])
}
Loading