feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions

View File

@@ -2,9 +2,19 @@ import { firaMono, firaSans } from "@/app/[lang]/(live)/fonts"
import styles from "./layout.module.css"
export default function LoyaltyPagesLayout({
import {
ContentTypeParams,
LangParams,
LayoutArgs,
UIDParams,
} from "@/types/params"
export default function ContentTypeLayout({
children,
}: React.PropsWithChildren) {
params,
}: React.PropsWithChildren<
LayoutArgs<LangParams & ContentTypeParams & UIDParams>
>) {
return (
<div
className={`${firaMono.variable} ${firaSans.variable} ${styles.layout}`}

View File

@@ -0,0 +1,26 @@
import { notFound } from "next/navigation"
import ContentPage from "@/components/ContentType/ContentPage"
import LoyaltyPage from "@/components/ContentType/LoyaltyPage"
import {
ContentTypeParams,
LangParams,
PageArgs,
UIDParams,
} from "@/types/params"
export default async function ContentTypePage({
params,
}: PageArgs<LangParams & ContentTypeParams & UIDParams, {}>) {
switch (params.contentType) {
case "loyalty-page":
return <LoyaltyPage />
case "content-page":
return <ContentPage />
default:
const type: never = params.contentType
console.error(`Unsupported content type given: ${type}`)
notFound()
}
}

View File

@@ -1,3 +0,0 @@
export default async function ContentPage() {
return null
}

View File

@@ -3,7 +3,7 @@ import { AuthError } from "next-auth"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import { badRequest, internalServerError } from "@/server/errors/next"
import { internalServerError } from "@/server/errors/next"
import { signIn } from "@/auth"
@@ -22,17 +22,15 @@ export async function GET(
// Normal login request from New web
redirectTo =
request.cookies.get("redirectTo")?.value || // Cookie gets set by authRequired middleware
request.headers.get("x-redirect-to") ||
request.nextUrl.searchParams.get("redirectTo") ||
""
"/"
// If above fails, always redirect to startpage
if (!redirectTo) {
// Make relative URL to absolute URL
if (redirectTo.startsWith("/")) {
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
redirectTo = env.PUBLIC_URL
console.log({ login_fallback: redirectTo })
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
}
// Clean up cookie from authRequired middleware
@@ -104,11 +102,11 @@ export async function GET(
}
} catch (error) {
if (error instanceof AuthError) {
console.log({ signInAuthError: error })
console.error({ signInAuthError: error })
} else {
console.log({ signInError: error })
console.error({ signInError: error })
}
}
return badRequest()
return internalServerError()
}

View File

@@ -1,31 +0,0 @@
.content {
display: grid;
padding-bottom: 7.7rem;
padding-left: 0;
padding-right: 0;
position: relative;
}
.blocks {
display: grid;
gap: 4.2rem;
padding: 1.6rem;
}
@media screen and (min-width: 950px) {
.content {
gap: 2.7rem;
grid-template-columns: 30rem 1fr;
padding-bottom: 17.5rem;
padding-left: 2.4rem;
padding-right: 2.4rem;
padding-top: 5.8rem;
}
.blocks {
gap: 6.4rem;
padding-left: 0;
padding-right: 0;
grid-column: 2 / -1;
}
}

View File

@@ -1,39 +0,0 @@
import { notFound } from "next/navigation"
import { serverClient } from "@/lib/trpc/server"
import { Blocks } from "@/components/Loyalty/Blocks"
import Sidebar from "@/components/Loyalty/Sidebar"
import MaxWidth from "@/components/MaxWidth"
import styles from "./page.module.css"
import type { LangParams, PageArgs, UriParams } from "@/types/params"
export default async function LoyaltyPage({
params,
searchParams,
}: PageArgs<LangParams, UriParams>) {
try {
if (!searchParams.uri) {
throw new Error("Bad URI")
}
const loyaltyPage = await serverClient().contentstack.loyaltyPage.get({
href: searchParams.uri,
locale: params.lang,
})
return (
<section className={styles.content}>
{loyaltyPage.sidebar ? <Sidebar blocks={loyaltyPage.sidebar} /> : null}
<MaxWidth className={styles.blocks} tag="main">
{loyaltyPage.blocks ? <Blocks blocks={loyaltyPage.blocks} /> : null}
</MaxWidth>
</section>
)
} catch (err) {
return notFound()
}
}