Skip to content

Instantly share code, notes, and snippets.

@wildseansy
Last active May 10, 2021 17:38
Show Gist options
  • Save wildseansy/bed2471c6f50db5b29ee2d83acf25a7f to your computer and use it in GitHub Desktop.
Save wildseansy/bed2471c6f50db5b29ee2d83acf25a7f to your computer and use it in GitHub Desktop.
import computedField from 'sanity-plugin-computed-field'
import createSchema from 'part:@sanity/base/schema-creator'
import schemaTypes from 'all:part:@sanity/base/schema-type'
const Course = {
name: 'course',
title: 'Title for Course',
description: 'Represents a given course taught at Sanity U',
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
description: 'Please give this course a title',
type: 'string',
},
{
title: 'Is Active',
name: 'courseActive',
description: 'Whether this course has scheduled items remaining this quarter.',
type: 'boolean',
inputComponent: computedField,
options: {
documentQuerySelection: `
"numScheduled": count(*[_type == "scheduledCourse" && references(^._id) && startTime > ${new Date().toISOString()}])
`,
reduceQueryResult: (queryResult: {numScheduled: number}) => {
return queryResult.numScheduled > 0
},
},
},
{
title: 'Description',
description: 'Generated by what instructors are teaching this class on the schedule.',
type: 'text',
name: 'description',
inputComponent: computedField,
options: {
editable: true,
documentQuerySelection: `
title,
"courses": *[_type == "scheduledCourse" && references(^._id)] {
instructor {
name
}
}`,
reduceQueryResult: (queryResult: {
title: string
courses: {instructor: {name: string}}[]
}) => {
// Sets a preview of the instructors that are currently scheduled to teach this course
if (queryResult.courses && queryResult.courses.length > 0) {
const instructorSet = queryResult.courses.reduce((prev, current) => {
prev.add(current.instructor.name)
return prev
}, new Set<string>())
return `${queryResult.title} is taught by ${Array.from(instructorSet.values()).join(
', '
)}`
}
return 'No instructors scheduled'
},
},
},
{
title: 'Days of the week occuring',
name: 'daysOfWeek',
description:
"Given the classes occurring after today, computes what day of the week they're on. Click 'Regenerate' to update",
type: 'string',
inputComponent: computedField,
options: {
documentQuerySelection: `
"itemsScheduled": *[_type == "scheduledCourse" && references(^._id) && startTime > ${new Date().toISOString()}] {
startTime
}`,
// Sets a preview of the days of the week that this course takes place
reduceQueryResult: (queryResult: {itemsScheduled?: {startTime: string}[]}) => {
const dateFormat = new Intl.DateTimeFormat('en-US', {
weekday: 'long',
})
if (!queryResult.itemsScheduled || queryResult.itemsScheduled.length > 0) {
return 'No Classes Scheduled'
}
const daySet = queryResult.itemsScheduled.reduce((prev, current) => {
prev.add(dateFormat.format(new Date(current.startTime)))
return prev
}, new Set<string>())
// Returns the days of the week, separated by a comma. "Sunday, Monday, Thursday" for example
return Array.from(daySet.values()).join(', ')
},
},
},
{
title: 'Number of classes',
name: 'numClasses',
description: 'Computed by number of classes on the schedule',
type: 'string',
inputComponent: computedField,
options: {
documentQuerySelection: `
"numScheduled": count(*[_type == "scheduledCourse" && references(^._id)])`,
// Sets the number of scheduled classes available for this course
reduceQueryResult: (queryResult: {numScheduled: number}) => {
return queryResult.numScheduled
},
},
},
],
}
// Represents a course that is scheduled for a time.
const ScheduledCourse = {
name: 'scheduledCourse',
title: 'Scheduled Course',
type: 'document',
fields: [
{
name: 'course',
type: 'reference',
to: [{type: 'course', title: 'Taught Course'}],
},
{
name: 'startTime',
description: 'Please choose the day and time this course is taught',
type: 'datetime',
},
{
name: 'instructor',
type: 'reference',
to: [{type: 'instructor', title: 'Instructor'}],
},
],
}
// Represents a course that is scheduled for a time.
const Instructor = {
name: 'instructor',
title: 'Instructor',
type: 'document',
fields: [
{
title: 'Name',
name: 'name',
description: 'Name of instructor',
type: 'string',
},
],
}
export const schema = createSchema({
// We name our schema
name: 'default',
// Then proceed to concatenate our document type
// to the ones provided by any plugins that are installed
types: schemaTypes.concat([
//Documents:
Course,
ScheduledCourse,
Instructor,
]),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment