Skip to content

Commit

Permalink
Add comments and remove all instances of cdcp-apply (#1123)
Browse files Browse the repository at this point in the history
* add comments and remove all instances of cdc-apply

* add missing imports

* fix broken content sections
  • Loading branch information
will0684 authored Nov 12, 2024
1 parent 3650fa6 commit 3cf5c93
Show file tree
Hide file tree
Showing 25 changed files with 1,047 additions and 365 deletions.
2 changes: 1 addition & 1 deletion __tests__/api/robots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("robots api", () => {
});
await handler(req, res);
expect(res._getData()).toBe(
"User-agent: *\nDisallow: /api\nDisallow: /projects/*\nDisallow: /notsupported.js\nDisallow: /cdcp-apply\nDisallow: /rsdc-demander\n"
"User-agent: *\nDisallow: /api\nDisallow: /projects/*\nDisallow: /notsupported.js\nDisallow: /rsdc-demander\n"
);
});
});
56 changes: 48 additions & 8 deletions components/fragment_renderer/FragmentRender.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Import UUID generator for unique React keys
import { v4 as uuid } from "uuid";

// Import all fragment-specific components
import TextWithImage from "./fragment_components/TextWithImage";
import TextContent from "./fragment_components/TextContent";
import Button from "./fragment_components/Button";
Expand All @@ -7,17 +10,33 @@ import QuoteVerticalLineContent from "./fragment_components/QuoteVerticalLineCon
import ImageWithCollapse from "./fragment_components/ImageWithCollapse";
import TextRender from "../text_node_renderer/TextRender";

/**
* Map of fragment types to their corresponding React components
* Each key represents a specific AEM fragment model type
* Values are the React components responsible for rendering that fragment type
*/
const FRAGMENTS = {
"SCLabs-Comp-Content-Image-v1": TextWithImage,
"SCLabs-Comp-Content-v1": QuoteVerticalLineContent,
"SCLabs-Content-v1": TextContent,
"SCLabs-Button-v1": Button,
"SCLabs-Feature-v1": ArticleCTA,
"SCLabs-Image-v1": ImageWithCollapse,
"SCLabs-Comp-Content-Image-v1": TextWithImage, // Combined text and image layouts
"SCLabs-Comp-Content-v1": QuoteVerticalLineContent, // Quote blocks with vertical line styling
"SCLabs-Content-v1": TextContent, // Generic text content blocks
"SCLabs-Button-v1": Button, // Interactive button elements
"SCLabs-Feature-v1": ArticleCTA, // Call-to-action article features
"SCLabs-Image-v1": ImageWithCollapse, // Images with collapsible details
};

/**
* Maps AEM fragment data to component-specific props
* Handles bilingual content and different layout variations
*
* @param {Object} fragmentData - Raw fragment data from AEM
* @param {string} fragmentName - Type identifier for the fragment
* @param {string} locale - Current language locale (en/fr)
* @param {boolean} excludeH1 - Whether to exclude h1 headers
* @returns {Object} Props object formatted for the specific component
*/
const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
switch (fragmentName) {
// Article CTA Fragment handling
case "SCLabs-Feature-v1":
return {
heading:
Expand All @@ -44,8 +63,10 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
},
};

