Skip to content

Commit

Permalink
refactor: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
magicspon committed Dec 29, 2023
1 parent 839b7a3 commit 8cd0a40
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
7 changes: 3 additions & 4 deletions apps/web/app/(site)/[...page]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as React from 'react'
import { LiveQuery } from 'next-sanity/preview/live-query'
import { draftMode } from 'next/headers'
import { Page, PagePreview, getData, Props } from '~pages/Page'

export default async function Index({ params }: Props) {
const resp = await getData({ params })
const { draftMode, id, ...resp } = await getData({ params })

return (
<LiveQuery
enabled={draftMode().isEnabled}
enabled={draftMode}
query={resp.query}
initialData={resp.data}
as={PagePreview}
params={{ id: resp.id }}
params={{ id }}
>
<Page {...resp} />
</LiveQuery>
Expand Down
8 changes: 3 additions & 5 deletions apps/web/app/(site)/blog/[category]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import * as React from 'react'
import { LiveQuery } from 'next-sanity/preview/live-query'
import { draftMode } from 'next/headers'
import {
PostCategory,
PostCategoryPreview,
Props,
getData,
} from '~pages/PostCategory'
import { blogCategoryQuery } from 'cms/queries/pages/blog.query'

export default async function Index({ params }: Props) {
const { data } = await getData({ params })
const { data, query, draftMode } = await getData({ params })

return (
<LiveQuery
enabled={draftMode().isEnabled}
query={blogCategoryQuery.query}
enabled={draftMode}
query={query}
initialData={data}
as={PostCategoryPreview}
params={params}
Expand Down
8 changes: 3 additions & 5 deletions apps/web/app/(site)/blog/post/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as React from 'react'
import { LiveQuery } from 'next-sanity/preview/live-query'
import { draftMode } from 'next/headers'
import { Post, PostPreview, Props, getData } from '~pages/Post'
import { blogPostQuery } from 'cms/queries/pages/blog.query'

export default async function Index({ params }: Props) {
const { data } = await getData({ params })
const { data, query, draftMode } = await getData({ params })

return (
<LiveQuery
enabled={draftMode().isEnabled}
query={blogPostQuery.query}
enabled={draftMode}
query={query}
initialData={data}
as={PostPreview}
params={params}
Expand Down
8 changes: 3 additions & 5 deletions apps/web/app/(site)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { homeQuery } from 'cms/queries/pages/home.query'
import * as React from 'react'
import { LiveQuery } from 'next-sanity/preview/live-query'
import { draftMode } from 'next/headers'
import { HomePage, HomePagePreview, getData } from '~pages/HomePage'

export default async function Index() {
const { data } = await getData()
const { data, draftMode, query } = await getData()

return (
<LiveQuery
enabled={draftMode().isEnabled}
query={homeQuery.query}
enabled={draftMode}
query={query}
initialData={data}
as={HomePagePreview}
>
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/pages/HomePage/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { homeQuery } from 'cms/queries/pages/home.query'
import { createSanityFetcher } from '~utils/createSanityFetcher'

export async function getData() {
const [draftMopde, runner] = createSanityFetcher()
const [draftMode, runner] = createSanityFetcher()
const data = await runner(homeQuery, {}, { next: { tags: ['home'] } })
// const allPagesMetadata = await getAllPageMetadata()
return { data, draftMopde }
return { data, draftMode, query: homeQuery.query }
}
17 changes: 10 additions & 7 deletions apps/web/app/pages/Page/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ export type Props = {
}
}

export type PageProps = {
type: 'page'
data: PageQuery
type CoreProps = {
id: string
query: string
draftMode: boolean
}

export type PageProps = CoreProps & {
type: 'page'
data: PageQuery
}
export type ListingProps = {
export type ListingProps = CoreProps & {
type: 'listing'
data: ListingQuery
id: string
query: string
}

export async function getData({ params }: Props) {
const [, runner] = createSanityFetcher()
const [draftMode, runner] = createSanityFetcher()
const pageMeta = await getPageMetaData(`/${params.page.join('/')}`)
if (!pageMeta) notFound()
const { _id: id, type } = pageMeta
Expand All @@ -45,5 +47,6 @@ export async function getData({ params }: Props) {
data,
id,
type,
draftMode,
} as PageProps | ListingProps
}
4 changes: 2 additions & 2 deletions apps/web/app/pages/Post/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type Props = {
}

export async function getData({ params }: Props) {
const [draftMopde, runner] = createSanityFetcher()
const [draftMode, runner] = createSanityFetcher()
const data = await runner(
blogPostQuery,
{
Expand All @@ -17,5 +17,5 @@ export async function getData({ params }: Props) {
{ next: { tags: ['post'] } },
)
// const allPagesMetadata = await getAllPageMetadata()
return { data, draftMopde }
return { data, draftMode, query: blogPostQuery.query }
}
4 changes: 2 additions & 2 deletions apps/web/app/pages/PostCategory/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export type Props = {
}

export async function getData({ params }: Props) {
const [draftMopde, runner] = createSanityFetcher()
const [draftMode, runner] = createSanityFetcher()
const data = await runner(blogCategoryQuery, params, {
next: { tags: ['category'] },
})
// const allPagesMetadata = await getAllPageMetadata()
return { data, draftMopde }
return { data, draftMode, query: blogCategoryQuery.query }
}

0 comments on commit 8cd0a40

Please sign in to comment.