Gwk-Cultural-Park/schemas/post.ts
2024-09-07 08:40:25 +07:00

103 lines
2.2 KiB
TypeScript

import { format, parseISO } from 'date-fns'
import { TiEdit } from 'react-icons/ti'
import { defineField, defineType } from 'sanity'
import authorType from './author'
import category from './category'
//import { categoryQuery } from 'lib/sanity.queries'
export default defineType({
name: 'post',
title: 'Post',
icon: TiEdit,
type: 'document',
fields: [
defineField({
name: 'title',
title: 'Title',
type: 'string',
validation: (rule) => rule.required(),
}),
defineField({
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
maxLength: 96,
isUnique: (value, context) => context.defaultIsUnique(value, context),
},
//validation: (rule) => rule.required(),
}),
defineField({
name: 'content',
title: 'Content',
type: 'array',
of: [
{
type: 'block',
},
{
type: 'image',
name: 'Photo',
options: {
hotspot: true,
},
// fields: {
// name: 'caption',
// type: 'string',
// title: 'Caption',
// },
},
],
}),
defineField({
name: 'excerpt',
title: 'Excerpt',
type: 'text',
}),
defineField({
name: 'coverImage',
title: 'Cover Image',
type: 'image',
options: {
hotspot: true,
},
}),
defineField({
name: 'date',
title: 'Date',
type: 'datetime',
initialValue: () => new Date().toISOString(),
}),
defineField({
name: 'author',
title: 'Author',
type: 'reference',
to: [{ type: authorType.name }],
}),
defineField({
name: 'category',
type: 'reference',
to: [{ type: category.name }],
options: { filter: 'defined(parent)' },
}),
],
preview: {
select: {
title: 'title',
author: 'author.name',
date: 'date',
media: 'coverImage',
},
prepare({ title, media, author, date }) {
const subtitles = [
author && `by ${author}`,
date && `on ${format(parseISO(date), 'LLL d, yyyy')}`,
].filter(Boolean)
return { title, media, subtitle: subtitles.join(' ') }
},
},
})