Skip to content

Commit

Permalink
Add event schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinandsalis committed Jan 14, 2025
1 parent 6f6f76c commit 09a23ad
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 1 deletion.
200 changes: 200 additions & 0 deletions app/sanity/schema/event.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { Calendar1Icon } from 'lucide-react'
import { defineField, defineType, ObjectInputProps } from 'sanity'
import { z } from 'zod'

export const AttachmentSchema = z.object({
_type: z.literal('file'),
asset: z.object({
_type: z.literal('reference'),
_ref: z.string(),
}),
})

export const EventSchema = z.object({
title: z.string(),
location: z.string().nullable(),
description: z.string().nullable(),
start: z.union([
z.object({
date: z.string(),
dateTime: z.string().nullable(),
}),
z.object({
date: z.string().nullable(),
dateTime: z.string(),
}),
]),
end: z.union([
z.object({
date: z.string(),
dateTime: z.string().nullable(),
}),
z.object({
date: z.string().nullable(),
dateTime: z.string(),
}),
]),
type: z.union([
z.literal('general'),
z.literal('internal'),
z.literal('year'),
]),
attachments: z.array(AttachmentSchema),
})

export type Event = z.infer<typeof EventSchema>

function MyTimeInput(props: ObjectInputProps) {
return <input type="time" {...props.elementProps} />
}

export default defineType({
name: 'event',
title: 'Ereignis',
type: 'document',
icon: Calendar1Icon,
fields: [
defineField({
name: 'title',
type: 'string',
title: 'Titel',
validation: Rule => Rule.required(),
}),
defineField({
name: 'description',
type: 'array',
title: 'Beschreibung',
of: [{ type: 'block' }],
}),
defineField({
name: 'location',
type: 'string',
title: 'Ort',
}),
defineField({
name: 'type',
type: 'string',
title: 'Typ',
options: {
list: [
{ title: 'Allgemein', value: 'general' },
{ title: 'Intern', value: 'internal' },
{ title: 'Jahrgang', value: 'year' },
],
},
validation: Rule => Rule.required(),
}),
defineField({
name: 'year',
type: 'reference',
title: 'Jahrgang',
to: [{ type: 'year' }],
weak: true,
hidden: ({ document }) => document?.type !== 'year',
}),
defineField({
name: 'attachments',
type: 'array',
title: 'Anhänge',
of: [{ type: 'file' }],
options: {
layout: 'grid',
},
}),
// timezone is constant for all events and for start and end
defineField({
name: 'timeZone',
type: 'string',
title: 'Zeitzone',
hidden: true,
initialValue: 'Europe/Vienna',
}),
defineField({
name: 'start',
type: 'object',
title: 'Start',
validation: rule => rule.required(),
fields: [
defineField({
name: 'date',
type: 'date',
title: 'Datum',
validation: rule => rule.required(),
}),
defineField({
name: 'time',
type: 'string',
title: 'Uhrzeit',
components: {
// @ts-ignore
input: MyTimeInput,
},
}),
],
}),
defineField({
name: 'end',
type: 'object',
title: 'Ende',
validation: rule =>
rule.custom((end, context) => {
if (end?.date && end?.time) {
return true
}
if (!end?.date && end?.time) {
return 'Enddatum fehlt'
}
if (end?.date && context.document?.start) {
// @ts-ignore
const startDate = new Date(context.document.start.date)
// @ts-ignore
const endDate = new Date(end.date)
if (endDate < startDate) {
return 'Enddatum liegt vor dem Startdatum'
}
}
return true
}),
fields: [
defineField({
name: 'date',
type: 'date',
title: 'Datum',
}),
defineField({
name: 'time',
type: 'string',
title: 'Uhrzeit',
components: {
// @ts-ignore
input: MyTimeInput,
},
}),
],
}),
],
preview: {
select: {
title: 'title',
start: 'start',
end: 'end',
},
prepare({ title, start, end }) {
const startString = new Date(start?.date).toLocaleDateString('de-AT')
const endString = end?.date
? new Date(end.date).toLocaleDateString('de-AT')
: ''
return {
title: title,
subtitle: startString + (endString ? ' - ' + endString : ''),
}
},
},
orderings: [
{
title: 'Startdatum, Absteigend',
name: 'start',
by: [{ field: 'start.date', direction: 'desc' }],
},
],
})
2 changes: 2 additions & 0 deletions app/sanity/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import costs from './costs.ts'
import { curriculum, project } from './curriculum.ts'
import event from './event.tsx'
import homeHero from './home-hero.ts'
import person from './person.ts'
import post from './post.ts'
Expand All @@ -8,6 +9,7 @@ import testimonial from './testimonial.ts'
import year from './year.ts'

export const schemaTypes = [
event,
homeHero,
testimonial,
schoolYear,
Expand Down
3 changes: 2 additions & 1 deletion sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export default defineConfig({
.title('Content')
.items([
S.documentTypeListItem('post').title('Posts'),
S.documentTypeListItem('event').title('Ereignisse'),
S.divider(),
S.documentTypeListItem('schoolYear').title('Schuljahr'),
S.documentTypeListItem('costs').title('Kosten'),
S.documentTypeListItem('person').title('Personen'),
S.documentTypeListItem('year').title('Jahrgang'),
S.documentTypeListItem('schoolYear').title('Schuljahr'),
S.documentTypeListItem('testimonial').title('Erfahrungsberichte'),

S.divider(),
Expand Down

0 comments on commit 09a23ad

Please sign in to comment.