Files
web/components/MyPages/AccountPage/Content.tsx
2024-05-03 08:16:51 +02:00

105 lines
3.0 KiB
TypeScript

import { Lang } from "@/constants/languages"
import {
AccountPageContentItem,
ContentEntries,
DynamicContent,
DynamicContentComponents,
} from "@/types/requests/myPages/accountpage"
import { User } from "@/types/user"
import CurrentBenefitsBlock from "@/components/MyPages/Blocks/Benefits/CurrentLevel"
import JsonToHtml from "@/components/JsonToHtml"
import NextLevelBenefitsBlock from "@/components/MyPages/Blocks/Benefits/NextLevel"
import Overview from "@/components/MyPages/Blocks/Overview"
import Shortcuts from "@/components/MyPages/Blocks/Shortcuts"
import UpcomingStays from "@/components/MyPages/Blocks/Stays/Upcoming"
import SoonestStays from "@/components/MyPages/Blocks/Stays/Soonest"
import { renderOptions } from "@/components/JsonToHtml/renderOptions"
function DynamicComponent({
user,
lang,
content,
}: {
content: DynamicContent
lang: Lang
user: User
}) {
const componentProps = {
title: content.title,
// TODO: rename preamble to subtitle in Contentstack
subtitle: content.preamble,
link: content.link.linkConnection.edges.length
? {
href: content.link.linkConnection.edges[0].node.url,
text: content.link.link_text,
}
: null,
}
switch (content.component) {
case DynamicContentComponents.membership_overview:
return <Overview user={user} />
case DynamicContentComponents.previous_stays:
return null
case DynamicContentComponents.soonest_stays:
return <SoonestStays lang={lang} {...componentProps} />
case DynamicContentComponents.upcoming_stays:
return <UpcomingStays lang={lang} {...componentProps} />
case DynamicContentComponents.current_benefits:
return <CurrentBenefitsBlock {...componentProps} />
case DynamicContentComponents.next_benefits:
return <NextLevelBenefitsBlock {...componentProps} />
default:
return null
}
}
export default function Content({
user,
lang,
content,
}: {
user: User
lang: Lang
content: AccountPageContentItem[]
}) {
return (
<>
{content.map((item) => {
switch (item.__typename) {
case ContentEntries.AccountPageContentDynamicContent:
return (
<DynamicComponent
user={user}
lang={lang}
content={item.dynamic_content}
/>
)
case ContentEntries.AccountPageContentShortcuts:
const shortcuts = item.shortcuts.shortcuts.map(
(shortcut) => shortcut.linkConnection.edges[0].node
)
return (
<Shortcuts
shortcuts={shortcuts}
subtitle={item.preamble}
title={item.title}
/>
)
case ContentEntries.AccountPageContentTextContent:
return (
<JsonToHtml
embeds={[]}
nodes={item.text_content.content.json.children}
renderOptions={renderOptions}
/>
)
default:
return null
}
})}
</>
)
}