-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f6f76c
commit 81d573e
Showing
3 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
import { Calendar1Icon } from 'lucide-react' | ||
import { defineField, defineType, type 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('talk'), | ||
z.literal('holiday'), | ||
z.literal('theater'), | ||
z.literal('exam'), | ||
z.literal('project'), | ||
]), | ||
attachments: z.array(AttachmentSchema), | ||
}) | ||
|
||
export type Event = z.infer<typeof EventSchema> | ||
|
||
function tType(type: Event['type']) { | ||
switch (type) { | ||
case 'general': | ||
return 'Allgemein' | ||
case 'talk': | ||
return 'Vortrag' | ||
case 'holiday': | ||
return 'Ferien' | ||
case 'theater': | ||
return 'Theater' | ||
case 'exam': | ||
return 'Prüfung' | ||
case 'project': | ||
return 'Projekt' | ||
default: | ||
return type | ||
} | ||
} | ||
|
||
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: { | ||
layout: 'dropdown', | ||
list: [ | ||
{ title: 'Allgemein', value: 'general' }, | ||
{ title: 'Vortrag', value: 'talk' }, | ||
{ title: 'Ferien', value: 'holiday' }, | ||
{ title: 'Theater', value: 'theater' }, | ||
{ title: 'Prüfung', value: 'exam' }, | ||
{ title: 'Projekt', value: 'project' }, | ||
], | ||
}, | ||
}), | ||
defineField({ | ||
name: 'year', | ||
type: 'reference', | ||
title: 'Jahrgang', | ||
to: [{ type: 'year' }], | ||
weak: true, | ||
}), | ||
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', | ||
type: 'type', | ||
}, | ||
prepare({ title, start, end, type }) { | ||
const startString = new Date(start?.date).toLocaleDateString('de-AT') | ||
const endString = end?.date | ||
? new Date(end.date).toLocaleDateString('de-AT') | ||
: null | ||
return { | ||
title: `${type ? `${tType(type)}: ` : ''}${title}`, | ||
subtitle: endString ? `${startString} – ${endString}` : startString, | ||
} | ||
}, | ||
}, | ||
orderings: [ | ||
{ | ||
title: 'Startdatum, Absteigend', | ||
name: 'start', | ||
by: [{ field: 'start.date', direction: 'desc' }], | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters