diff --git a/components/Blocks/index.tsx b/components/Blocks/index.tsx index e90221fcc..1f710343d 100644 --- a/components/Blocks/index.tsx +++ b/components/Blocks/index.tsx @@ -5,8 +5,8 @@ import TextCols from "@/components/Blocks/TextCols" import UspGrid from "@/components/Blocks/UspGrid" import JsonToHtml from "@/components/JsonToHtml" -import Table from "./Table" import AccordionSection from "./Accordion" +import Table from "./Table" import type { BlocksProps } from "@/types/components/blocks" import { BlocksEnums } from "@/types/enums/blocks" @@ -67,11 +67,10 @@ export default function Blocks({ blocks }: BlocksProps) { case BlocksEnums.block.UspGrid: return case BlocksEnums.block.Accordion: - console.log("Bub", block.accordion.faq) return ( ) diff --git a/components/ContentType/HotelPage/AmenitiesList/index.tsx b/components/ContentType/HotelPage/AmenitiesList/index.tsx index e5881b0bd..8b6a1977e 100644 --- a/components/ContentType/HotelPage/AmenitiesList/index.tsx +++ b/components/ContentType/HotelPage/AmenitiesList/index.tsx @@ -11,7 +11,6 @@ import { getLang } from "@/i18n/serverContext" import styles from "./amenitiesList.module.css" import type { AmenitiesListProps } from "@/types/components/hotelPage/amenities" -import type { HotelData } from "@/types/hotel" export default async function AmenitiesList({ detailedFacilities, diff --git a/lib/graphql/Fragments/Blocks/Accordion.graphql b/lib/graphql/Fragments/Blocks/Accordion.graphql index b47d22917..ea82ba484 100644 --- a/lib/graphql/Fragments/Blocks/Accordion.graphql +++ b/lib/graphql/Fragments/Blocks/Accordion.graphql @@ -21,7 +21,10 @@ fragment AccordionBlock on Accordion { edges { node { __typename + ...AccountPageLink ...ContentPageLink + ...HotelPageLink + ...LoyaltyPageLink } } } @@ -65,7 +68,10 @@ fragment SpecificFaq on ContentPageBlocksAccordionBlockFaqSpecificFaq { edges { node { __typename + ...AccountPageLink ...ContentPageLink + ...HotelPageLink + ...LoyaltyPageLink } } } @@ -104,7 +110,10 @@ fragment SpecificFaqRefs on ContentPageBlocksAccordionBlockFaqSpecificFaq { edges { node { __typename + ...AccountPageRef ...ContentPageRef + ...HotelPageRef + ...LoyaltyPageRef } } } diff --git a/lib/graphql/Query/HotelPage/HotelPage.graphql b/lib/graphql/Query/HotelPage/HotelPage.graphql index 58876f94a..e7a99fde8 100644 --- a/lib/graphql/Query/HotelPage/HotelPage.graphql +++ b/lib/graphql/Query/HotelPage/HotelPage.graphql @@ -24,18 +24,21 @@ query GetHotelPage($locale: String!, $uid: String!) { specific_faq { __typename questions { + question answer { json embedded_itemsConnection { edges { node { __typename + ...AccountPageLink ...ContentPageLink + ...HotelPageLink + ...LoyaltyPageLink } } } } - question } } } diff --git a/server/routers/contentstack/hotelPage/output.ts b/server/routers/contentstack/hotelPage/output.ts index 47e1c3d4d..e14f37d0c 100644 --- a/server/routers/contentstack/hotelPage/output.ts +++ b/server/routers/contentstack/hotelPage/output.ts @@ -2,7 +2,7 @@ import { z } from "zod" import { discriminatedUnionArray } from "@/lib/discriminatedUnion" -import { activitiesCard } from "../schemas/blocks/activitiesCard" +import { activitiesCardSchema } from "../schemas/blocks/activitiesCard" import { accordionSchema } from "../schemas/blocks/faq" import { HotelPageEnum } from "@/types/enums/hotelPage" @@ -11,7 +11,7 @@ const contentBlockActivities = z .object({ __typename: z.literal(HotelPageEnum.ContentStack.blocks.ActivitiesCard), }) - .merge(activitiesCard) + .merge(activitiesCardSchema) export const contentBlock = z.discriminatedUnion("__typename", [ contentBlockActivities, diff --git a/server/routers/contentstack/schemas/blocks/accordion.ts b/server/routers/contentstack/schemas/blocks/accordion.ts index 75a34e55c..64c646c4d 100644 --- a/server/routers/contentstack/schemas/blocks/accordion.ts +++ b/server/routers/contentstack/schemas/blocks/accordion.ts @@ -8,12 +8,13 @@ export const faqSchema = z.array( z.object({ question: z.string(), answer: z.object({ - json: z.any(), + json: z.any(), // JSON embedded_itemsConnection: z.object({ edges: z.array( z.object({ node: z .discriminatedUnion("__typename", [ + pageLinks.accountPageSchema, pageLinks.contentPageSchema, pageLinks.hotelPageSchema, pageLinks.loyaltyPageSchema, @@ -44,9 +45,9 @@ export const accordionSchema = z.object({ .literal(BlocksEnums.block.Accordion) .optional() .default(BlocksEnums.block.Accordion), - title: z.string().optional().default(""), accordion: z .object({ + title: z.string().optional().default(""), faq: z.array( z.object({ __typename: z.enum([ @@ -102,6 +103,44 @@ const actualRefs = z.discriminatedUnion("__typename", [ pageLinks.loyaltyPageRefSchema, ]) +export const globalFaqConnectionRefs = z.object({ + edges: z.array( + z.object({ + node: z.object({ + questions: z.array( + z.object({ + answer: z.object({ + embedded_itemsConnection: z.object({ + edges: z.array( + z.object({ + node: actualRefs, + }) + ), + }), + }), + }) + ), + }), + }) + ), +}) + +export const specificFaqConnectionRefs = z.object({ + questions: z.array( + z.object({ + answer: z.object({ + embedded_itemsConnection: z.object({ + edges: z.array( + z.object({ + node: actualRefs, + }) + ), + }), + }), + }) + ), +}) + export const accordionRefsSchema = z.object({ accordion: z .object({ @@ -113,46 +152,10 @@ export const accordionRefsSchema = z.object({ ]), global_faq: z .object({ - global_faqConnection: z.object({ - edges: z.array( - z.object({ - node: z.object({ - questions: z.array( - z.object({ - answer: z.object({ - embedded_itemsConnection: z.object({ - edges: z.array( - z.object({ - node: actualRefs, - }) - ), - }), - }), - }) - ), - }), - }) - ), - }), - }) - .optional(), - specific_faq: z - .object({ - questions: z.array( - z.object({ - answer: z.object({ - embedded_itemsConnection: z.object({ - edges: z.array( - z.object({ - node: actualRefs, - }) - ), - }), - }), - }) - ), + global_faqConnection: globalFaqConnectionRefs, }) .optional(), + specific_faq: specificFaqConnectionRefs.optional(), }) ), }) diff --git a/server/routers/contentstack/schemas/blocks/faq.ts b/server/routers/contentstack/schemas/blocks/faq.ts index e0c8adfd9..9810fae9f 100644 --- a/server/routers/contentstack/schemas/blocks/faq.ts +++ b/server/routers/contentstack/schemas/blocks/faq.ts @@ -1,50 +1,22 @@ import { z } from "zod" -import * as pageLinks from "@/server/routers/contentstack/schemas/pageLinks" +import { + faqSchema, + globalFaqConnectionRefs, + specificFaqConnectionRefs, +} from "./accordion" import { HotelPageEnum } from "@/types/enums/hotelPage" -export const faqSchema = z.array( - z.object({ - answer: z.object({ - json: z.any(), - embedded_itemsConnection: z.object({ - edges: z.array( - z.object({ - node: z - .discriminatedUnion("__typename", [ - pageLinks.contentPageSchema, - pageLinks.hotelPageSchema, - pageLinks.loyaltyPageSchema, - ]) - .transform((data) => { - const link = pageLinks.transform(data) - if (link) { - return link - } - return data - }), - }) - ), - }), - }), - question: z.string(), - }) -) - -export type FAQtype = z.infer // MOVE - -enum AccordionEnum { - HotelPageFaqGlobalFaqConnection = "HotelPageFaqGlobalFaqConnection", - GlobalAccordion = "GlobalAccordion", -} export const accordionSchema = z .object({ - __typename: z.enum([HotelPageEnum.ContentStack.blocks.Accordion]), + __typename: z + .literal(HotelPageEnum.ContentStack.blocks.Faq) + .optional() + .default(HotelPageEnum.ContentStack.blocks.Faq), title: z.string().optional().default(""), global_faqConnection: z .object({ - __typename: z.enum([AccordionEnum.HotelPageFaqGlobalFaqConnection]), edges: z.array( z.object({ node: z.object({ @@ -56,7 +28,6 @@ export const accordionSchema = z .optional(), specific_faq: z .object({ - __typename: z.enum([AccordionEnum.GlobalAccordion]), questions: faqSchema, }) .optional(), @@ -72,79 +43,28 @@ export const accordionSchema = z return { ...data, faq: array.flat(2) } }) -const actualRefs = z.discriminatedUnion("__typename", [ - pageLinks.accountPageRefSchema, - pageLinks.contentPageRefSchema, - pageLinks.hotelPageRefSchema, - pageLinks.loyaltyPageRefSchema, -]) - -export const accordionRefsSchema = z.object({ - global_faqConnection: z - .object({ - edges: z.array( - z.object({ - node: z.object({ - questions: z.array( - z.object({ - answer: z.object({ - embedded_itemsConnection: z.object({ - edges: z.array( - z.object({ - node: actualRefs, - }) - ), - }), - }), - }) - ), - }), - }) - ), - }) - .optional(), - specific_faq: z - .object({ - questions: z.array( - z.object({ - answer: z.object({ - embedded_itemsConnection: z.object({ - edges: z.array( - z.object({ - node: actualRefs, - }) - ), - }), - }), - }) - ), - }) - .optional(), - - /*.transform((data) => { - return data.faq.flatMap((faq) => { - switch (faq.__typename) { - case AccordionEnum.ContentPageBlocksAccordionBlockFaqGlobalFaq: - return ( - faq.global_faq?.global_faqConnection.edges.flatMap( - ({ node: faqConnection }) => { - return faqConnection.questions.flatMap((question) => - question.answer.embedded_itemsConnection.edges.flatMap( - ({ node }) => node.system - ) - ) - } - ) || [] - ) - case AccordionEnum.ContentPageBlocksAccordionBlockFaqSpecificFaq: - return ( - faq.specific_faq?.questions.flatMap((question) => - question.answer.embedded_itemsConnection.edges.flatMap( - ({ node }) => node.system - ) - ) || [] - ) - } - }) - }),*/ -}) +export const accordionRefsSchema = z + .object({ + global_faqConnection: globalFaqConnectionRefs.optional(), + specific_faq: specificFaqConnectionRefs.optional(), + }) + .transform((data) => { + const array = [] + array.push( + data.global_faqConnection?.edges.flatMap(({ node: faqConnection }) => { + return faqConnection.questions.flatMap((question) => + question.answer.embedded_itemsConnection.edges.flatMap( + ({ node }) => node.system + ) + ) + }) || [] + ) + array.push( + data.specific_faq?.questions.flatMap((question) => + question.answer.embedded_itemsConnection.edges.flatMap( + ({ node }) => node.system + ) + ) || [] + ) + return { faq: array.flat(2) } + }) diff --git a/server/routers/hotels/query.ts b/server/routers/hotels/query.ts index 5773a3092..1822e09e0 100644 --- a/server/routers/hotels/query.ts +++ b/server/routers/hotels/query.ts @@ -82,7 +82,7 @@ async function getContentstackData( if (!response.data) { throw notFound(response) } - console.log("HEJ", response.data) + const hotelPageData = hotelPageSchema.safeParse(response.data) if (!hotelPageData.success) { console.error( @@ -103,7 +103,6 @@ export const hotelQueryRouter = router({ const { include } = input const contentstackData = await getContentstackData(lang, uid) - console.log("då", contentstackData) const hotelId = contentstackData?.hotel_page_id if (!hotelId) { diff --git a/types/enums/hotelPage.ts b/types/enums/hotelPage.ts index 64c5d889c..bfae4bfbe 100644 --- a/types/enums/hotelPage.ts +++ b/types/enums/hotelPage.ts @@ -1,7 +1,7 @@ export namespace HotelPageEnum { export namespace ContentStack { export const enum blocks { - Accordion = "HotelPageFaq", + Faq = "HotelPageFaq", ActivitiesCard = "HotelPageContentUpcomingActivitiesCard", } } diff --git a/types/trpc/routers/contentstack/hotelPage.ts b/types/trpc/routers/contentstack/hotelPage.ts index 329a7b207..aa1c584c4 100644 --- a/types/trpc/routers/contentstack/hotelPage.ts +++ b/types/trpc/routers/contentstack/hotelPage.ts @@ -4,12 +4,12 @@ import { contentBlock, hotelPageSchema, } from "@/server/routers/contentstack/hotelPage/output" -import { activitiesCard } from "@/server/routers/contentstack/schemas/blocks/activitiesCard" +import { activitiesCardSchema } from "@/server/routers/contentstack/schemas/blocks/activitiesCard" // Will be extended once we introduce more functionality to our entries. export interface GetHotelPageData extends z.input {} export interface HotelPage extends z.output {} -export interface ActivitiesCard extends z.output {} +export interface ActivitiesCard extends z.output {} export type ActivityCard = ActivitiesCard["upcoming_activities_card"] export interface ContentBlock extends z.output {}