// Text with Image Fragment handling
case "SCLabs-Comp-Content-Image-v1":
switch (fragmentData.scLabLayout) {
// Default layout configuration
case "default":
return {
src:
Expand All @@ -66,6 +87,7 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
layout: fragmentData.scLabLayout,
excludeH1: excludeH1,
};
// Vertical line layout configuration
case "image-vertical-line-content":
return {
src:
Expand Down Expand Up @@ -106,6 +128,7 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
break;
}

// Quote with vertical line styling
case "SCLabs-Comp-Content-v1":
return {
quoteText:
Expand All @@ -118,6 +141,7 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
: fragmentData.scLabContent[1].scContentFr.json,
};

// Generic text content
case "SCLabs-Content-v1":
return {
data:
Expand All @@ -126,6 +150,7 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
: fragmentData.scContentFr.json,
};

// Interactive button element
case "SCLabs-Button-v1":
return {
id: fragmentData.scId,
Expand All @@ -137,6 +162,7 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
text: locale === "en" ? fragmentData.scTitleEn : fragmentData.scTitleFr,
};

// Image with collapsible details
case "SCLabs-Image-v1":
return {
id: fragmentData.scId,
Expand Down Expand Up @@ -182,14 +208,28 @@ const mapFragmentsToProps = (fragmentData, fragmentName, locale, excludeH1) => {
}
};

/**
* FragmentRender Component
* Main component responsible for rendering AEM content fragments
* Handles the transformation of AEM data into React components
*
* @param {Object} props - Component props
* @param {Array} props.fragments - Array of AEM content fragments
* @param {string} props.locale - Current language locale
* @param {boolean} props.excludeH1 - Flag to exclude h1 headers
*/
export default function FragmentRender(props) {
// Create and return array of elements corresponding to
// fragments
// Map each fragment to its corresponding React component
const pageFragments = props.fragments.map((fragmentData) => {
// Get the appropriate component based on the fragment model type
const Fragment = FRAGMENTS[fragmentData?._model.title];

// Skip if no matching component is found
if (!Fragment) {
return;
}

// Render the component with mapped props
return (
<Fragment
key={uuid()}
Expand Down
38 changes: 28 additions & 10 deletions components/text_node_renderer/TextRecur.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v4 as uuid } from "uuid";

// Import all supported node type components
import HeaderText from "./nodes/HeaderText";
import LineBreak from "./nodes/LineBreak";
import ListItem from "./nodes/ListItem";
Expand All @@ -10,23 +11,37 @@ import Span from "./nodes/Span";
import UnorderedList from "./nodes/UnorderedList";
import Link from "./nodes/Link";

// todo: more components will like need to be added, but for now, these are the only ones returned in the aem json response
/**
* TextRecur Component
* Recursive component that handles the actual rendering of content nodes
* Supports various content types (headers, paragraphs, lists, etc.)
* Can handle nested content structures of arbitrary depth
*/

// Map of supported node types to their corresponding components
// This mapping allows dynamic selection of the appropriate component based on nodeType
const NODES = {
header: HeaderText,
paragraph: Paragraph,
link: Link,
text: Text,
span: Span,
"unordered-list": UnorderedList,
"ordered-list": OrderedList,
"list-item": ListItem,
"line-break": LineBreak,
header: HeaderText, // Renders h1-h6 headers
paragraph: Paragraph, // Renders paragraph blocks
link: Link, // Renders hyperlinks
text: Text, // Renders plain text
span: Span, // Renders inline styled text
"unordered-list": UnorderedList, // Renders bullet lists
"ordered-list": OrderedList, // Renders numbered lists
"list-item": ListItem, // Renders individual list items
"line-break": LineBreak, // Renders line breaks
};

export default function TextRecur(props) {
// Get the appropriate component for the current node type
const Node = NODES[props.node?.nodeType];

// Get any child content from the current node
let content = props.node?.content;

// Skip rendering if:
// 1. No matching component exists for this node type
// 2. Node is an h1 header and excludeH1 flag is true
if (
!Node ||
(props.excludeH1 &&
Expand All @@ -38,13 +53,16 @@ export default function TextRecur(props) {

return (
<>
{/* Handle nodes differently based on whether they have child content */}
{content && content.length ? (
// For nodes with children: render the node and recursively render its children
<Node key={uuid()} node={props.node} index={props.index}>
{content.map((node) => (
<TextRecur key={uuid()} node={node} />
))}
</Node>
) : (
// For leaf nodes: render just the node itself
<Node key={uuid()} node={props.node}></Node>
)}
</>
Expand Down
16 changes: 11 additions & 5 deletions components/text_node_renderer/TextRender.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import TextRecur from "./TextRecur";
import { v4 as uuid } from "uuid";
import { v4 as uuid } from "uuid"; // Import UUID generator for unique keys

/**
* TextRender Component
* Top-level component that initiates the recursive rendering of text content
* Processes an array of content nodes from AEM and renders them recursively
*/
export default function TextRender(props) {
return (
<>
{/* Map through each content node and render it recursively */}
{props.data.map((node, index) => {
return (
<TextRecur
key={uuid()}
node={node}
index={index}
excludeH1={props.excludeH1}
key={uuid()} // Generate unique key for React rendering
node={node} // Pass the current node data
index={index} // Pass index for potential ordering/styling
excludeH1={props.excludeH1} // Flag to exclude h1 headers if needed
/>
);
})}
Expand Down
Loading

0 comments on commit 3cf5c93

Please sign in to comment.