Skip to content

Commit

Permalink
feat: add preview component (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebiggianthead authored Oct 25, 2024
1 parent 0f655a3 commit e34d859
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/components/RecurringDatesPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, format} from '@sanity/util/legacyDateFormat'
import {upperFirst} from 'lodash'
import React from 'react'
import {rrulestr} from 'rrule'
import type {ObjectSchemaType, PreviewProps} from 'sanity'

import type {PluginConfig, RecurringDate, WithRequiredProperty} from '../types'

type CastPreviewProps = PreviewProps &
RecurringDate & {
pluginConfig: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>
}

type RecurringDateObjectSchemaType = Omit<ObjectSchemaType, 'options'> & {
options?: PluginConfig
}

export function RecurringDatesPreview(props: CastPreviewProps): React.JSX.Element {
const {startDate, endDate, rrule, schemaType, pluginConfig} = props
const options: RecurringDateObjectSchemaType = schemaType?.options

const {dateTimeOptions, dateOnly} = {
...pluginConfig,
...options,
}

const rule = rrule && rrulestr(rrule)

const dateFormat = dateTimeOptions?.dateFormat || DEFAULT_DATE_FORMAT
const timeFormat = dateTimeOptions?.timeFormat || DEFAULT_TIME_FORMAT

const start = startDate ? new Date(startDate) : undefined
const end = endDate ? new Date(endDate) : undefined
const sameDay = start && end && start.toDateString() === end.toDateString()

let title = 'No start date'
if (dateOnly) {
title = start ? format(start, dateFormat) : 'No start date'
if (end && !sameDay) {
title += ` - ${format(end, dateFormat)}`
}
} else {
title = start ? format(start, `${dateFormat} ${timeFormat}`) : 'No start date'
if (end) {
title += ` - ${format(end, sameDay ? timeFormat : `${dateFormat} ${timeFormat}`)}`
}
}

const previewProps = {
title,
subtitle: rule && upperFirst(rule.toText()),
}

return props.renderDefault({...previewProps, ...props})
}
11 changes: 11 additions & 0 deletions src/schema/recurringDates.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {CalendarIcon} from '@sanity/icons'
import {defineField, SchemaTypeDefinition} from 'sanity'

import {RecurringDates} from '../components/RecurringDate'
import {RecurringDatesPreview} from '../components/RecurringDatesPreview'
import {PluginConfig, WithRequiredProperty} from '../types'

export default (
Expand All @@ -12,6 +14,7 @@ export default (
name: 'recurringDates',
title: 'Dates',
type: 'object',
icon: CalendarIcon,
fields: [
defineField({
title: 'Start Date',
Expand Down Expand Up @@ -43,6 +46,14 @@ export default (
],
components: {
input: (props) => RecurringDates({...props, pluginConfig: config}),
preview: (props) => RecurringDatesPreview({...props, pluginConfig: config}),
},
preview: {
select: {
startDate: 'startDate',
endDate: 'endDate',
rrule: 'rrule',
},
},
})
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare module 'sanity' {
}

export interface RecurringDate {
rrule: string
startDate: string
endDate: string
rrule?: string
startDate?: string
endDate?: string
}

0 comments on commit e34d859

Please sign in to comment.