refactor: DRY up trpc query
This commit is contained in:
@@ -7,11 +7,10 @@ import styles from "./page.module.css"
|
|||||||
import type { LangParams, PageArgs } from "@/types/params"
|
import type { LangParams, PageArgs } from "@/types/params"
|
||||||
|
|
||||||
export default async function BenefitsPage({ params }: PageArgs<LangParams>) {
|
export default async function BenefitsPage({ params }: PageArgs<LangParams>) {
|
||||||
const accountPage = await serverClient().contentstack.accountPage.getBenefits(
|
const accountPage = await serverClient().contentstack.accountPage.get({
|
||||||
{
|
url: "/my-pages/benefits",
|
||||||
lang: params.lang,
|
lang: params.lang,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className={styles.container}>
|
<main className={styles.container}>
|
||||||
|
|||||||
@@ -9,11 +9,10 @@ import styles from "./page.module.css"
|
|||||||
import type { LangParams, PageArgs } from "@/types/params"
|
import type { LangParams, PageArgs } from "@/types/params"
|
||||||
|
|
||||||
export default async function MyPageOverview({ params }: PageArgs<LangParams>) {
|
export default async function MyPageOverview({ params }: PageArgs<LangParams>) {
|
||||||
const accountPage = await serverClient().contentstack.accountPage.getOverview(
|
const accountPage = await serverClient().contentstack.accountPage.get({
|
||||||
{
|
url: "/my-pages/overview",
|
||||||
lang: params.lang,
|
lang: params.lang,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MaxWidth className={styles.blocks} tag="main">
|
<MaxWidth className={styles.blocks} tag="main">
|
||||||
|
|||||||
@@ -2,4 +2,7 @@ import { z } from "zod"
|
|||||||
|
|
||||||
import { Lang } from "@/constants/languages"
|
import { Lang } from "@/constants/languages"
|
||||||
|
|
||||||
export const getAccountPageInput = z.object({ lang: z.nativeEnum(Lang) })
|
export const getAccountPageInput = z.object({
|
||||||
|
url: z.string(),
|
||||||
|
lang: z.nativeEnum(Lang),
|
||||||
|
})
|
||||||
|
|||||||
@@ -15,14 +15,11 @@ import { Edges } from "@/types/requests/utils/edges"
|
|||||||
import { RTEDocument } from "@/types/rte/node"
|
import { RTEDocument } from "@/types/rte/node"
|
||||||
|
|
||||||
export const accountPageQueryRouter = router({
|
export const accountPageQueryRouter = router({
|
||||||
getOverview: publicProcedure
|
get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => {
|
||||||
.input(getAccountPageInput)
|
|
||||||
.query(async ({ input }) => {
|
|
||||||
try {
|
try {
|
||||||
const url = "/my-pages/overview"
|
|
||||||
const response = await request<GetAccountPageData>(GetAccountPage, {
|
const response = await request<GetAccountPageData>(GetAccountPage, {
|
||||||
locale: input.lang,
|
locale: input.lang,
|
||||||
url,
|
url: input.url,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
@@ -72,61 +69,4 @@ export const accountPageQueryRouter = router({
|
|||||||
throw internalServerError()
|
throw internalServerError()
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
getBenefits: publicProcedure
|
|
||||||
.input(getAccountPageInput)
|
|
||||||
.query(async ({ input }) => {
|
|
||||||
try {
|
|
||||||
const url = "/my-pages/benefits"
|
|
||||||
const response = await request<GetAccountPageData>(GetAccountPage, {
|
|
||||||
locale: input.lang,
|
|
||||||
url,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.data) {
|
|
||||||
throw badRequestError()
|
|
||||||
}
|
|
||||||
|
|
||||||
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
|
||||||
response.data
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!validatedAccountPage.success) {
|
|
||||||
throw badRequestError()
|
|
||||||
}
|
|
||||||
|
|
||||||
const content =
|
|
||||||
validatedAccountPage.data.all_account_page.items[0].content.map(
|
|
||||||
(block) => {
|
|
||||||
switch (block.__typename) {
|
|
||||||
case ContentEntries.AccountPageContentDynamicContent:
|
|
||||||
case ContentEntries.AccountPageContentShortcuts:
|
|
||||||
return block
|
|
||||||
case ContentEntries.AccountPageContentTextContent:
|
|
||||||
return {
|
|
||||||
...block,
|
|
||||||
text_content: {
|
|
||||||
content: {
|
|
||||||
json: block.text_content.content.json as RTEDocument,
|
|
||||||
embedded_itemsConnection: block.text_content.content
|
|
||||||
.embedded_itemsConnection as Edges<Embeds>,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const accountPage = {
|
|
||||||
...validatedAccountPage.data.all_account_page.items[0],
|
|
||||||
content,
|
|
||||||
} as AccountPage
|
|
||||||
return accountPage
|
|
||||||
} catch (error) {
|
|
||||||
console.info(`Get Account Page Benefits Error`)
|
|
||||||
console.error(error)
|
|
||||||
throw internalServerError()
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user