Skip to content

Commit

Permalink
Refactor embedded work page to include GTM dataLayer values
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed Jun 12, 2024
1 parent 6ada9b9 commit f769684
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const customJestConfig = {
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
testEnvironment: "jest-environment-jsdom",
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
watchPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/.next/"],
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
Expand Down
1 change: 1 addition & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function MyApp({ Component, pageProps }: MyAppProps) {

React.useEffect(() => {
if (typeof window !== "undefined" && mounted) {
console.log("pageProps", pageProps);
const payload = {
...pageProps.dataLayer,
isLoggedIn: user?.isLoggedIn,
Expand Down
50 changes: 27 additions & 23 deletions pages/embedded-viewer/[manifestId].tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { GetServerSideProps, NextPage } from "next";

import { DCAPI_ENDPOINT } from "@/lib/constants/endpoints";
import { NextPage } from "next";
import React from "react";
import type { ViewerConfigOptions } from "@samvera/clover-iiif";
import type { Work } from "@nulib/dcapi-types";
import WorkRestrictedDisplay from "@/components/Work/RestrictedDisplay";
import WorkViewerWrapper from "@/components/Clover/ViewerWrapper";
import { buildWorkDataLayer } from "@/lib/ga/data-layer";
import { getUrlSearchParams } from "@/lib/utils/get-url-search-params";
import { getWork } from "@/lib/work-helpers";
import { useRouter } from "next/router";
import useWorkAuth from "@/hooks/useWorkAuth";

const EmbeddedViewerPage: NextPage = () => {
const router = useRouter();
const [manifestId, setManifestId] = React.useState("");
const [work, setWork] = React.useState<Work | null>();
interface EmbeddedViewerPageProps {
work: Work;
}

const EmbeddedViewerPage: NextPage<EmbeddedViewerPageProps> = ({ work }) => {
const router = useRouter();
const { isWorkRestricted } = useWorkAuth(work);
const thumbnail = work?.thumbnail || "";

Expand Down Expand Up @@ -44,28 +47,11 @@ const EmbeddedViewerPage: NextPage = () => {
showTitle: showTitle === "true" ? true : false,
};

React.useEffect(() => {
if (!router.isReady || !router.query.manifestId) return;

const mId = decodeURIComponent(router.query.manifestId as string);

const workId = mId
.replace(`${DCAPI_ENDPOINT}/works/`, "")
.replace("?as=iiif", "");

(async () => {
const workResponse = await getWork(workId);
setWork(workResponse);
})();

setManifestId(mId);
}, [router.isReady, router.query.manifestId]);

return (
<>
{!isWorkRestricted ? (
<WorkViewerWrapper
manifestId={manifestId}
manifestId={work.iiif_manifest}
viewerOptions={viewerOptions}
/>
) : (
Expand All @@ -75,4 +61,22 @@ const EmbeddedViewerPage: NextPage = () => {
);
};

export const getServerSideProps: GetServerSideProps = async (context) => {
const mId = decodeURIComponent(context?.params?.manifestId as string);

const workId = mId
.replace(`${DCAPI_ENDPOINT}/works/`, "")
.replace("?as=iiif", "");

const work = await getWork(workId);
const dataLayer = work ? buildWorkDataLayer(work) : [];

return {
props: {
dataLayer,
work,
},
};
};

export default EmbeddedViewerPage;

0 comments on commit f769684

Please sign in to comment.