Skip to content

Commit

Permalink
refactor: update reader component props and rename asset type
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGross committed Nov 22, 2024
1 parent b6b2519 commit 0314c4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/pages/workPageLayout/WorkPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function WorkPageLayout({ wid }: { wid: string }) {
{identifier && (
<Button ariaLabel="Prøv ebog" asChild>
<SmartLink linkType="external" href={url}>
Read
Prøv ebog
</SmartLink>
</Button>
)}
Expand Down
18 changes: 10 additions & 8 deletions components/shared/publizonReader/PublizonReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ type ReaderType =
| {
type: "demo"
identifier: string
orderId?: never
onBackCallback: () => void
}
| {
type: "rent"
identifier?: never
orderId: string
onBackCallback: () => void
}

const Reader = (props: ReaderType) => {
const Reader = ({ type, onBackCallback, identifier, orderId }: ReaderType) => {
useEffect(() => {
readerAssets.forEach(appendAsset)

Expand All @@ -26,7 +28,7 @@ const Reader = (props: ReaderType) => {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-shadow
window.onReaderBackCallback = () => {
props.onBackCallback()
onBackCallback()
}

return () => {
Expand All @@ -35,16 +37,16 @@ const Reader = (props: ReaderType) => {
delete window.onReaderBackCallback
readerAssets.forEach(removeAsset)
}
}, [props])
}, [])

if (props.type === "rent") {
if (type === "rent") {
return (
<div>
<p>orderId: {props.orderId}</p>
<p>orderId: {orderId}</p>
<div
id="pubhub-reader"
// eslint-disable-next-line react/no-unknown-property
order-id={props.orderId}
order-id={orderId}
role="button"
tabIndex={0}
close-href="javascript:window.onReaderBackCallback()"
Expand All @@ -54,14 +56,14 @@ const Reader = (props: ReaderType) => {
)
}

if (props.type === "demo") {
if (type === "demo") {
return (
<div
id="pubhub-reader"
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line react/no-unknown-property
identifier={props.identifier}
identifier={identifier}
close-href="javascript:window.onReaderBackCallback()"
role="button"
tabIndex={0}
Expand Down
8 changes: 4 additions & 4 deletions components/shared/publizonReader/helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type AssetType = {
type TAssetType = {
src: string
type: "script" | "link"
}

export const readerAssets: AssetType[] = [
export const readerAssets: TAssetType[] = [
{
src: "https://reader.pubhub.dk/2.2.0/js/chunk-vendors.js",
type: "script",
Expand All @@ -22,7 +22,7 @@ export const readerAssets: AssetType[] = [
},
]

export const appendAsset = ({ src, type }: AssetType) => {
export const appendAsset = ({ src, type }: TAssetType) => {
if (type === "script") {
const scriptElement = document.createElement("script")
scriptElement.src = src
Expand All @@ -40,7 +40,7 @@ export const appendAsset = ({ src, type }: AssetType) => {
}
}

export const removeAsset = ({ src, type }: AssetType) => {
export const removeAsset = ({ src, type }: TAssetType) => {
if (type === "script") {
const scriptElement = document.querySelector(`script[src="${src}"]`)
scriptElement?.remove()
Expand Down

0 comments on commit 0314c4e

Please sign in to comment.