Files
web/components/MyPages/Blocks/Benefits/CurrentLevel/index.tsx
Michael Zetterberg bc84122a40 fix(SW-236): properly handle expired token in webviews
Trying out a new pattern for errors in data fetching.

Next.js is not a fan of throwing errors. Instead it recommends returning
different shapes for each state. Ref:
https://nextjs.org/docs/app/building-your-application/routing/error-handling#handling-expected-errors-from-server-components

It requires some more detailing and a bit more refactoring in non webview part,
but it is a start. This webview specific implementation should not break web.
2024-08-13 16:54:33 +02:00

66 lines
2.1 KiB
TypeScript

import { MembershipLevelEnum } from "@/constants/membershipLevels"
import { serverClient } from "@/lib/trpc/server"
import SectionContainer from "@/components/Section/Container"
import SectionHeader from "@/components/Section/Header"
import SectionLink from "@/components/Section/Link"
import Grids from "@/components/TempDesignSystem/Grids"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getMembershipLevelObject } from "@/utils/membershipLevel"
import { getMembership } from "@/utils/user"
import styles from "./current.module.css"
import type { AccountPageComponentProps } from "@/types/components/myPages/myPage/accountPage"
import type { LangParams } from "@/types/params"
export default async function CurrentBenefitsBlock({
title,
subtitle,
link,
lang,
}: AccountPageComponentProps & LangParams) {
const user = await serverClient().user.get()
// TAKE NOTE: we need clarification on how benefits stack from different levels
// in order to determine if a benefit is specific to a level or if it is a cumulative benefit
// we might have to add a new boolean property "exclusive" or similar
if (!user || "error" in user) {
return null
}
const membership = getMembership(user.memberships)
if (!membership) {
// TODO: handle this case?
return null
}
const currentLevel = getMembershipLevelObject(
user.memberships[0].membershipLevel as MembershipLevelEnum,
lang
)
if (!currentLevel) {
// TODO: handle this case?
return null
}
return (
<SectionContainer>
<SectionHeader title={title} link={link} subtitle={subtitle} />
<Grids.Stackable>
{currentLevel.benefits.map((benefit, idx) => (
<article className={styles.card} key={`${currentLevel}-${idx}`}>
<Title
as="h5"
level="h3"
textAlign="center"
textTransform="uppercase"
>
{benefit.title}
</Title>
</article>
))}
</Grids.Stackable>
<SectionLink link={link} variant="mobile" />
</SectionContainer>
)
}