Skip to content

Commit

Permalink
[frontend] add recursive gql-node-renderer (#847)
Browse files Browse the repository at this point in the history
* resolve merge conflicts

change fallback to blocking

* modify layout on article pages, add revalidate to article pages

* content spans 7 colummns, image spans 5 columns

---------

Co-authored-by: Jordan Willis <[email protected]>
Co-authored-by: Jordan <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2023
1 parent 4fd8376 commit de2b666
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 391 deletions.
50 changes: 50 additions & 0 deletions components/gql_node_renderer/Recur.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { v4 as uuid } from "uuid";

import Header from "./nodes/Header";
import LineBreak from "./nodes/LineBreak";
import ListItem from "./nodes/ListItem";
import OrderedList from "./nodes/OrderedList";
import Paragraph from "./nodes/Paragraph";
import Text from "./nodes/Text";
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
const NODES = {
header: Header,
paragraph: Paragraph,
link: Link,
text: Text,
"unordered-list": UnorderedList,
"ordered-list": OrderedList,
"list-item": ListItem,
"line-break": LineBreak,
};

export default function Recur(props) {
const Node = NODES[props.node?.nodeType];
let content = props.node?.content;

if (
!Node ||
(props.excludeH1 &&
props.node?.nodeType === "header" &&
props.node?.style === "h1")
) {
return;
}

return (
<>
{content && content.length ? (
<Node key={uuid()} node={props.node}>
{content.map((node) => (
<Recur key={uuid()} node={node} />
))}
</Node>
) : (
<Node key={uuid()} node={props.node}></Node>
)}
</>
);
}
12 changes: 12 additions & 0 deletions components/gql_node_renderer/Render.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Recur from "./Recur";
import { v4 as uuid } from "uuid";

export default function Render(props) {
return (
<>
{props.data.map((node) => (
<Recur key={uuid()} node={node} excludeH1={props.excludeH1} />
))}
</>
);
}
16 changes: 16 additions & 0 deletions components/gql_node_renderer/nodes/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function Header(props) {
switch (props.node.style) {
case "h1":
return <h1>{props.children}</h1>;
case "h2":
return <h2>{props.children}</h2>;
case "h3":
return <h3>{props.children}</h3>;
case "h4":
return <h4>{props.children}</h4>;
case "h5":
return <h5>{props.children}</h5>;
default:
return <h6>{props.children}</h6>;
}
}
3 changes: 3 additions & 0 deletions components/gql_node_renderer/nodes/LineBreak.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function LineBreak() {
return <br />;
}
7 changes: 7 additions & 0 deletions components/gql_node_renderer/nodes/Link.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Link(props) {
return (
<a href={props.node.data.href} className="underline">
{props.node.value}
</a>
);
}
3 changes: 3 additions & 0 deletions components/gql_node_renderer/nodes/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ListItem(props) {
return <li>{props.children}</li>;
}
3 changes: 3 additions & 0 deletions components/gql_node_renderer/nodes/OrderedList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function UnorderedList(props) {
return <ol>{props.children}</ol>;
}
3 changes: 3 additions & 0 deletions components/gql_node_renderer/nodes/Paragraph.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Paragraph(props) {
return <p>{props.children}</p>;
}
11 changes: 11 additions & 0 deletions components/gql_node_renderer/nodes/Text.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function Text(props) {
// todo: guessing on italic since I didn't see it, but I'm assuming it exists and will need to be updated
switch (props.node?.format?.variants?.[0]) {
case "strong":
return <strong>{props.node.value}</strong>;
case "emphasis":
return <em>{props.node.value}</em>;
default:
return <>{props.node.value}</>;
}
}
3 changes: 3 additions & 0 deletions components/gql_node_renderer/nodes/UnorderedList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function UnorderedList(props) {
return <ul>{props.children}</ul>;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"remark-breaks": "^3.0.3",
"remark-html": "^15.0.1",
"remark-parse": "^10.0.2",
"unified": "^10.1.2"
"unified": "^10.1.2",
"uuid": "^9.0.0"
},
"scripts": {
"dev": "next dev",
Expand Down
Loading

0 comments on commit de2b666

Please sign in to comment.