Skip to content

Commit

Permalink
πŸ› Check for future date on published news #2524 (#2525)
Browse files Browse the repository at this point in the history
* πŸ› Check for future date on published news #2524

* 🎨 Make date field required and date must be in the past #2524

* Adjustments to time logic #2524

* ♻️ Refactor for better readability and comments #2524

* πŸ›  Adding missing fields and custom publication logic #2524
  • Loading branch information
millianapia authored Oct 9, 2024
1 parent 82d0bc7 commit d6744f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
13 changes: 10 additions & 3 deletions sanityv3/schemas/documents/localNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,21 @@ export default {
media: 'heroImage.image',
description: 'ingress',
publishedDate: 'publishDateTime',
firstPublishedAt: 'firstPublishedAt',
isCustomDate: 'customPublicationDate',
},
prepare(selection: any) {
const { title, media, description, publishedDate } = selection
const date = publishedDate ? formatDate(publishedDate) : 'Ikke oppgitt'
const { title, media, description, publishedDate, firstPublishedAt, isCustomDate } = selection
const currentDate = new Date()
const date =
publishedDate && isCustomDate ? new Date(publishedDate) : firstPublishedAt ? new Date(firstPublishedAt) : null

const displayDate = date && date <= currentDate ? formatDate(date) : 'Not Published'

const ingressBlock = (description || []).find((ingressBlock: any) => ingressBlock._type === 'block')
return {
title,
subtitle: `Published date: ${date}`,
subtitle: `Published date: ${displayDate}`,
description: ingressBlock
? ingressBlock.children
.filter((child: any) => child._type === 'span')
Expand Down
12 changes: 6 additions & 6 deletions sanityv3/schemas/documents/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ export default {
},
prepare(selection) {
const { title, media, description, publishedDate, firstPublishedAt, isCustomDate } = selection
const currentDate = new Date()
const date =
publishedDate && isCustomDate
? formatDate(publishedDate)
: firstPublishedAt
? formatDate(firstPublishedAt)
: 'Not Published'
publishedDate && isCustomDate ? new Date(publishedDate) : firstPublishedAt ? new Date(firstPublishedAt) : null

const displayDate = date && date <= currentDate ? formatDate(date) : 'Not Published'

const ingressBlock = (description || []).find((ingressBlock) => ingressBlock._type === 'block')
return {
title,
subtitle: `Published date: ${date}`,
subtitle: `Published date: ${displayDate}`,
description: ingressBlock
? ingressBlock.children
.filter((child) => child._type === 'span')
Expand Down
27 changes: 25 additions & 2 deletions sanityv3/schemas/documents/news/sharedNewsFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,34 @@ export const publishDateTime = [
validation: (Rule: Rule) =>
Rule.custom((value: PublishDateTimeType, context: ValidationContext) => {
const { parent } = context as { parent: PublishDateTimeType }
if (!parent.customPublicationDate || value) {
// If customPublicationDate is false, skip validation
if (!parent.customPublicationDate) {
return true
} else {
}

// If customPublicationDate is true and no value is provided, return an error
if (!value) {
return 'Field is required'
}

// Parse the selected publish date and today's date
const publishedDate = new Date(value.toString())
const today = new Date()

// Set time to 00:00:00 for both dates, so only the date part is compared
const publishedDateNoTime = new Date(
publishedDate.getFullYear(),
publishedDate.getMonth(),
publishedDate.getDate(),
)
const todayNoTime = new Date(today.getFullYear(), today.getMonth(), today.getDate())

// Allow publication if the selected date is today or in the past
if (publishedDateNoTime <= todayNoTime) {
return true // Valid date
} else {
return 'The date can’t be in the future'
}
}),
},
{
Expand Down

0 comments on commit d6744f3

Please sign in to comment.