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

Feat/curr get cloudinary url #312

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/components/atoms/OakIcon/OakIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { create } from "react-test-renderer";

import { OakIcon, isValidIconName } from "./OakIcon";
import { OakIcon, generateOakIconURL, isValidIconName } from "./OakIcon";

describe("OakIcon", () => {
it("renders", () => {
Expand Down Expand Up @@ -69,3 +69,23 @@ describe("isValidIconName", () => {
expect(isValidIconName("banana-sandwich")).toBe(false);
});
});

describe("generateOakIconURL", () => {
it("is valid url when the string is a valid icon name", () => {
expect(generateOakIconURL("home")).toBe(
"https://res.cloudinary.com/mock-cloudinary-cloud/image/upload/v1699887218/icons/gvqxjxcw07ei2kkmwnes.svg",
);
});

it("is returns url for question mark when the string is not a valid icon name", () => {
expect(generateOakIconURL("banana-sandwich")).toBe(
"https://res.cloudinary.com/mock-cloudinary-cloud/image/upload/v1706872277/icons/question-mark.svg",
);
});

it("is returns url for books when the string is not a valid subject icon name", () => {
expect(generateOakIconURL("subject-potions")).toBe(
"https://res.cloudinary.com/mock-cloudinary-cloud/image/upload/v1699953657/icons/hz4l3iq6i68kazvkvorq.svg",
);
});
});
16 changes: 15 additions & 1 deletion src/components/atoms/OakIcon/OakIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ export function isValidIconName(iconName: string): iconName is OakIconName {
return oakIconNames.includes(iconName as OakIconName);
}

/**
* returns a Icon URL from Cloudinary if is a valid icon, otherwise returns undefined
*/
export function generateOakIconURL(iconName: string) {
sonalisin marked this conversation as resolved.
Show resolved Hide resolved
const urlPath = `https://${process.env.NEXT_PUBLIC_OAK_ASSETS_HOST}/${process.env.NEXT_PUBLIC_OAK_ASSETS_PATH}`;
if (isValidIconName(iconName)) {
return `${urlPath}/${icons[iconName]}`;
} else if (iconName.includes("subject")) {
return `${urlPath}/${icons["books"]}`;
} else {
return `${urlPath}/${icons["question-mark"]}`;
}
}

/**
* A wrapper around OakImage which uses the image-map.json file to map icon names to image paths.
*/
Expand All @@ -42,7 +56,7 @@ export const OakIcon = (props: OakIconProps) => {

return (
<OakImage
src={`https://${process.env.NEXT_PUBLIC_OAK_ASSETS_HOST}/${process.env.NEXT_PUBLIC_OAK_ASSETS_PATH}/${icons[iconName]}`}
src={generateOakIconURL(iconName)}
alt={alt ?? iconName}
$width={$width}
$height={$height}
Expand Down