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.
This commit is contained in:
Michael Zetterberg
2024-08-10 01:02:03 +02:00
parent c0284cd56c
commit bc84122a40
12 changed files with 64 additions and 32 deletions

View File

@@ -1,16 +0,0 @@
import { serverClient } from "@/lib/trpc/server"
import MyPagesSidebar from "@/components/MyPages/Sidebar"
import { LangParams } from "@/types/params"
export default async function MyPagesNavigation({ lang }: LangParams) {
const user = await serverClient().user.name()
// Check if we have user, that means we are logged in.
if (!user) {
return null
}
return <MyPagesSidebar lang={lang} />
}

View File

@@ -1,7 +1,7 @@
import JsonToHtml from "@/components/JsonToHtml"
import SidebarMyPages from "@/components/MyPages/Sidebar"
import JoinLoyaltyContact from "./JoinLoyalty"
import MyPagesNavigation from "./MyPagesNavigation"
import styles from "./sidebar.module.css"
@@ -44,7 +44,7 @@ export default function SidebarLoyalty({
switch (block.dynamic_content.component) {
case LoyaltySidebarDynamicComponentEnum.my_pages_navigation:
return (
<MyPagesNavigation
<SidebarMyPages
key={`${block.__typename}-${idx}`}
lang={lang}
/>

View File

@@ -24,7 +24,7 @@ export default async function CurrentBenefitsBlock({
// 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) {
if (!user || "error" in user) {
return null
}
const membership = getMembership(user.memberships)

View File

@@ -25,7 +25,7 @@ export default async function NextLevelBenefitsBlock({
}: AccountPageComponentProps) {
const { formatMessage } = await getIntl()
const user = await serverClient().user.get()
if (!user) {
if (!user || "error" in user) {
return null
}
const nextLevel = getMembershipLevelObject(

View File

@@ -21,7 +21,7 @@ export default async function Overview({
lang,
}: AccountPageComponentProps & LangParams) {
const user = await serverClient().user.get()
if (!user) {
if (!user || "error" in user) {
return null
}

View File

@@ -21,7 +21,7 @@ export default async function PointsOverview({
lang,
}: AccountPageComponentProps & LangParams) {
const user = await serverClient().user.get()
if (!user) {
if (!user || "error" in user) {
return null
}

View File

@@ -12,7 +12,7 @@ import styles from "./sidebar.module.css"
import type { LangParams } from "@/types/params"
export default async function Sidebar({ lang }: LangParams) {
export default async function SidebarMyPages({ lang }: LangParams) {
const navigation = await serverClient().contentstack.myPages.navigation.get()
const { formatMessage } = await getIntl()
if (!navigation) {