From d9f1470eb7ae0d758595368f976657ca23a2fefb Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Mon, 29 Apr 2024 14:00:24 +0200 Subject: [PATCH] refactor: zod validation and pr comments --- .../(protected)/my-pages/layout.module.css | 3 +- .../(public)/loyalty-page/layout.module.css | 2 - .../(public)/loyalty-page/page.module.css | 1 + .../(live)/(public)/loyalty-page/page.tsx | 9 +-- app/globals.css | 1 + components/Loyalty/Blocks/CardGrid/index.tsx | 18 +++--- .../DynamicContent/HowItWorks/index.tsx | 4 +- .../DynamicContent/LoyaltyLevels/index.tsx | 4 +- .../DynamicContent/dynamicContent.module.css | 3 +- .../Loyalty/Blocks/DynamicContent/index.tsx | 36 ++++++----- components/Loyalty/Blocks/index.tsx | 10 ++-- .../JoinLoyalty/Contact/ContactRow/index.tsx | 8 +-- .../Sidebar/JoinLoyalty/Contact/index.tsx | 2 +- .../Loyalty/Sidebar/JoinLoyalty/index.tsx | 2 +- .../JoinLoyalty/joinLoyalty.module.css | 2 +- components/Loyalty/Sidebar/index.tsx | 38 +++++++----- components/Loyalty/Sidebar/sidebar.module.css | 8 +-- components/TempDesignSystem/Card/index.tsx | 4 +- lib/graphql/Query/LoyaltyPage.graphql | 7 +-- .../contentstack/contactConfig/input.ts | 5 ++ .../contentstack/contactConfig/output.ts | 60 +++++++++++++++++++ .../contentstack/contactConfig/query.ts | 46 +++++++------- server/routers/contentstack/index.ts | 2 +- .../routers/contentstack/loyaltyPage/query.ts | 38 ++++++------ server/trpc.ts | 7 ++- types/components/loyalty/blocks.ts | 3 - types/components/loyalty/sidebar.ts | 7 ++- types/requests/contactConfig.ts | 53 ---------------- types/requests/contentTypeUid.ts | 24 ++++---- utils/contactConfig.ts | 3 +- utils/contentType.ts | 19 ++++-- 31 files changed, 222 insertions(+), 207 deletions(-) create mode 100644 server/routers/contentstack/contactConfig/input.ts create mode 100644 server/routers/contentstack/contactConfig/output.ts delete mode 100644 types/requests/contactConfig.ts diff --git a/app/[lang]/(live)/(protected)/my-pages/layout.module.css b/app/[lang]/(live)/(protected)/my-pages/layout.module.css index bc197d5f7..4782b4cb4 100644 --- a/app/[lang]/(live)/(protected)/my-pages/layout.module.css +++ b/app/[lang]/(live)/(protected)/my-pages/layout.module.css @@ -1,5 +1,4 @@ .layout { - --max-width: 101.4rem; --header-height: 4.5rem; display: grid; @@ -25,4 +24,4 @@ padding-right: 2.4rem; padding-top: 5.8rem; } -} \ No newline at end of file +} diff --git a/app/[lang]/(live)/(public)/loyalty-page/layout.module.css b/app/[lang]/(live)/(public)/loyalty-page/layout.module.css index e17395732..55219a7f1 100644 --- a/app/[lang]/(live)/(public)/loyalty-page/layout.module.css +++ b/app/[lang]/(live)/(public)/loyalty-page/layout.module.css @@ -1,6 +1,4 @@ .layout { - --max-width: 101.4rem; - display: grid; font-family: var(--ff-fira-sans); background-color: var(--Brand-Coffee-Subtle); diff --git a/app/[lang]/(live)/(public)/loyalty-page/page.module.css b/app/[lang]/(live)/(public)/loyalty-page/page.module.css index 3311af02b..d6b3c2533 100644 --- a/app/[lang]/(live)/(public)/loyalty-page/page.module.css +++ b/app/[lang]/(live)/(public)/loyalty-page/page.module.css @@ -26,5 +26,6 @@ gap: 6.4rem; padding-left: 0; padding-right: 0; + grid-column: 2 / -1; } } diff --git a/app/[lang]/(live)/(public)/loyalty-page/page.tsx b/app/[lang]/(live)/(public)/loyalty-page/page.tsx index 152f7ed34..d35d2947c 100644 --- a/app/[lang]/(live)/(public)/loyalty-page/page.tsx +++ b/app/[lang]/(live)/(public)/loyalty-page/page.tsx @@ -26,13 +26,8 @@ export default async function LoyaltyPage({ return (
- + {loyaltyPage.sidebar ? : null} + diff --git a/app/globals.css b/app/globals.css index e6547e169..f6581020a 100644 --- a/app/globals.css +++ b/app/globals.css @@ -1,5 +1,6 @@ :root { font-size: 62.5%; + --max-width: 113.5rem; } * { diff --git a/components/Loyalty/Blocks/CardGrid/index.tsx b/components/Loyalty/Blocks/CardGrid/index.tsx index f288979cd..cd457f550 100644 --- a/components/Loyalty/Blocks/CardGrid/index.tsx +++ b/components/Loyalty/Blocks/CardGrid/index.tsx @@ -5,7 +5,7 @@ import Title from "@/components/Title" import styles from "./cardGrid.module.css" -import { CardGridProps, CardProps } from "@/types/components/loyalty/blocks" +import { CardGridProps } from "@/types/components/loyalty/blocks" export default function CardGrid({ card_grid }: CardGridProps) { return ( @@ -27,17 +27,15 @@ export default function CardGrid({ card_grid }: CardGridProps) {
{card_grid.cards.map((card, i) => ( - +
+ +
))}
) } - -function CardWrapper({ card }: CardProps) { - return ( -
- -
- ) -} diff --git a/components/Loyalty/Blocks/DynamicContent/HowItWorks/index.tsx b/components/Loyalty/Blocks/DynamicContent/HowItWorks/index.tsx index 426cda514..1ede3b337 100644 --- a/components/Loyalty/Blocks/DynamicContent/HowItWorks/index.tsx +++ b/components/Loyalty/Blocks/DynamicContent/HowItWorks/index.tsx @@ -4,10 +4,10 @@ import styles from "./howItWorks.module.css" export default function HowItWorks() { return ( -
+
How it works Placeholder -
+ ) } diff --git a/components/Loyalty/Blocks/DynamicContent/LoyaltyLevels/index.tsx b/components/Loyalty/Blocks/DynamicContent/LoyaltyLevels/index.tsx index 888dc3d99..5541f649d 100644 --- a/components/Loyalty/Blocks/DynamicContent/LoyaltyLevels/index.tsx +++ b/components/Loyalty/Blocks/DynamicContent/LoyaltyLevels/index.tsx @@ -33,7 +33,7 @@ export default async function LoyaltyLevels() { function LevelCard({ level }: LevelCardProps) { return ( -
+
{level.tier} {level.name}

@@ -45,6 +45,6 @@ function LevelCard({ level }: LevelCardProps) { {benefit}

))} -
+ ) } diff --git a/components/Loyalty/Blocks/DynamicContent/dynamicContent.module.css b/components/Loyalty/Blocks/DynamicContent/dynamicContent.module.css index a448e08f6..b1ebc5a58 100644 --- a/components/Loyalty/Blocks/DynamicContent/dynamicContent.module.css +++ b/components/Loyalty/Blocks/DynamicContent/dynamicContent.module.css @@ -8,7 +8,7 @@ .titleContainer { display: grid; - grid-template-areas: "title link"; + grid-template-areas: "title link" "subtitle subtitle"; grid-template-columns: 1fr max-content; padding-bottom: 0.8rem; } @@ -25,4 +25,5 @@ .subtitle { margin: 0; + grid-area: subtitle; } diff --git a/components/Loyalty/Blocks/DynamicContent/index.tsx b/components/Loyalty/Blocks/DynamicContent/index.tsx index 9f324d4fa..ebf241098 100644 --- a/components/Loyalty/Blocks/DynamicContent/index.tsx +++ b/components/Loyalty/Blocks/DynamicContent/index.tsx @@ -32,25 +32,23 @@ export default function DynamicContent({ }: DynamicContentProps) { return (
-
-
- {dynamicContent.title && ( - - {dynamicContent.title} - - )} - {dynamicContent.link ? ( - - {dynamicContent.link.text} - - ) : null} -
+
+ {dynamicContent.title && ( + + {dynamicContent.title} + + )} + {dynamicContent.link ? ( + + {dynamicContent.link.text} + + ) : null} {dynamicContent.subtitle && ( case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: return ( - <JsonToHtml - nodes={block.content.content.json.children} - embeds={block.content.content.embedded_itemsConnection.edges} - /> + <section> + <JsonToHtml + nodes={block.content.content.json.children} + embeds={block.content.content.embedded_itemsConnection.edges} + /> + </section> ) case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent: return <DynamicContentBlock dynamicContent={block.dynamic_content} /> diff --git a/components/Loyalty/Sidebar/JoinLoyalty/Contact/ContactRow/index.tsx b/components/Loyalty/Sidebar/JoinLoyalty/Contact/ContactRow/index.tsx index fe5fa82d2..1e395f5a8 100644 --- a/components/Loyalty/Sidebar/JoinLoyalty/Contact/ContactRow/index.tsx +++ b/components/Loyalty/Sidebar/JoinLoyalty/Contact/ContactRow/index.tsx @@ -5,13 +5,9 @@ import { getValueFromContactConfig } from "@/utils/contactConfig" import styles from "./contactRow.module.css" -import type { ContactFields } from "@/types/requests/contactConfig" +import type { ContactRowProps } from "@/types/components/loyalty/sidebar" -export default async function ContactRow({ - contact, -}: { - contact: ContactFields -}) { +export default async function ContactRow({ contact }: ContactRowProps) { const data = await serverClient().contentstack.contactConfig.get({ lang: Lang.en, }) diff --git a/components/Loyalty/Sidebar/JoinLoyalty/Contact/index.tsx b/components/Loyalty/Sidebar/JoinLoyalty/Contact/index.tsx index 5eeb35a66..577b4d48f 100644 --- a/components/Loyalty/Sidebar/JoinLoyalty/Contact/index.tsx +++ b/components/Loyalty/Sidebar/JoinLoyalty/Contact/index.tsx @@ -19,7 +19,7 @@ export default async function Contact({ contactBlock }: ContactProps) { case JoinLoyaltyContactTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact: return ( <ContactRow - key={`${contact.display_text}-i`} + key={`${contact.display_text}-${i}`} contact={contact} /> ) diff --git a/components/Loyalty/Sidebar/JoinLoyalty/index.tsx b/components/Loyalty/Sidebar/JoinLoyalty/index.tsx index 59ee01a0c..6fc54eb0b 100644 --- a/components/Loyalty/Sidebar/JoinLoyalty/index.tsx +++ b/components/Loyalty/Sidebar/JoinLoyalty/index.tsx @@ -28,7 +28,7 @@ export default function JoinLoyaltyContact({ block }: JoinLoyaltyContactProps) { <span>{_("Join Scandic Friends")}</span> </Button> <div className={styles.linkContainer}> - <Link href="/login" className={styles.logoutLink}> + <Link href="/login" className={styles.loginLink}> {_("Already a friend?")} <br /> {_("Click here to log in")} </Link> diff --git a/components/Loyalty/Sidebar/JoinLoyalty/joinLoyalty.module.css b/components/Loyalty/Sidebar/JoinLoyalty/joinLoyalty.module.css index a2983576f..442cb4dc0 100644 --- a/components/Loyalty/Sidebar/JoinLoyalty/joinLoyalty.module.css +++ b/components/Loyalty/Sidebar/JoinLoyalty/joinLoyalty.module.css @@ -21,7 +21,7 @@ margin: 0; } -.logoutLink { +.loginLink { text-decoration: none; color: var(--some-black-color, #2e2e2e); font-size: 1.2rem; diff --git a/components/Loyalty/Sidebar/index.tsx b/components/Loyalty/Sidebar/index.tsx index 12cdc81f6..75fdcf92f 100644 --- a/components/Loyalty/Sidebar/index.tsx +++ b/components/Loyalty/Sidebar/index.tsx @@ -7,20 +7,26 @@ import styles from "./sidebar.module.css" import { SidebarProps } from "@/types/components/loyalty/sidebar" import { SidebarTypenameEnum } from "@/types/requests/loyaltyPage" -export default function SidebarLoyalty({ block }: SidebarProps) { - switch (block.__typename) { - case SidebarTypenameEnum.LoyaltyPageSidebarContent: - return ( - <section className={styles.content}> - <JsonToHtml - embeds={block.content.content.embedded_itemsConnection.edges} - nodes={block.content.content.json.children} - /> - </section> - ) - case SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact: - return <JoinLoyaltyContact block={block.join_loyalty_contact} /> - default: - return null - } +export default function SidebarLoyalty({ blocks }: SidebarProps) { + return ( + <aside> + {blocks.map((block) => { + switch (block.__typename) { + case SidebarTypenameEnum.LoyaltyPageSidebarContent: + return ( + <section className={styles.content}> + <JsonToHtml + embeds={block.content.content.embedded_itemsConnection.edges} + nodes={block.content.content.json.children} + /> + </section> + ) + case SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact: + return <JoinLoyaltyContact block={block.join_loyalty_contact} /> + default: + return null + } + })} + </aside> + ) } diff --git a/components/Loyalty/Sidebar/sidebar.module.css b/components/Loyalty/Sidebar/sidebar.module.css index 71e960078..b757da7a0 100644 --- a/components/Loyalty/Sidebar/sidebar.module.css +++ b/components/Loyalty/Sidebar/sidebar.module.css @@ -1,9 +1,5 @@ -.content { - padding: 0 1.6rem; -} - -@media screen and (min-width: 950px) { +@media screen and (max-width: 950px) { .content { - padding: 0; + padding: 0 1.6rem; } } diff --git a/components/TempDesignSystem/Card/index.tsx b/components/TempDesignSystem/Card/index.tsx index 11523155c..afb352530 100644 --- a/components/TempDesignSystem/Card/index.tsx +++ b/components/TempDesignSystem/Card/index.tsx @@ -15,7 +15,7 @@ export default function Card({ openInNewTab = false, }: CardProps) { return ( - <div className={styles.linkCard}> + <article className={styles.linkCard}> {title ? ( <Title level="h3" weight="semiBold"> {title} @@ -33,6 +33,6 @@ export default function Card({ </Link> </Button> ) : null} - </div> + </article> ) } diff --git a/lib/graphql/Query/LoyaltyPage.graphql b/lib/graphql/Query/LoyaltyPage.graphql index 743df0539..b308e6680 100644 --- a/lib/graphql/Query/LoyaltyPage.graphql +++ b/lib/graphql/Query/LoyaltyPage.graphql @@ -7,8 +7,8 @@ query GetLoyaltyPage($locale: String!, $url: String!) { all_loyalty_page(where: { url: $url, locale: $locale }) { items { blocks { + __typename ... on LoyaltyPageBlocksDynamicContent { - __typename dynamic_content { title subtitle @@ -28,7 +28,6 @@ query GetLoyaltyPage($locale: String!, $url: String!) { } } ... on LoyaltyPageBlocksCardGrid { - __typename card_grid { title subtitle @@ -51,7 +50,6 @@ query GetLoyaltyPage($locale: String!, $url: String!) { } } ... on LoyaltyPageBlocksContent { - __typename content { content { json @@ -70,8 +68,8 @@ query GetLoyaltyPage($locale: String!, $url: String!) { } title sidebar { + __typename ... on LoyaltyPageSidebarJoinLoyaltyContact { - __typename join_loyalty_contact { title preamble @@ -87,7 +85,6 @@ query GetLoyaltyPage($locale: String!, $url: String!) { } } ... on LoyaltyPageSidebarContent { - __typename content { content { json diff --git a/server/routers/contentstack/contactConfig/input.ts b/server/routers/contentstack/contactConfig/input.ts new file mode 100644 index 000000000..b6be31232 --- /dev/null +++ b/server/routers/contentstack/contactConfig/input.ts @@ -0,0 +1,5 @@ +import { z } from "zod" + +import { Lang } from "@/constants/languages" + +export const getConfigInput = z.object({ lang: z.nativeEnum(Lang) }) diff --git a/server/routers/contentstack/contactConfig/output.ts b/server/routers/contentstack/contactConfig/output.ts new file mode 100644 index 000000000..c42a19dc9 --- /dev/null +++ b/server/routers/contentstack/contactConfig/output.ts @@ -0,0 +1,60 @@ +import { z } from "zod" + +// Help me write this zod schema based on the type ContactConfig +export const validateContactConfigSchema = z.object({ + all_contact_config: z.object({ + items: z.array( + z.object({ + email: z.object({ + name: z.string().nullable(), + address: z.string().nullable(), + }), + email_loyalty: z.object({ + name: z.string().nullable(), + address: z.string().nullable(), + }), + mailing_address: z.object({ + zip: z.string().nullable(), + street: z.string().nullable(), + name: z.string().nullable(), + city: z.string().nullable(), + country: z.string().nullable(), + }), + phone: z.object({ + number: z.string().nullable(), + name: z.string().nullable(), + }), + phone_loyalty: z.object({ + number: z.string().nullable(), + name: z.string().nullable(), + }), + visiting_address: z.object({ + zip: z.string().nullable(), + country: z.string().nullable(), + city: z.string().nullable(), + street: z.string().nullable(), + }), + }) + ), + }), +}) + +export enum ContactFieldGroupsEnum { + email = "email", + email_loyalty = "email_loyalty", + mailing_address = "mailing_address", + phone = "phone", + phone_loyalty = "phone_loyalty", + visiting_address = "visiting_address", +} + +export type ContactFieldGroups = keyof typeof ContactFieldGroupsEnum + +export type ContactConfigData = z.infer<typeof validateContactConfigSchema> + +export type ContactConfig = ContactConfigData["all_contact_config"]["items"][0] + +export type ContactFields = { + display_text?: string + contact_field: string +} diff --git a/server/routers/contentstack/contactConfig/query.ts b/server/routers/contentstack/contactConfig/query.ts index 83d183c54..6985aee67 100644 --- a/server/routers/contentstack/contactConfig/query.ts +++ b/server/routers/contentstack/contactConfig/query.ts @@ -1,32 +1,34 @@ -import { z } from "zod" - +import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql" +import { request } from "@/lib/graphql/request" import { badRequestError } from "@/server/errors/trpc" import { publicProcedure, router } from "@/server/trpc" -import { request } from "@/lib/graphql/request" -import { Lang } from "@/constants/languages" -import GetContactConfig from "@/lib/graphql/Query/ContactConfig.graphql" - -import type { GetContactConfigData } from "@/types/requests/contactConfig" +import { getConfigInput } from "./input" +import { type ContactConfigData, validateContactConfigSchema } from "./output" export const contactConfigQueryRouter = router({ - get: publicProcedure - .input(z.object({ lang: z.nativeEnum(Lang) })) - .query(async ({ input }) => { - const contactConfig = await request<GetContactConfigData>( - GetContactConfig, - { - locale: input.lang, - }, - { - tags: [`contact-config-${input.lang}`], - } - ) + get: publicProcedure.input(getConfigInput).query(async ({ input }) => { + try { + const contactConfig = await request<ContactConfigData>(GetContactConfig, { + locale: input.lang, + }) - if (contactConfig.data && contactConfig.data.all_contact_config.total) { - return contactConfig.data.all_contact_config.items[0] + if (!contactConfig.data) { + throw badRequestError() } + const validatedContactConfigConfig = + validateContactConfigSchema.safeParse(contactConfig.data) + + if (!validatedContactConfigConfig.success) { + console.error(validatedContactConfigConfig.error) + throw badRequestError() + } + + return validatedContactConfigConfig.data.all_contact_config.items[0] + } catch (e) { + console.log(e) throw badRequestError() - }), + } + }), }) diff --git a/server/routers/contentstack/index.ts b/server/routers/contentstack/index.ts index 3f765813d..2841129f4 100644 --- a/server/routers/contentstack/index.ts +++ b/server/routers/contentstack/index.ts @@ -1,8 +1,8 @@ import { router } from "@/server/trpc" import { breadcrumbsRouter } from "./breadcrumbs" -import { loyaltyPageRouter } from "./loyaltyPage" import { contactConfigRouter } from "./contactConfig" +import { loyaltyPageRouter } from "./loyaltyPage" export const contentstackRouter = router({ breadcrumbs: breadcrumbsRouter, diff --git a/server/routers/contentstack/loyaltyPage/query.ts b/server/routers/contentstack/loyaltyPage/query.ts index 0169f7b41..6a23f2a66 100644 --- a/server/routers/contentstack/loyaltyPage/query.ts +++ b/server/routers/contentstack/loyaltyPage/query.ts @@ -31,6 +31,7 @@ export const loyaltyPageQueryRouter = router({ ) if (!validatedLoyaltyPage.success) { + console.error(validatedLoyaltyPage.error) throw badRequestError() } @@ -68,15 +69,13 @@ export const loyaltyPageQueryRouter = router({ cards: block.card_grid.cards.map((card) => { return { ...card, - link: - card.referenceConnection.totalCount > 0 - ? { - href: card.referenceConnection.edges[0].node - .url, - title: - card.referenceConnection.edges[0].node.title, - } - : undefined, + link: card.referenceConnection.totalCount + ? { + href: card.referenceConnection.edges[0].node.url, + title: + card.referenceConnection.edges[0].node.title, + } + : undefined, } }), }, @@ -86,17 +85,16 @@ export const loyaltyPageQueryRouter = router({ ...block, dynamic_content: { ...block.dynamic_content, - link: - block.dynamic_content.link.pageConnection.totalCount > 0 - ? { - text: block.dynamic_content.link.text, - href: block.dynamic_content.link.pageConnection - .edges[0].node.url, - title: - block.dynamic_content.link.pageConnection.edges[0] - .node.title, - } - : undefined, + link: block.dynamic_content.link.pageConnection.totalCount + ? { + text: block.dynamic_content.link.text, + href: block.dynamic_content.link.pageConnection + .edges[0].node.url, + title: + block.dynamic_content.link.pageConnection.edges[0] + .node.title, + } + : undefined, }, } case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: diff --git a/server/trpc.ts b/server/trpc.ts index 428e38796..95d575418 100644 --- a/server/trpc.ts +++ b/server/trpc.ts @@ -1,11 +1,12 @@ import { initTRPC } from "@trpc/server" import { env } from "@/env/server" -import { transformer } from "./transformer" -import { unauthorizedError } from "./errors/trpc" -import type { Context } from "./context" +import { unauthorizedError } from "./errors/trpc" +import { transformer } from "./transformer" + import type { Meta } from "@/types/trpc/meta" +import type { Context } from "./context" const t = initTRPC.context<Context>().meta<Meta>().create({ transformer }) diff --git a/types/components/loyalty/blocks.ts b/types/components/loyalty/blocks.ts index b3fc03c85..2c73c8d58 100644 --- a/types/components/loyalty/blocks.ts +++ b/types/components/loyalty/blocks.ts @@ -1,7 +1,6 @@ import { Block, CardGrid, - CardGridCard, DynamicContent, RteBlockContent, } from "@/server/routers/contentstack/loyaltyPage/output" @@ -18,8 +17,6 @@ export type DynamicComponentProps = { component: DynamicContent["dynamic_content"]["component"] } -export type CardProps = { card: CardGridCard } - export type CardGridProps = Pick<CardGrid, "card_grid"> export type Content = { content: RteBlockContent["content"]["content"] } diff --git a/types/components/loyalty/sidebar.ts b/types/components/loyalty/sidebar.ts index e1bdb1098..60ef2b99f 100644 --- a/types/components/loyalty/sidebar.ts +++ b/types/components/loyalty/sidebar.ts @@ -1,10 +1,11 @@ +import { ContactFields } from "@/server/routers/contentstack/contactConfig/output" import { JoinLoyaltyContact, Sidebar, } from "@/server/routers/contentstack/loyaltyPage/output" export type SidebarProps = { - block: Sidebar + blocks: Sidebar[] } export type JoinLoyaltyContactProps = { @@ -14,3 +15,7 @@ export type JoinLoyaltyContactProps = { export type ContactProps = { contactBlock: JoinLoyaltyContact["join_loyalty_contact"]["contact"] } + +export type ContactRowProps = { + contact: ContactFields +} diff --git a/types/requests/contactConfig.ts b/types/requests/contactConfig.ts deleted file mode 100644 index d68090d16..000000000 --- a/types/requests/contactConfig.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { AllRequestResponse } from "./utils/all" - -export type ContactConfig = { - email: { - name?: string - address?: string - } - email_loyalty: { - name?: string - address?: string - } - mailing_address: { - zip?: string - street?: string - name?: string - city?: string - country?: string - } - phone: { - number?: string - name?: string - } - phone_loyalty: { - number?: string - name?: string - } - visiting_address: { - zip?: string - country?: string - city?: string - street?: string - } -} - -export enum ContactFieldGroupsEnum { - email = "email", - email_loyalty = "email_loyalty", - mailing_address = "mailing_address", - phone = "phone", - phone_loyalty = "phone_loyalty", - visiting_address = "visiting_address", -} - -export type ContactFieldGroups = keyof typeof ContactFieldGroupsEnum - -export type GetContactConfigData = { - all_contact_config: AllRequestResponse<ContactConfig> -} - -export type ContactFields = { - display_text?: string - contact_field: string -} diff --git a/types/requests/contentTypeUid.ts b/types/requests/contentTypeUid.ts index e8f358d07..5caabfa08 100644 --- a/types/requests/contentTypeUid.ts +++ b/types/requests/contentTypeUid.ts @@ -1,11 +1,13 @@ -export type GetContentTypeUidType = { - all_content_page: { - total: number - } - all_loyalty_page: { - total: number - } - all_current_blocks_page: { - total: number - } -} +import z from "zod" + +export const validateContentTypeUid = z.object({ + all_content_page: z.object({ + total: z.number(), + }), + all_loyalty_page: z.object({ + total: z.number(), + }), + all_current_blocks_page: z.object({ + total: z.number(), + }), +}) diff --git a/utils/contactConfig.ts b/utils/contactConfig.ts index 3eed6da76..7a888fe0a 100644 --- a/utils/contactConfig.ts +++ b/utils/contactConfig.ts @@ -1,7 +1,7 @@ import { ContactConfig, ContactFieldGroups, -} from "@/types/requests/contactConfig" +} from "@/server/routers/contentstack/contactConfig/output" export function getValueFromContactConfig( keyString: string, @@ -16,4 +16,5 @@ export function getValueFromContactConfig( return fieldGroup[key] } + return undefined } diff --git a/utils/contentType.ts b/utils/contentType.ts index 2d3668c90..af30d5815 100644 --- a/utils/contentType.ts +++ b/utils/contentType.ts @@ -1,10 +1,10 @@ -import { DocumentNode } from "graphql" +import { DocumentNode, print } from "graphql" import { Lang } from "@/constants/languages" import { env } from "@/env/server" -import GetContentTypeUid from "@/lib/graphql/Query/ContentTypeUid.graphql" +import { GetContentTypeUid } from "@/lib/graphql/Query/ContentTypeUid.graphql" -import type { GetContentTypeUidType } from "@/types/requests/contentTypeUid" +import { validateContentTypeUid } from "@/types/requests/contentTypeUid" export enum PageTypeEnum { CurrentBlocksPage = "CurrentBlocksPage", @@ -16,7 +16,6 @@ export async function getContentTypeByPathName( pathNameWithoutLang: string, lang = Lang.en ) { - const print = (await import("graphql/language/printer")).print const result = await fetch(env.CMS_URL, { method: "POST", headers: { @@ -33,7 +32,17 @@ export async function getContentTypeByPathName( }) const pageTypeData = await result.json() - const pageType = pageTypeData.data as GetContentTypeUidType + + const validatedContentTypeUid = validateContentTypeUid.safeParse( + pageTypeData.data + ) + + if (!validatedContentTypeUid.success) { + console.error(validatedContentTypeUid.error) + return null + } + + const pageType = validatedContentTypeUid.data if (pageType.all_content_page.total) { return PageTypeEnum.ContentPage