Skip to content

Commit

Permalink
add NotFound_Page schema
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Sep 21, 2024
1 parent 37b5a5e commit c9aa39f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
40 changes: 40 additions & 0 deletions apps/sanity/schema/singleTypes/NotFound_Page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defineField, defineType } from "sanity"
import { defineSlugForDocument } from "../../utils/define-slug-for-document";

const name = 'NotFound_Page';
const title = 'Strony nie znaleziono';
const slug = '/404';
const icon = () => '🔍';

export default defineType({
name: name,
type: 'document',
title: title,
icon,
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
})
}
});
30 changes: 30 additions & 0 deletions apps/sanity/structure/create-collection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { StructureBuilder } from "sanity/structure";
import { schemaTypes } from "./schema-types";
import { Preview } from "./preview";
import { TYPES_TO_EXCLUDE_PREVIEWS } from ".";

export const createCollection = (S: StructureBuilder, name: string) => {
const { title, icon } = schemaTypes.find(item => item.name === name) as { title: string, icon: React.ReactNode };
return S.listItem()
.id(name)
.title(title)
.icon(icon)
.child(
S.documentTypeList(name)
.title(title)
.child(documentId =>
S.document()
.documentId(documentId)
.schemaType(name)
.views([
S.view.form().title('Editor').icon(() => '🖋️'),
...(!TYPES_TO_EXCLUDE_PREVIEWS.includes(name) ? [
S.view
.component(Preview)
.title('Preview')
.icon(() => '👀')
] : []),
])
)
);
};
18 changes: 3 additions & 15 deletions apps/sanity/structure/create-singleton.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@

import type { StructureBuilder } from 'sanity/structure'
import { Iframe, type IframeProps } from 'sanity-plugin-iframe-pane'
import { schemaTypes } from "./schema-types";
import { PREVIEW_DEPLOYMENT_DOMAIN } from '../constants';

const Preview = ({ document }: { document: IframeProps['document'] }) => {
const slug = (document.displayed.slug as { current?: string })?.current;
if (!slug) return <div style={{ padding: '1rem' }}>🛑 Preview not available: The slug is missing</div>;
return <Iframe
document={document}
options={{
url: `${PREVIEW_DEPLOYMENT_DOMAIN}${slug}`,
reload: { button: true }
}} />
}

const TYPES_TO_EXCLUDE_PREVIEWS = ['global', 'redirects'];
import { Preview } from './preview';
import { TYPES_TO_EXCLUDE_PREVIEWS } from '.';

export const createSingleton = (S: StructureBuilder, name: string) => {
const { title, icon } = schemaTypes.find(item => item.name === name) as { title: string, icon: React.ReactNode };
Expand Down
7 changes: 6 additions & 1 deletion apps/sanity/structure/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

import type { StructureResolver } from 'sanity/structure'
import { createSingleton } from './create-singleton';
import { createCollection } from './create-collection';

export const TYPES_TO_EXCLUDE_PREVIEWS = ['global', 'redirects', 'Faq_Collection'];

export const structure: StructureResolver = (S) =>
S.list()
Expand All @@ -11,6 +15,7 @@ export const structure: StructureResolver = (S) =>
S.divider(),
createSingleton(S, "Index_Page"),
createSingleton(S, "Contact_Page"),
createSingleton(S, "NotFound_Page"),
S.divider(),
S.documentTypeListItem("Faq_Collection"),
createCollection(S, "Faq_Collection"),
])
13 changes: 13 additions & 0 deletions apps/sanity/structure/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Iframe, type IframeProps } from "sanity-plugin-iframe-pane";
import { PREVIEW_DEPLOYMENT_DOMAIN } from "../constants";

export const Preview = ({ document }: { document: IframeProps['document'] }) => {
const slug = (document.displayed.slug as { current?: string })?.current;
if (!slug) return <div style={{ padding: '1rem' }}>🛑 Preview not available: The slug is missing</div>;
return <Iframe
document={document}
options={{
url: `${PREVIEW_DEPLOYMENT_DOMAIN}${slug}`,
reload: { button: true }
}} />
}
2 changes: 2 additions & 0 deletions apps/sanity/structure/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import global from '../schema/singleTypes/global';
import redirects from '../schema/singleTypes/redirects';
import Index_Page from '../schema/singleTypes/Index_Page';
import Contact_Page from '../schema/singleTypes/Contact_Page';
import NotFound_Page from '../schema/singleTypes/NotFound_Page';

const singleTypes = [
global,
redirects,
Index_Page,
Contact_Page,
NotFound_Page,
];

// Collections Types
Expand Down

0 comments on commit c9aa39f

Please sign in to comment.