-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add article publishing information component
- Loading branch information
Showing
6 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/components/Blog/BlogArticlePublishingInformation/cmp.builder.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Builder } from "@builder.io/react"; | ||
import { DEFAULT_PROPS } from "@/constants/builderProps"; | ||
import { CSS_EDITABLE_INPUTS_ADVANCED } from "@/constants/builderInputs"; | ||
import BlogArticlePublishingInformation from "./cmp"; | ||
|
||
Builder.registerComponent(BlogArticlePublishingInformation, { | ||
...DEFAULT_PROPS, | ||
name: "Blog - Article Publishing Information", | ||
image: | ||
"https://cdn.builder.io/api/v1/image/assets%2Fd72545ec06f647d993e1349bf57ebd7f%2Ffd9c99abfd534bbf9ebc839eed5acfd1", | ||
inputs: [ | ||
...DEFAULT_PROPS.inputs, | ||
{ | ||
name: "variant", | ||
type: "string", | ||
defaultValue: "readTime", | ||
enum: ["readTime", "writtenBy"], | ||
}, | ||
], | ||
}); |
81 changes: 81 additions & 0 deletions
81
src/components/Blog/BlogArticlePublishingInformation/cmp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import Image from "next/image"; | ||
import { BlogArticlePublishingInformationProps } from "./types"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import { fetchBuilderData } from "@/utils/fetchBuilderData"; | ||
import { buildArticleSchemaMarkup } from "@/utils/blog/buildArticleSchemaMarkup"; | ||
|
||
export const BlogArticlePublishingInformation = ({ | ||
variant, | ||
}: BlogArticlePublishingInformationProps) => { | ||
const router = useRouter(); | ||
|
||
const [currentPage, setCurrentPage] = useState<any>(); | ||
|
||
const articleSchemaMarkup = useMemo( | ||
() => buildArticleSchemaMarkup(currentPage), | ||
[currentPage] | ||
); | ||
|
||
useEffect(() => { | ||
async function getCurrentBlogArticlePage() { | ||
const page = await fetchBuilderData("get", [ | ||
"blog-article", | ||
{ | ||
userAttributes: { | ||
urlPath: router.asPath, | ||
}, | ||
includeRefs: true, | ||
}, | ||
]); | ||
|
||
setCurrentPage(page); | ||
} | ||
|
||
getCurrentBlogArticlePage(); | ||
}, [router.asPath]); | ||
|
||
if (!articleSchemaMarkup) return null; | ||
|
||
const { author, datePublished, timeRequired } = articleSchemaMarkup; | ||
|
||
// timeRequired is in ISO 8601 duration format (but only minutes) | ||
const readableTimeRequired = timeRequired.match(/PT(\d+)M/)?.[1]; | ||
|
||
let title; | ||
let subtitle; | ||
|
||
switch (variant) { | ||
case "readTime": | ||
title = author.name; | ||
subtitle = ( | ||
<> | ||
{timeRequired ? `${readableTimeRequired} min. read` : ""}{" "} | ||
{datePublished ? `- ${datePublished}` : ""} | ||
</> | ||
); | ||
break; | ||
case "writtenBy": | ||
title = `Written by ${author.name}`; | ||
subtitle = author.title; | ||
break; | ||
} | ||
|
||
return ( | ||
<div tw="flex flex-row items-center gap-3"> | ||
<Image | ||
src={author.image} | ||
alt="Publisher" | ||
width={32} | ||
height={32} | ||
tw="rounded-full" | ||
/> | ||
<div tw="flex flex-col justify-around"> | ||
<p className="tp-info text-base2 fs-12">{title}</p> | ||
<p className="tp-body1 text-base2 fs-10">{subtitle}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BlogArticlePublishingInformation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./cmp"; |
3 changes: 3 additions & 0 deletions
3
src/components/Blog/BlogArticlePublishingInformation/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type BlogArticlePublishingInformationProps = { | ||
variant: "readTime" | "writtenBy"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters