Skip to content

Commit

Permalink
feat: added experiment flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kimon-satan committed Nov 18, 2024
1 parent 5810114 commit b20596e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
File renamed without changes.
20 changes: 10 additions & 10 deletions src/pages-helpers/teacher/share-experiments/getUpdatedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import {
createAndStoreShareId,
getShareIdKey,
shareMethods,
} from "@/utils/createShareId";
} from "@/pages-helpers/teacher/share-experiments/createShareId";

/**
*
* updates the url with either the stored cookie share-id or a newly generated one
*
*/

export const getUpdatedUrl = ({
url,
urlShareId,
cookieShareId,
lessonSlug,
}: {
url: string;
urlShareId: string | null;
cookieShareId: string | undefined;
lessonSlug: string;
}) => {
const shareIdKey = getShareIdKey(lessonSlug);

const strippedUrl = url.split("?")[0];

console.log("URL Share ID:", urlShareId);
console.log("Cookie Share ID:", cookieShareId);

if (urlShareId && cookieShareId === urlShareId) {
if (cookieShareId) {
console.log("Share ID already stored in cookie");
// we already generated a share-id from this page
return { url, shareIdKey, shareId: urlShareId };
const updatedUrl = `${strippedUrl}?${shareIdKey}=${cookieShareId}&sm=${shareMethods.url}`;
return { url: updatedUrl, shareIdKey, shareId: cookieShareId };
}

// generate share-id and store it as a cookie
Expand Down
15 changes: 11 additions & 4 deletions src/pages/teachers/lessons/[lessonSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
oakDefaultTheme,
} from "@oaknational/oak-components";
import { useEffect, useRef } from "react";
import { useFeatureFlagVariantKey } from "posthog-js/react";

import curriculumApi2023 from "@/node-lib/curriculum-api-2023";
import getPageProps from "@/node-lib/getPageProps";
Expand All @@ -25,7 +26,10 @@ import { LessonOverview } from "@/components/TeacherViews/LessonOverview/LessonO
import OakError from "@/errors/OakError";
import { LessonOverviewCanonical } from "@/node-lib/curriculum-api-2023/queries/lessonOverview/lessonOverview.schema";
import { populateLessonWithTranscript } from "@/utils/handleTranscript";
import { getShareIdFromCookie, getShareIdKey } from "@/utils/createShareId";
import {
getShareIdFromCookie,
getShareIdKey,
} from "@/pages-helpers/teacher/share-experiments/createShareId";
import { getUpdatedUrl } from "@/pages-helpers/teacher/share-experiments/getUpdatedUrl";

type PageProps = {
Expand All @@ -44,8 +48,12 @@ export default function LessonOverviewCanonicalPage({
const shareIdRef = useRef<string | null>(null);
const shareIdKeyRef = useRef<string | null>(null);

const shareExperimentFlag = useFeatureFlagVariantKey(
"delivery-sq-share-experiment",
);

useEffect(() => {
if (!shareIdRef.current) {
if (!shareIdRef.current && shareExperimentFlag) {
// get the current url params
const urlParams = new URLSearchParams(window.location.search);
const urlShareId = urlParams.get(getShareIdKey(lesson.lessonSlug));
Expand All @@ -58,7 +66,6 @@ export default function LessonOverviewCanonicalPage({

const { url, shareIdKey, shareId } = getUpdatedUrl({
url: window.location.href,
urlShareId,
cookieShareId,
lessonSlug: lesson.lessonSlug,
});
Expand All @@ -70,7 +77,7 @@ export default function LessonOverviewCanonicalPage({
window.history.replaceState({}, "", url);
}
}
}, [lesson.lessonSlug]);
}, [lesson.lessonSlug, shareExperimentFlag]);

const pathwayGroups = groupLessonPathways(lesson.pathways);
return (
Expand Down
1 change: 0 additions & 1 deletion src/utils/getCookiesWithSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function getCookiesWithSchema<
const { dflt, disableLogging } = opts;
let cookieValue: unknown;
const cookieValueRaw = Cookies.get(key);

if (cookieValueRaw === undefined) {
return dflt;
}
Expand Down

0 comments on commit b20596e

Please sign in to comment.