mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Added search pages to sanity and storefront
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import {TagIcon} from '@sanity/icons'
|
||||
import {defineField, defineType} from 'sanity'
|
||||
import {slugWithLocalizedType} from './slugWithLocalizedType'
|
||||
import {languages} from '../../languages'
|
||||
import {validateImage} from '../../utils/validation'
|
||||
import { TagIcon } from '@sanity/icons';
|
||||
import { defineField, defineType } from 'sanity';
|
||||
import { languages } from '../../languages';
|
||||
import { validateImage } from '../../utils/validation';
|
||||
import { slugWithLocalizedType } from '../slugWithLocalizedType';
|
||||
|
||||
const GROUPS = [
|
||||
{
|
||||
name: 'editorial',
|
||||
title: 'Editorial',
|
||||
title: 'Editorial'
|
||||
},
|
||||
{
|
||||
name: 'seo',
|
||||
title: 'SEO',
|
||||
},
|
||||
]
|
||||
title: 'SEO'
|
||||
}
|
||||
];
|
||||
|
||||
export default defineType({
|
||||
name: 'category',
|
||||
@@ -27,7 +27,7 @@ export default defineType({
|
||||
name: 'language',
|
||||
type: 'string',
|
||||
readOnly: true,
|
||||
description: 'Language of this document.',
|
||||
description: 'Language of this document.'
|
||||
// hidden: true,
|
||||
}),
|
||||
// Title
|
||||
@@ -35,7 +35,7 @@ export default defineType({
|
||||
name: 'title',
|
||||
title: 'Title',
|
||||
type: 'string',
|
||||
description: 'Category title.',
|
||||
description: 'Category title.'
|
||||
}),
|
||||
// Slug
|
||||
slugWithLocalizedType('category', 'title'),
|
||||
@@ -60,67 +60,67 @@ export default defineType({
|
||||
name: 'image',
|
||||
type: 'mainImage',
|
||||
title: 'Image',
|
||||
validation: (Rule) => validateImage(Rule, true),
|
||||
validation: (Rule) => validateImage(Rule, true)
|
||||
}),
|
||||
defineField({
|
||||
name: 'description',
|
||||
title: 'Description',
|
||||
type: 'text',
|
||||
rows: 5,
|
||||
description: 'Description of this category.',
|
||||
description: 'Description of this category.'
|
||||
}),
|
||||
defineField({
|
||||
name: 'id',
|
||||
title: 'ID',
|
||||
type: 'number',
|
||||
description: 'Unique ID.',
|
||||
description: 'Unique ID.'
|
||||
}),
|
||||
defineField({
|
||||
name: 'categoryId',
|
||||
title: 'Category ID',
|
||||
type: 'number',
|
||||
description: 'Unique category ID.',
|
||||
description: 'Unique category ID.'
|
||||
}),
|
||||
defineField({
|
||||
name: 'parentId',
|
||||
title: 'Parent ID',
|
||||
type: 'number',
|
||||
description: 'Unique parent category ID.',
|
||||
description: 'Unique parent category ID.'
|
||||
}),
|
||||
// SEO
|
||||
defineField({
|
||||
name: 'seo',
|
||||
title: 'SEO',
|
||||
type: 'seo',
|
||||
group: 'seo',
|
||||
}),
|
||||
group: 'seo'
|
||||
})
|
||||
],
|
||||
orderings: [
|
||||
{
|
||||
name: 'titleAsc',
|
||||
title: 'Title (A-Z)',
|
||||
by: [{field: 'title', direction: 'asc'}],
|
||||
by: [{ field: 'title', direction: 'asc' }]
|
||||
},
|
||||
{
|
||||
name: 'titleDesc',
|
||||
title: 'Title (Z-A)',
|
||||
by: [{field: 'title', direction: 'desc'}],
|
||||
},
|
||||
by: [{ field: 'title', direction: 'desc' }]
|
||||
}
|
||||
],
|
||||
preview: {
|
||||
select: {
|
||||
title: 'title',
|
||||
language: 'language',
|
||||
language: 'language'
|
||||
},
|
||||
prepare(selection) {
|
||||
const {title, language} = selection
|
||||
const currentLang = languages.find((lang) => lang.id === language)
|
||||
const { title, language } = selection;
|
||||
const currentLang = languages.find((lang) => lang.id === language);
|
||||
|
||||
return {
|
||||
title,
|
||||
subtitle: `${currentLang ? currentLang.title : ''}`,
|
||||
media: TagIcon,
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
media: TagIcon
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import {DocumentIcon} from '@sanity/icons'
|
||||
import {defineField} from 'sanity'
|
||||
import {languages} from '../../languages'
|
||||
import {COMPONENT_REFERENCES} from '../../constants'
|
||||
import {slugWithLocalizedType} from './slugWithLocalizedType'
|
||||
import { DocumentIcon } from '@sanity/icons';
|
||||
import { defineField } from 'sanity';
|
||||
import { COMPONENT_REFERENCES } from '../../constants';
|
||||
import { languages } from '../../languages';
|
||||
import { slugWithLocalizedType } from '../slugWithLocalizedType';
|
||||
|
||||
export default defineField({
|
||||
name: 'page',
|
||||
@@ -12,19 +12,19 @@ export default defineField({
|
||||
groups: [
|
||||
{
|
||||
name: 'editorial',
|
||||
title: 'Editorial',
|
||||
title: 'Editorial'
|
||||
},
|
||||
{
|
||||
name: 'seo',
|
||||
title: 'SEO',
|
||||
},
|
||||
title: 'SEO'
|
||||
}
|
||||
],
|
||||
fields: [
|
||||
defineField({
|
||||
name: 'language',
|
||||
type: 'string',
|
||||
readOnly: true,
|
||||
description: 'Language of this document.',
|
||||
description: 'Language of this document.'
|
||||
// hidden: true,
|
||||
}),
|
||||
// Title
|
||||
@@ -33,7 +33,7 @@ export default defineField({
|
||||
title: 'Title',
|
||||
type: 'string',
|
||||
description: 'Page title.',
|
||||
validation: (Rule) => Rule.required(),
|
||||
validation: (Rule) => Rule.required()
|
||||
}),
|
||||
// Slug
|
||||
slugWithLocalizedType('page', 'title'),
|
||||
@@ -44,32 +44,32 @@ export default defineField({
|
||||
type: 'array',
|
||||
group: 'editorial',
|
||||
description: 'Add, reorder, edit or delete page sections.',
|
||||
of: COMPONENT_REFERENCES,
|
||||
of: COMPONENT_REFERENCES
|
||||
}),
|
||||
// SEO
|
||||
defineField({
|
||||
name: 'seo',
|
||||
title: 'SEO',
|
||||
type: 'seo',
|
||||
group: 'seo',
|
||||
}),
|
||||
group: 'seo'
|
||||
})
|
||||
],
|
||||
preview: {
|
||||
select: {
|
||||
seoImage: 'seo.image',
|
||||
title: 'title',
|
||||
language: 'language',
|
||||
language: 'language'
|
||||
},
|
||||
prepare(selection) {
|
||||
const {seoImage, title, language} = selection
|
||||
const { seoImage, title, language } = selection;
|
||||
|
||||
const currentLang = languages.find((lang) => lang.id === language)
|
||||
const currentLang = languages.find((lang) => lang.id === language);
|
||||
|
||||
return {
|
||||
media: seoImage,
|
||||
title,
|
||||
subtitle: `${currentLang ? currentLang.title : ''}`,
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
subtitle: `${currentLang ? currentLang.title : ''}`
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import { PackageIcon } from '@sanity/icons';
|
||||
import { defineField, defineType } from 'sanity';
|
||||
import { slugWithLocalizedType } from './slugWithLocalizedType';
|
||||
import { languages } from '../../languages';
|
||||
import { validateImage } from '../../utils/validation';
|
||||
import { slugWithLocalizedType } from '../slugWithLocalizedType';
|
||||
|
||||
const GROUPS = [
|
||||
{
|
||||
|
@@ -1,56 +0,0 @@
|
||||
import {Rule, Slug} from 'sanity'
|
||||
import slugify from "slugify";
|
||||
import { i18n } from "../../languages";
|
||||
import { localizedTypes } from "../../localizedTypes";
|
||||
|
||||
const MAX_LENGTH = 96
|
||||
|
||||
function formatSlug(input: string, docType: string, context: any, schemaType: object | any) {
|
||||
const locale = schemaType?.parent?.language ? schemaType?.parent?.language : i18n.base;
|
||||
|
||||
let currentDocType: any;
|
||||
|
||||
currentDocType = localizedTypes.find(item => item.type === docType);
|
||||
const currentDocTypeLocalized = currentDocType[locale];
|
||||
|
||||
const slugStart = currentDocTypeLocalized ? `/${currentDocTypeLocalized}/` : `/`;
|
||||
const slug = slugify(input, { lower: true });
|
||||
|
||||
return slugStart + slug;
|
||||
}
|
||||
|
||||
export function slugWithLocalizedType(documentType = '', source = `title`) {
|
||||
const docType = documentType;
|
||||
|
||||
return {
|
||||
name: `slug`,
|
||||
type: `slug`,
|
||||
options: {
|
||||
source,
|
||||
slugify: (value: any, context: any, schemaType: object | any) => formatSlug(value, docType, context, schemaType),
|
||||
},
|
||||
validation: (Rule: Rule) => {
|
||||
return Rule.required().custom(async (value: Slug) => {
|
||||
|
||||
const currentSlug = value && value.current
|
||||
|
||||
if (!currentSlug) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (currentSlug.length >= MAX_LENGTH) {
|
||||
return `Must be less than ${MAX_LENGTH} characters`
|
||||
}
|
||||
|
||||
if (currentSlug.length === 0) {
|
||||
return 'Slug cannot be empty'
|
||||
}
|
||||
|
||||
if (currentSlug.endsWith("/")) {
|
||||
return 'Slug cannot end with "/"'
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user