Skip to content

Commit

Permalink
clean up the queries, add home query to sanity queries and remove ext…
Browse files Browse the repository at this point in the history
…ra queries from index
  • Loading branch information
iamkevingreen committed Aug 6, 2023
1 parent bc8b900 commit f76fda2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
31 changes: 30 additions & 1 deletion app/queries/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,33 @@ const MODULE_STANDARD_TEXT = groq`
}
`

const MODULE_HERO = groq`
(_type == 'module.hero') => {
'bgColor': bgColor.hex,
'image': image.asset-> {
metadata,
url
},
text[] {
${richText}
}
}
`



const MODULES = groq`
_type,
_key,
${MODULE_STANDARD_TEXT},
${MODULE_HERO}
`

const PAGE_MODULES = groq`
_type,
_key,
${MODULE_STANDARD_TEXT},
${MODULE_HERO}
`

const pageQuery = groq`
Expand All @@ -100,6 +115,12 @@ const pageQuery = groq`
}
`

const homeQuery = groq`
'modules': modules[] {
${PAGE_MODULES}
}
`

const productQuery = groq`
_type,
'slug': store.slug.current,
Expand Down Expand Up @@ -133,4 +154,12 @@ export const QUERY_PAGE = (slug) => groq`*[
!(_id in path("drafts.**"))
][0] {
${pageQuery}
}`
}`

export const QUERY_HOME = groq`
*[_type == 'home' &&
!(_id in path("drafts.**"))] {
${homeQuery}
}[0]
`

24 changes: 5 additions & 19 deletions app/routes/($locale)._index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {Await, useLoaderData, useFetcher, Link} from '@remix-run/react';
import { AnalyticsPageType } from '@shopify/hydrogen';
import { flattenConnection } from '@shopify/hydrogen-react'

import { QUERY_HOME } from '~/queries/sanity'

import PageComponentList from '~/components/PageComponentList'

export async function loader({context}) {
Expand All @@ -12,22 +14,9 @@ export async function loader({context}) {
const products = await context.storefront.query(PRODUCT_QUERY)
const articles = await context.storefront.query(ARTICLES_QUERY)

const sanityPage = await context.sanity.fetch(`
*[_type == 'home'] {
...,
modules[] {
_type,
_key,
(_type == 'module.hero') => {
'bgColor': bgColor.hex,
'image': image.asset-> {
metadata,
url
},
text
}
}
}[0]`)
const sanityPage = await context.sanity.fetch(QUERY_HOME)

console.log('sanityPage', sanityPage)

// We're not covering accounts/auth in this class
// const customerAccessToken = await context.session.get('customerAccessToken');
Expand Down Expand Up @@ -66,9 +55,6 @@ export default function Homepage() {
return (
<div className='relative'>
<>
<div className='min-h-screen flex justify-center items-center'>
<h2 className='text-mono-64'>{sanityPage.title}</h2>
</div>
<div>
<PageComponentList components={sanityPage.modules} />
</div>
Expand Down

0 comments on commit f76fda2

Please sign in to comment.