refactor: clean up an reuse code
This commit is contained in:
@@ -11,8 +11,10 @@ type bool = {
|
||||
}
|
||||
export default async function Breadcrumbs({ b = false }: bool) {
|
||||
let breadcrumbs
|
||||
if (!b) breadcrumbs = await serverClient().contentstack.breadcrumbs.get()
|
||||
else breadcrumbs = await serverClient().contentstack.breadcrumbs.loyalty()
|
||||
if (!b)
|
||||
breadcrumbs = await serverClient().contentstack.breadcrumbs.getAccountPage()
|
||||
else
|
||||
breadcrumbs = await serverClient().contentstack.breadcrumbs.getLoyaltyPage()
|
||||
return (
|
||||
<nav className={styles.breadcrumbs}>
|
||||
<ul className={styles.list}>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#import "../Fragments/Refs/ContentPage.graphql"
|
||||
#import "../Fragments/Refs/LoyaltyPage.graphql"
|
||||
#import "../Fragments/Refs/System.graphql"
|
||||
#import "../Fragments/MyPages/Breadcrumbs.graphql"
|
||||
|
||||
query GetLoyaltyPage($locale: String!, $uid: String!) {
|
||||
loyalty_page(uid: $uid, locale: $locale) {
|
||||
@@ -127,6 +128,7 @@ query GetLoyaltyPage($locale: String!, $uid: String!) {
|
||||
}
|
||||
}
|
||||
}
|
||||
...LoyaltyPageBreadcrumbs
|
||||
system {
|
||||
uid
|
||||
created_at
|
||||
|
||||
@@ -2,68 +2,6 @@ import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const validateBreadcrumbsRefsConstenstackSchema = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: z.object({
|
||||
breadcrumbs: z.object({
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export const validateBreadcrumbsContenstackSchema = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
}),
|
||||
system: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
}),
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export const getBreadcrumbsSchema = z.array(
|
||||
z.object({
|
||||
href: z.string().optional(),
|
||||
@@ -72,26 +10,50 @@ export const getBreadcrumbsSchema = z.array(
|
||||
})
|
||||
)
|
||||
|
||||
export const validateBreadcrumbsRefsContenstackSchemaLoyalty = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: z.object({
|
||||
breadcrumbs: z.object({
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
const validateBreadcrumbs = z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
}),
|
||||
system: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
}),
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
const validateBreadcrumbsRefs = z.object({
|
||||
breadcrumbs: z.object({
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
export const validateBreadcrumbsRefsContentstackSchemaAccountPage = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: validateBreadcrumbsRefs,
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
@@ -101,31 +63,38 @@ export const validateBreadcrumbsRefsContenstackSchemaLoyalty = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export const validateBreadcrumbsContenstackSchemaLoyalty = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
export const validateBreadcrumbsContentstackSchemaAccountPage = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
parentsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
}),
|
||||
system: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
}),
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
web: validateBreadcrumbs,
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export const validateBreadcrumbsRefsContentstackSchemaLoyaltyPage = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: validateBreadcrumbsRefs,
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export const validateBreadcrumbsContentstackSchemaLoyaltyPage = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
web: validateBreadcrumbs,
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
|
||||
@@ -19,15 +19,15 @@ import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import {
|
||||
getBreadcrumbsSchema,
|
||||
validateBreadcrumbsContenstackSchema,
|
||||
validateBreadcrumbsContenstackSchemaLoyalty,
|
||||
validateBreadcrumbsRefsConstenstackSchema,
|
||||
validateBreadcrumbsRefsContenstackSchemaLoyalty,
|
||||
validateBreadcrumbsContentstackSchemaAccountPage,
|
||||
validateBreadcrumbsContentstackSchemaLoyaltyPage,
|
||||
validateBreadcrumbsRefsContentstackSchemaAccountPage,
|
||||
validateBreadcrumbsRefsContentstackSchemaLoyaltyPage,
|
||||
} from "./output"
|
||||
import {
|
||||
affix,
|
||||
getConnections,
|
||||
getConnectionsLoyalty,
|
||||
getConnectionsAccountPage,
|
||||
getConnectionsLoyaltyPage,
|
||||
homeBreadcrumbs,
|
||||
} from "./utils"
|
||||
|
||||
@@ -39,7 +39,7 @@ import type {
|
||||
} from "@/types/requests/myPages/breadcrumbs"
|
||||
|
||||
export const breadcrumbsQueryRouter = router({
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
getAccountPage: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const refsResponse = await request<GetAccountPageBreadcrumbsRefsData>(
|
||||
GetAccountPageBreadcrumbsRefs,
|
||||
{ locale: ctx.lang, url: ctx.pathname },
|
||||
@@ -49,18 +49,19 @@ export const breadcrumbsQueryRouter = router({
|
||||
},
|
||||
}
|
||||
)
|
||||
console.log([generateRefsResponseTag(ctx.lang, ctx.pathname, affix)])
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
console.log(refsResponse)
|
||||
|
||||
const validatedRefsData =
|
||||
validateBreadcrumbsRefsConstenstackSchema.safeParse(refsResponse.data)
|
||||
validateBreadcrumbsRefsContentstackSchemaAccountPage.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedRefsData.success) {
|
||||
throw internalServerError(validatedRefsData.error)
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedRefsData.data)
|
||||
const connections = getConnectionsAccountPage(validatedRefsData.data)
|
||||
const tags = generateTags(ctx.lang, connections)
|
||||
const page = validatedRefsData.data.all_account_page.items[0]
|
||||
tags.push(generateTag(ctx.lang, page.system.uid, affix))
|
||||
@@ -75,7 +76,7 @@ export const breadcrumbsQueryRouter = router({
|
||||
}
|
||||
|
||||
const validatedBreadcrumbsData =
|
||||
validateBreadcrumbsContenstackSchema.safeParse(response.data)
|
||||
validateBreadcrumbsContentstackSchemaAccountPage.safeParse(response.data)
|
||||
|
||||
if (!validatedBreadcrumbsData.success) {
|
||||
throw internalServerError(validatedBreadcrumbsData.error)
|
||||
@@ -101,7 +102,7 @@ export const breadcrumbsQueryRouter = router({
|
||||
uid: breadcrumb.system.uid,
|
||||
}
|
||||
})
|
||||
console.log(pageBreadcrumb)
|
||||
|
||||
const breadcrumbs = [
|
||||
homeBreadcrumbs[ctx.lang],
|
||||
parentBreadcrumbs,
|
||||
@@ -115,7 +116,7 @@ export const breadcrumbsQueryRouter = router({
|
||||
|
||||
return validatedBreadcrumbs.data
|
||||
}),
|
||||
loyalty: contentstackProcedure.query(async ({ ctx }) => {
|
||||
getLoyaltyPage: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const refsResponse = await request<GetLoyaltyPageBreadcrumbsRefsData>(
|
||||
GetLoyaltyPageBreadcrumbsRefs,
|
||||
{ locale: ctx.lang, url: ctx.pathname },
|
||||
@@ -125,14 +126,13 @@ export const breadcrumbsQueryRouter = router({
|
||||
},
|
||||
}
|
||||
)
|
||||
console.log(ctx.pathname)
|
||||
console.log(refsResponse)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
const validatedRefsData =
|
||||
validateBreadcrumbsRefsContenstackSchemaLoyalty.safeParse(
|
||||
validateBreadcrumbsRefsContentstackSchemaLoyaltyPage.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
|
||||
@@ -140,7 +140,7 @@ export const breadcrumbsQueryRouter = router({
|
||||
throw internalServerError(validatedRefsData.error)
|
||||
}
|
||||
|
||||
const connections = getConnectionsLoyalty(validatedRefsData.data)
|
||||
const connections = getConnectionsLoyaltyPage(validatedRefsData.data)
|
||||
const tags = generateTags(ctx.lang, connections)
|
||||
const page = validatedRefsData.data.all_loyalty_page.items[0]
|
||||
tags.push(generateTag(ctx.lang, page.system.uid, affix))
|
||||
@@ -156,12 +156,12 @@ export const breadcrumbsQueryRouter = router({
|
||||
}
|
||||
|
||||
const validatedBreadcrumbsData =
|
||||
validateBreadcrumbsContenstackSchemaLoyalty.safeParse(response.data)
|
||||
validateBreadcrumbsContentstackSchemaLoyaltyPage.safeParse(response.data)
|
||||
|
||||
if (!validatedBreadcrumbsData.success) {
|
||||
throw internalServerError(validatedBreadcrumbsData.error)
|
||||
}
|
||||
console.log(validatedBreadcrumbsData)
|
||||
|
||||
const parentBreadcrumbs =
|
||||
validatedBreadcrumbsData.data.all_loyalty_page.items[0].web.breadcrumbs.parentsConnection.edges.map(
|
||||
(breadcrumb) => {
|
||||
@@ -182,18 +182,18 @@ export const breadcrumbsQueryRouter = router({
|
||||
uid: breadcrumb.system.uid,
|
||||
}
|
||||
})
|
||||
|
||||
const breadcrumbs = [
|
||||
homeBreadcrumbs[ctx.lang],
|
||||
parentBreadcrumbs,
|
||||
pageBreadcrumb,
|
||||
].flat()
|
||||
console.log(breadcrumbs)
|
||||
|
||||
const validatedBreadcrumbs = getBreadcrumbsSchema.safeParse(breadcrumbs)
|
||||
if (!validatedBreadcrumbs.success) {
|
||||
throw internalServerError(validatedBreadcrumbs.error)
|
||||
}
|
||||
console.log(validatedBreadcrumbs.data)
|
||||
|
||||
return validatedBreadcrumbs.data
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -7,24 +7,23 @@ import type {
|
||||
import type { Edges } from "@/types/requests/utils/edges"
|
||||
import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
|
||||
export function getConnections(refs: GetAccountPageBreadcrumbsRefsData) {
|
||||
export function getConnectionsAccountPage(
|
||||
refs: GetAccountPageBreadcrumbsRefsData
|
||||
) {
|
||||
const connections: Edges<NodeRefs>[] = []
|
||||
refs.all_account_page.items.forEach((ref) => {
|
||||
console.log(ref.web.breadcrumbs)
|
||||
console.log(ref.web.breadcrumbs.parentsConnection)
|
||||
connections.push(ref.web.breadcrumbs.parentsConnection)
|
||||
})
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
export function getConnectionsLoyalty(refs: GetLoyaltyPageBreadcrumbsRefsData) {
|
||||
export function getConnectionsLoyaltyPage(
|
||||
refs: GetLoyaltyPageBreadcrumbsRefsData
|
||||
) {
|
||||
const connections: Edges<NodeRefs>[] = []
|
||||
refs.all_loyalty_page.items.forEach((ref) => {
|
||||
console.log(ref)
|
||||
connections.push(ref.web.breadcrumbs.parentsConnection)
|
||||
})
|
||||
console.log(connections)
|
||||
return connections
|
||||
}
|
||||
|
||||
|
||||
@@ -4,27 +4,7 @@ import type { System } from "../system"
|
||||
import type { AllRequestResponse } from "../utils/all"
|
||||
import type { Edges } from "../utils/edges"
|
||||
|
||||
interface AccountPageBreadcrumbs {
|
||||
web: {
|
||||
breadcrumbs: {
|
||||
title: string
|
||||
parentsConnection: Edges<{
|
||||
breadcrumbs: {
|
||||
title: string
|
||||
}
|
||||
system: {
|
||||
locale: Lang
|
||||
uid: string
|
||||
}
|
||||
url: string
|
||||
}>
|
||||
}
|
||||
system: {
|
||||
uid: string
|
||||
}
|
||||
}
|
||||
}
|
||||
interface LoyaltyPageBreadcrumbs {
|
||||
interface MyPagesBreadcrumbs {
|
||||
web: {
|
||||
breadcrumbs: {
|
||||
title: string
|
||||
@@ -45,21 +25,17 @@ interface LoyaltyPageBreadcrumbs {
|
||||
}
|
||||
}
|
||||
|
||||
interface AllAccountPageResponse
|
||||
extends AllRequestResponse<AccountPageBreadcrumbs> {}
|
||||
|
||||
interface AllLoyaltyPageResponse
|
||||
extends AllRequestResponse<LoyaltyPageBreadcrumbs> {}
|
||||
interface AllPageResponse extends AllRequestResponse<MyPagesBreadcrumbs> {}
|
||||
|
||||
export interface GetAccountPageBreadcrumbsData {
|
||||
all_account_page: AllAccountPageResponse
|
||||
all_account_page: AllPageResponse
|
||||
}
|
||||
|
||||
export interface GetLoyaltyPageBreadcrumbsData {
|
||||
all_loyalty_page: AllLoyaltyPageResponse
|
||||
all_loyalty_page: AllPageResponse
|
||||
}
|
||||
|
||||
interface AccountPageBreadcrumbRefs extends System {
|
||||
interface MyPagesBreadcrumbRefs extends System {
|
||||
web: {
|
||||
breadcrumbs: {
|
||||
parentsConnection: Edges<System>
|
||||
@@ -67,24 +43,13 @@ interface AccountPageBreadcrumbRefs extends System {
|
||||
}
|
||||
}
|
||||
|
||||
interface LoyaltyPageBreadcrumbRefs extends System {
|
||||
web: {
|
||||
breadcrumbs: {
|
||||
parentsConnection: Edges<System>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface AllAccountPageRefsResponse
|
||||
extends AllRequestResponse<AccountPageBreadcrumbRefs> {}
|
||||
|
||||
interface AllLoyaltyPageRefsResponse
|
||||
extends AllRequestResponse<LoyaltyPageBreadcrumbRefs> {}
|
||||
interface AllPageRefsResponse
|
||||
extends AllRequestResponse<MyPagesBreadcrumbRefs> {}
|
||||
|
||||
export interface GetAccountPageBreadcrumbsRefsData {
|
||||
all_account_page: AllAccountPageRefsResponse
|
||||
all_account_page: AllPageRefsResponse
|
||||
}
|
||||
|
||||
export interface GetLoyaltyPageBreadcrumbsRefsData {
|
||||
all_loyalty_page: AllLoyaltyPageRefsResponse
|
||||
all_loyalty_page: AllPageRefsResponse
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user