From 92fbd685e6dbd7dc775ad97c4d512bbf015d88ab Mon Sep 17 00:00:00 2001 From: Kipruto <43873157+kelvinkipruto@users.noreply.github.com> Date: Tue, 2 Jul 2024 11:50:06 +0300 Subject: [PATCH] Use CMS Content Signed-off-by: Kipruto <43873157+kelvinkipruto@users.noreply.github.com> --- apps/roboshield/payload-types.ts | 32 +++++++- .../src/components/Content/Content.tsx | 60 +++++++++----- .../LongFormExternalEmbed.tsx | 30 +++++++ .../components/LongFormExternalEmbed/index.ts | 3 + .../LongFormMedia/LongFormMedia.tsx | 43 ++++++++++ .../src/components/LongFormMedia/index.ts | 3 + .../LongFormRichText/LongFormRichText.tsx | 78 +++++++++++++++++++ .../src/components/LongFormRichText/index.ts | 3 + apps/roboshield/src/payload/blocks/Content.ts | 24 ++---- .../src/payload/blocks/ExternalEmbedd.ts | 62 +++++++++++++++ .../src/payload/blocks/MediaBlock.ts | 16 ++++ .../roboshield/src/payload/blocks/RichText.ts | 16 ++++ apps/roboshield/src/utils/blocks.ts | 4 + 13 files changed, 333 insertions(+), 41 deletions(-) create mode 100644 apps/roboshield/src/components/LongFormExternalEmbed/LongFormExternalEmbed.tsx create mode 100644 apps/roboshield/src/components/LongFormExternalEmbed/index.ts create mode 100644 apps/roboshield/src/components/LongFormMedia/LongFormMedia.tsx create mode 100644 apps/roboshield/src/components/LongFormMedia/index.ts create mode 100644 apps/roboshield/src/components/LongFormRichText/LongFormRichText.tsx create mode 100644 apps/roboshield/src/components/LongFormRichText/index.ts create mode 100644 apps/roboshield/src/payload/blocks/ExternalEmbedd.ts create mode 100644 apps/roboshield/src/payload/blocks/MediaBlock.ts create mode 100644 apps/roboshield/src/payload/blocks/RichText.ts diff --git a/apps/roboshield/payload-types.ts b/apps/roboshield/payload-types.ts index 357b67364..6d731008f 100644 --- a/apps/roboshield/payload-types.ts +++ b/apps/roboshield/payload-types.ts @@ -57,9 +57,33 @@ export interface Page { } | { content?: - | { - [k: string]: unknown; - }[] + | ( + | { + content: { + [k: string]: unknown; + }[]; + id?: string | null; + blockName?: string | null; + blockType: "richtext"; + } + | { + image: string | Media; + id?: string | null; + blockName?: string | null; + blockType: "mediaBlock"; + } + | { + externalEmbeddFields?: { + embedType?: ("url" | "code") | null; + url?: string | null; + caption?: string | null; + code?: string | null; + }; + id?: string | null; + blockName?: string | null; + blockType: "externalEmbedd"; + } + )[] | null; id?: string | null; blockName?: string | null; @@ -70,7 +94,7 @@ export interface Page { statistics?: | { name: string; - value: number; + value: string; description: { [k: string]: unknown; }[]; diff --git a/apps/roboshield/src/components/Content/Content.tsx b/apps/roboshield/src/components/Content/Content.tsx index 529a2e06d..d01bbb7ae 100644 --- a/apps/roboshield/src/components/Content/Content.tsx +++ b/apps/roboshield/src/components/Content/Content.tsx @@ -1,14 +1,45 @@ import { Section } from "@commons-ui/core"; -import RichText, { Children } from "@/roboshield/components/RichText"; import { Page } from "@/root/payload-types"; -import { ExtractBlockType } from "@/roboshield/utils/blocks"; +import { + ExtractBlockType, + ExtractNestedBlockType, +} from "@/roboshield/utils/blocks"; +import LongFormRichText from "@/roboshield/components/LongFormRichText"; +import LongFormMedia from "@/roboshield/components/LongFormMedia"; +import LongFormExternalEmbed from "@/roboshield/components/LongFormExternalEmbed"; type ContentProps = ExtractBlockType< NonNullable[number], "content" >; +export type ExternalEmbeddBlock = ExtractNestedBlockType< + NonNullable[number], + "externalEmbedd" +>; + +export type RichTextBlock = ExtractNestedBlockType< + NonNullable[number], + "richtext" +>; + +export type MediaBlock = ExtractNestedBlockType< + NonNullable[number], + "mediaBlock" +>; + +type ComponentMap = { + richtext: (props: RichTextBlock) => JSX.Element; + mediaBlock?: (props: MediaBlock) => JSX.Element; + externalEmbedd?: (props: ExternalEmbeddBlock) => JSX.Element; +}; export default function Content({ content }: ContentProps) { + const COMPONENT_BY_CONTENT_TYPE: ComponentMap = { + richtext: LongFormRichText, + mediaBlock: LongFormMedia, + externalEmbedd: LongFormExternalEmbed, + }; + return (
- ({ - mb: "30px", - "& h2": { - typography: { xs: "h4", md: "h2" }, - }, - "& p,& a, & li": { - typography: { xs: "body1", md: "subheading" }, - mb: 2, - }, - "& a": { - textDecorationColor: theme.palette.primary.main, - }, - })} - /> + {content?.map((child) => { + const Component = COMPONENT_BY_CONTENT_TYPE[child.blockType]; + + if (Component) { + return ; + } + + return null; + })}
); } diff --git a/apps/roboshield/src/components/LongFormExternalEmbed/LongFormExternalEmbed.tsx b/apps/roboshield/src/components/LongFormExternalEmbed/LongFormExternalEmbed.tsx new file mode 100644 index 000000000..7f75a2a79 --- /dev/null +++ b/apps/roboshield/src/components/LongFormExternalEmbed/LongFormExternalEmbed.tsx @@ -0,0 +1,30 @@ +import { RichTypography } from "@commons-ui/core"; +import { Box } from "@mui/material"; +import React from "react"; +import { ExternalEmbeddBlock } from "@/roboshield/components/Content/Content"; + +export default function LongFormExternalEmbed(props: ExternalEmbeddBlock) { + const { externalEmbeddFields: { code = "", caption = "", url = "" } = {} } = + props; + + return ( + + {code && {code}} + {url && ( +