Skip to content

Commit

Permalink
feat: refactored expiring banner into a component
Browse files Browse the repository at this point in the history
  • Loading branch information
kimon-satan committed Dec 6, 2024
1 parent fc31fac commit 677f20b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import {
OakPupilJourneyList,
OakPupilJourneyListCounter,
OakBox,
OakInlineBanner,
isValidIconName,
OakSecondaryButton,
} from "@oaknational/oak-components";
import { useState } from "react";

import { resolveOakHref } from "@/common-lib/urls";
import { LessonListingBrowseData } from "@/node-lib/curriculum-api-2023/queries/pupilLessonListing/pupilLessonListing.schema";
import AppLayout from "@/components/SharedComponents/AppLayout";
import { getSeoProps } from "@/browser-lib/seo/getSeoProps";
import { ExpiringBanner } from "@/components/SharedComponents/ExpiringBanner";

export type PupilLessonListingViewProps = {
unitData: LessonListingBrowseData[number]["unitData"];
Expand Down Expand Up @@ -108,7 +107,7 @@ export const PupilViewsLessonListing = (props: PupilLessonListingViewProps) => {
tag="h2"
/>
</OakFlex>
<OakInlineBanner
{/* <OakInlineBanner
canDismiss
cta={
<OakSecondaryButton
Expand All @@ -125,8 +124,16 @@ export const PupilViewsLessonListing = (props: PupilLessonListingViewProps) => {
onDismiss={() => {
setShowExpiredLessonsBanner(false);
}}
title="Some of these lessons will soon be taken down."
title="These lessons will be removed by Summer Term 2025."
type="alert"
/> */}
<ExpiringBanner
isOpen={showExpiredLessonsBanner}
isResourcesMessage={false}
onwardHref={unitListingHref}
onClose={() => {
setShowExpiredLessonsBanner(false);
}}
/>
</OakFlex>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
OakGridArea,
OakHeading,
OakIcon,
OakInlineBanner,
OakLessonBottomNav,
OakLessonLayout,
OakLessonNavItem,
OakP,
OakPrimaryButton,
OakPupilContentGuidance,
OakSecondaryButton,
OakSpan,
OakSubjectIcon,
isValidIconName,
Expand All @@ -30,6 +28,7 @@ import { useGetSectionLinkProps } from "@/components/PupilComponents/pupilUtils/
import { LessonBrowseData } from "@/node-lib/curriculum-api-2023/queries/pupilLesson/pupilLesson.schema";
import { useTrackSectionStarted } from "@/hooks/useTrackSectionStarted";
import { usePupilAnalytics } from "@/components/PupilComponents/PupilAnalyticsProvider/usePupilAnalytics";
import { ExpiringBanner } from "@/components/SharedComponents/ExpiringBanner";

type PupilViewsLessonOverviewProps = {
browseData: LessonBrowseData;
Expand Down Expand Up @@ -116,6 +115,17 @@ export const PupilViewsLessonOverview = ({
</OakP>
));

const expiringBanner = (
<ExpiringBanner
isOpen={showExpiredLessonsBanner}
isResourcesMessage={false}
onwardHref={unitListingHref}
onClose={() => {
setShowExpiredLessonsBanner(false);
}}
/>
);

return (
<OakLessonLayout
lessonSectionName={"overview"}
Expand Down Expand Up @@ -157,28 +167,13 @@ export const PupilViewsLessonOverview = ({
}}
/>
</OakGridArea>
<OakGridArea $colStart={[1, 1, 2]} $colSpan={[12, 12, 10]}>
<OakInlineBanner
canDismiss
cta={
<OakSecondaryButton
element="a"
iconName="arrow-right"
isTrailingIcon
href={unitListingHref}
>
View new lessons
</OakSecondaryButton>
}
isOpen={showExpiredLessonsBanner}
message="We've made brand-new and improved lessons for you."
onDismiss={() => {
setShowExpiredLessonsBanner(false);
}}
title="These lessons will be removed by Spring 2025."
type="alert"
$mt={"space-between-m"}
/>
<OakGridArea
$colStart={[1, 1, 2]}
$colSpan={[12, 12, 10]}
$pt="inner-padding-xl"
$display={["none", "block"]}
>
{expiringBanner}
</OakGridArea>
</OakGrid>

Expand Down Expand Up @@ -282,6 +277,16 @@ export const PupilViewsLessonOverview = ({
</OakBox>
)}
</OakGridArea>

<OakGridArea
$display={["block", "none"]}
$colStart={[1, 1, 7]}
$colSpan={[12, 12, 5]}
$ph={["inner-padding-m", "inner-padding-none"]}
$pb={"inner-padding-m"}
>
{expiringBanner}
</OakGridArea>
<OakGridArea
$colStart={[1, 1, 7]}
$colSpan={[12, 12, 5]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ExpiringBanner } from "./ExpiringBanner";

import renderWithTheme from "@/__tests__/__helpers__/renderWithTheme";

const render = renderWithTheme;

describe("ExpiringBanner", () => {
const defaultProps = {
isOpen: true,
isResourcesMessage: false,
onwardHref: "/new-lessons",
onClose: jest.fn(),
onViewNewLessons: jest.fn(),
};

it("should render the banner when isOpen is true", () => {
const screen = render(<ExpiringBanner {...defaultProps} />);
expect(
screen.getByText("We've made brand-new and improved lessons for you."),
).toBeInTheDocument();
});

it("should not render the banner when isOpen is false", () => {
const screen = render(<ExpiringBanner {...defaultProps} isOpen={false} />);
expect(
screen.queryByText("We've made brand-new and improved lessons for you."),
).not.toBeVisible();
});

it("should display the correct title for resources message", () => {
const screen = render(
<ExpiringBanner {...defaultProps} isResourcesMessage={true} />,
);
expect(
screen.getByText(
"These resources will be removed by end of Summer Term 2025.",
),
).toBeInTheDocument();
});

it("should display the correct title for lessons message", () => {
const screen = render(
<ExpiringBanner {...defaultProps} isResourcesMessage={false} />,
);
expect(
screen.getByText(
"These lessons will be removed by end of Summer Term 2025.",
),
).toBeInTheDocument();
});

it("should call onClose when the dismiss button is clicked", () => {
const screen = render(<ExpiringBanner {...defaultProps} />);
screen.getByRole("button", { name: /dismiss/i }).click();
expect(defaultProps.onClose).toHaveBeenCalled();
});

it("should call onViewNewLessons when the View new lessons button is clicked", () => {
const screen = render(<ExpiringBanner {...defaultProps} />);
screen.getByRole("link", { name: /view new lessons/i }).click();
expect(defaultProps.onViewNewLessons).toHaveBeenCalled();
});
});
45 changes: 45 additions & 0 deletions src/components/SharedComponents/ExpiringBanner/ExpiringBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
OakBox,
OakInlineBanner,
OakSecondaryButton,
} from "@oaknational/oak-components";

export type ExpiringBannerProps = {
isOpen: boolean;
isResourcesMessage: boolean;
onwardHref: string;
onClose: () => void;
onViewNewLessons?: () => void;
};

export const ExpiringBanner = ({
isOpen,
isResourcesMessage,
onwardHref,
onClose,
onViewNewLessons,
}: ExpiringBannerProps) => {
return (
<OakInlineBanner
canDismiss
cta={
<OakBox $pt="inner-padding-s">
<OakSecondaryButton
element="a"
iconName="arrow-right"
isTrailingIcon
href={onwardHref}
onClick={onViewNewLessons}
>
View new lessons
</OakSecondaryButton>
</OakBox>
}
isOpen={isOpen}
message="We've made brand-new and improved lessons for you."
onDismiss={onClose}
title={`These ${isResourcesMessage ? "resources" : "lessons"} will be removed by end of Summer Term 2025.`}
type="alert"
/>
);
};
1 change: 1 addition & 0 deletions src/components/SharedComponents/ExpiringBanner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ExpiringBanner";

0 comments on commit 677f20b

Please sign in to comment.