Skip to content

Commit

Permalink
add Service_Page schema and front-end code
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Jan 6, 2025
1 parent 6d303d8 commit e8ac49a
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 8 deletions.
3 changes: 3 additions & 0 deletions apps/astro/src/components/global/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ const { feauturedProjects } = Astro.props as { feauturedProjects: feauturedProje
}
</ul>
</li>
<li>
<a href="/oferta">Oferta</a>
</li>
<li>
<a href="/inwestycje">Inwestycje</a>
</li>
Expand Down
4 changes: 4 additions & 0 deletions apps/astro/src/components/global/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const { feauturedProjects } = Astro.props as { feauturedProjects: feauturedProje
<div class="max-width">
<nav data-lenis-prevent>
<ul>
<li>
<a href="/oferta">Oferta</a>
</li>
<li class="dropdown-item">
<span>Realizacje</span>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" class="dropdown-icon">
Expand Down Expand Up @@ -238,6 +241,7 @@ const { feauturedProjects } = Astro.props as { feauturedProjects: feauturedProje
align-items: center;
height: unset;
&.dropdown-item {
order: -1;
& > span {
font-size: calc(14rem / 16);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/astro/src/components/global/ServicesOverview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const { index, heading, paragraph, items, ctaDescription, cta } = Astro.props

<section class="ServicesOverview">
<header>
<PortableText value={heading} heading={index === 0 ? 'h1' : 'h2'} class="heading" />
<PortableText value={heading} heading={index === 0 ? 'h1' : 'h2'} class="heading h2" />
<PortableText value={paragraph} />
</header>
<div>
Expand Down
23 changes: 19 additions & 4 deletions apps/astro/src/components/ui/PortableText/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
export { default, type PortableTextValue } from './index.astro';

/**
* GROQ query helper for Portable Text fields that ensures proper handling of text formatting
* and link references. The coalesce() wrapper is crucial here because:
*
* 1. When duplicating Sanity documents with formatted text (bold, italic etc.),
* the markDefs array sometimes doesn't get properly initialized
* 2. This causes Astro's Portable Text renderer to throw "Cannot read properties
* of null (reading 'find')" error when processing marks like 'strong'
* 3. By using coalesce(), we ensure markDefs is always an array (falling back
* to empty [] if null), preventing the runtime error
*
* @param name - The field name in your Sanity schema to query
* @returns A GROQ query string that safely handles Portable Text fields
*/
export const PortableTextQuery = (name: string) => `
${name}[] {
...,
markDefs[] {
"markDefs": coalesce(markDefs[] {
...,
_type == "link" => {
_type,
_key,
type,
"href": select(type == "internal" => internal -> slug.current, type == "external" => external, "#"),
linkType,
"href": select(linkType == "internal" => internal -> slug.current, linkType == "external" => external, "#"),
},
},
}, []),
},
`
24 changes: 24 additions & 0 deletions apps/astro/src/pages/oferta.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
import Layout from '@layouts/Layout.astro'
import metadataFetch from '@utils/metadata.fetch'
import sanityFetch from '@utils/sanity.fetch'
import Breadcrumbs from '@components/ui/Breadcrumbs.astro'
import Components, { Components_Query, type ComponentsProps } from '@components/Components.astro'
const metadata = await metadataFetch('/oferta')
const data = await sanityFetch<{ name: string; slug: string; components: ComponentsProps }>({
query: `
*[_type == "Service_Page"][0] {
"name": title,
"slug": slug.current,
${Components_Query}
}
`,
})
---

<Layout {...metadata}>
<Breadcrumbs data={[{ name: data.name, path: data.slug }]} />
<Components data={data.components} />
</Layout>
6 changes: 3 additions & 3 deletions apps/sanity/schema/components/SimpleCtaSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default defineField({
],
preview: {
select: {
name: 'name',
subtitle: 'name',
},
prepare: ({ name }) => ({
prepare: ({ subtitle }) => ({
title: title,
subtitle: toPlainText(name),
subtitle: toPlainText(subtitle),
...sectionPreview({ name, icon: icon() }),
}),
},
Expand Down
41 changes: 41 additions & 0 deletions apps/sanity/schema/singleTypes/Service_Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineField, defineType } from "sanity"
import { defineSlugForDocument } from "../../utils/define-slug-for-document";

const name = 'Service_Page';
const title = 'Oferta';
const slug = '/oferta';
const icon = () => '💼';

export default defineType({
name: name,
type: 'document',
title: title,
icon,
options: { documentPreview: true },
fields: [
...defineSlugForDocument({ slug: slug }),
defineField({
name: 'components',
type: 'components',
title: 'Page Components',
}),
defineField({
name: 'seo',
type: 'seo',
title: 'SEO',
group: 'seo',
}),
],
groups: [
{
name: 'seo',
title: 'SEO',
},
],
preview: {
prepare: () => ({
title: title,
subtitle: slug
})
}
});
1 change: 1 addition & 0 deletions apps/sanity/structure/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const structure: StructureResolver = (S) =>
createSingleton(S, "Projects_Page"),
createSingleton(S, "Investments_Page"),
createSingleton(S, "About_Page"),
createSingleton(S, "Service_Page"),
createSingleton(S, "Contact_Page"),
S.divider(),
createSingleton(S, "PrivacyPolicy_Page"),
Expand Down
1 change: 1 addition & 0 deletions apps/sanity/structure/internal-linkable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const InternalLinkableTypes: { type: string }[] = [
{ type: 'Projects_Page' },
{ type: 'Investments_Page' },
{ type: 'About_Page' },
{ type: 'Service_Page' },
{ type: 'Contact_Page' },
{ type: 'PrivacyPolicy_Page' },
{ type: 'Project_Collection' },
Expand Down
2 changes: 2 additions & 0 deletions apps/sanity/structure/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Contact_Page from '../schema/singleTypes/Contact_Page';
import Projects_Page from '../schema/singleTypes/Projects_Page';
import Investments_Page from '../schema/singleTypes/Investments_Page';
import About_Page from '../schema/singleTypes/About_Page';
import Service_Page from '../schema/singleTypes/Service_Page';
import PrivacyPolicy_Page from '../schema/singleTypes/PrivacyPolicy_Page';
import NotFound_Page from '../schema/singleTypes/NotFound_Page';

Expand All @@ -16,6 +17,7 @@ const singleTypes = [
Projects_Page,
Investments_Page,
About_Page,
Service_Page,
Contact_Page,
PrivacyPolicy_Page,
NotFound_Page,
Expand Down

0 comments on commit e8ac49a

Please sign in to comment.