Tried some updates

This commit is contained in:
Henrik Larsson 2023-08-13 14:36:35 +02:00
parent ac9d8c4386
commit 524b9c5818
9 changed files with 41 additions and 34 deletions

View File

@ -2,9 +2,10 @@ import getQueryFromSlug from '@/helpers/get-query-from-slug';
import { clientFetch } from 'lib/sanity/sanity.client'; import { clientFetch } from 'lib/sanity/sanity.client';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';
import CategoryPage from './components/category-page'; import CategoryPage from './pages/category-page';
import ProductPage from './components/product-page'; import HomePage from './pages/home-page';
import SinglePage from './components/single-page'; import ProductPage from './pages/product-page';
import SinglePage from './pages/single-page';
export const runtime = 'edge'; export const runtime = 'edge';
@ -48,6 +49,7 @@ export default async function Page({ params }: PageParams) {
return ( return (
<> <>
{docType === 'home' && <HomePage query={query} queryParams={queryParams} />}
{docType === 'page' && <SinglePage query={query} queryParams={queryParams} />} {docType === 'page' && <SinglePage query={query} queryParams={queryParams} />}
{docType === 'product' && <ProductPage query={query} queryParams={queryParams} />} {docType === 'product' && <ProductPage query={query} queryParams={queryParams} />}
{docType === 'category' && <CategoryPage query={query} queryParams={queryParams} />} {docType === 'category' && <CategoryPage query={query} queryParams={queryParams} />}

View File

@ -0,0 +1,22 @@
import DynamicContentManager from '@/components/layout/dynamic-content-manager/dynamic-content-manager';
import { clientFetch } from '@/lib/sanity/sanity.client';
import { notFound } from 'next/navigation';
interface HomePageParams {
query: string;
queryParams: {
locale: string;
};
}
export default async function HomePage({ query = '', queryParams }: HomePageParams) {
const homePage = await clientFetch(query, queryParams);
if (!homePage) return notFound();
return (
<div>
<DynamicContentManager content={homePage?.content} />;
</div>
);
}

View File

@ -1,26 +0,0 @@
const getParamsFromSlug = (slugArray: string[], locale: string) => {
let docType = ''
const [slugStart] = slugArray
// We now have to re-combine the slug array to match our slug in Sanity.
const queryParams = {
slug: `/${slugArray.join('/')}`,
locale: locale
}
if (slugStart === `produkt` || slugStart === `product`) {
docType = `product`
} else if (slugStart === `kategori` || slugStart === `category`) {
docType = `category`
} else {
docType = `page`
}
return {
docType,
queryParams,
}
}
export default getParamsFromSlug

View File

@ -1,17 +1,29 @@
import { import {
categoryQuery, categoryQuery,
homePageQuery,
pageQuery, pageQuery,
productQuery productQuery
} from '@/lib/sanity/queries' } from '@/lib/sanity/queries';
import { groq } from 'next-sanity' import { groq } from 'next-sanity';
const getQueryFromSlug = (slugArray: string[], locale: string) => { const getQueryFromSlug = (slugArray: string[], locale: string) => {
const docQuery: { [index: string]: string } = { const docQuery: { [index: string]: string } = {
'home': groq`${homePageQuery}`,
'product': groq`${productQuery}`, 'product': groq`${productQuery}`,
'category': groq`${categoryQuery}`, 'category': groq`${categoryQuery}`,
'page': groq`${pageQuery}`, 'page': groq`${pageQuery}`,
} }
if (!slugArray) {
return {
docType: "home",
queryParams: {
locale: locale
},
query: docQuery.home,
}
}
let docType = '' let docType = ''
const [slugStart] = slugArray const [slugStart] = slugArray
@ -21,7 +33,6 @@ const getQueryFromSlug = (slugArray: string[], locale: string) => {
slug: `/${slugArray.join("/")}`, slug: `/${slugArray.join("/")}`,
locale: locale locale: locale
}; };
console.log(slugStart)
if (slugStart === `produkt` || slugStart === `product`) { if (slugStart === `produkt` || slugStart === `product`) {
docType = `product` docType = `product`

View File

@ -164,11 +164,9 @@ export const reusableSection = `
export const homePageQuery = `*[_type == "home" && language == $locale][0] { export const homePageQuery = `*[_type == "home" && language == $locale][0] {
_type, _type,
title, title,
"slug": slug.current,
"locale": language, "locale": language,
"translations": *[_type == "translation.metadata" && references(^._id)].translations[].value->{ "translations": *[_type == "translation.metadata" && references(^._id)].translations[].value->{
title, title,
"slug": slug.current,
"locale": language "locale": language
}, },
content[] { content[] {