This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/cor 1335 automate sanity topical dates (#4659)
* feat: setup date config logic hook * feat: Add date logic hook to topical page * feat: optimise find date range script * test: update testing script * test: updated test file * feat: Add explanation to the math done. For reproducible code and understanding of whats going on. * fix: add explanation to the math done in the function * feat: added topical date config * feat: Added fields in sanity * feat: update sanity structure * feat: add toggle * feat: update typing and added document to sanity. * feat: applying pair programming feedback * feat: sanity tile date field * fix: solved issues on custom sanity component. * fix: also accept 0 as the day value. * fix: inconsistent rendering * fix: change iso week to just week * Fix: remove file * fix: merge conflict * fix: apply descriptions based on input by communication * fix: Update formatting * fix: Updating dutch * fix: PR feedback * fix: PR feedback * fix PR feedback, dutch grammar * fix: PR feedback fix apply typing. * Fix: PR feedback
- Loading branch information
1 parent
0d5ad07
commit 016efc3
Showing
11 changed files
with
237 additions
and
23 deletions.
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
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
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,48 @@ | ||
import React, { FormEvent, useState } from 'react'; | ||
import { withDocument } from 'part:@sanity/form-builder'; | ||
import { getTopicalTileDateConfig, ThemeTileDateConfig } from '../hooks/get-topical-tile-date-config'; | ||
import PatchEvent, { set } from '@sanity/form-builder/PatchEvent'; | ||
import { FormField, FormFieldProps } from '@sanity/base/components'; | ||
import { TextInput } from '@sanity/ui'; | ||
import { ValidationMarker } from '@sanity/types/dist/dts'; | ||
|
||
type ShowDateProps = { | ||
type: { title: 'Nederlands' | 'Engels' }; | ||
markers: ValidationMarker[]; | ||
presence: FormFieldProps['__unstable_presence']; | ||
onChange: (event: PatchEvent) => void; | ||
document: { themeTileDateConfig: ThemeTileDateConfig }; | ||
}; | ||
|
||
const ShowDate = (props: ShowDateProps) => { | ||
const { | ||
type, | ||
markers, | ||
presence, | ||
onChange, | ||
document: { themeTileDateConfig: dateConfig }, | ||
} = props; | ||
const isConfigValid = dateConfig?.startDayOfDate >= 0 && dateConfig?.weekOffset >= 0 && dateConfig?.timeSpanInDays >= 1; | ||
const formattedDate = !isConfigValid ? '' : getTopicalTileDateConfig({ config: dateConfig, inputDate: new Date(), language: type.title }); | ||
|
||
// The following line makes it possible to do realtime updates on this field with the new date configurations | ||
const initFormattedDateValue = set(formattedDate); | ||
onChange(PatchEvent.from(initFormattedDateValue)); | ||
const [value, setValue] = useState(''); | ||
|
||
const handleChange = (event: FormEvent<HTMLInputElement>) => { | ||
const inputValue = event.currentTarget.value; | ||
const newValue = inputValue === '' ? formattedDate : inputValue; | ||
const patchEventValue = set(newValue); | ||
setValue(newValue); | ||
onChange(PatchEvent.from(patchEventValue)); | ||
}; | ||
|
||
return ( | ||
<FormField description={`Het resultaat van de tegeldatum-configuratie is: ${formattedDate}`} title={type.title} __unstable_markers={markers} __unstable_presence={presence}> | ||
<TextInput onChange={handleChange} value={value === '' ? formattedDate : value} /> | ||
</FormField> | ||
); | ||
}; | ||
|
||
export const TopicalTileDate = withDocument(ShowDate); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { createFormatting } from '@corona-dashboard/common'; | ||
|
||
export interface ThemeTileDateConfig { | ||
weekOffset: number; | ||
startDayOfDate: 0 | 1 | 2 | 3 | 4 | 5 | 6; | ||
timeSpanInDays: number; | ||
} | ||
/** | ||
* Returns a object with the start and end date in seconds. Or a single unix date in seconds if it only contains one date. | ||
*/ | ||
|
||
export type TopicalDateConfig = { | ||
config: ThemeTileDateConfig; | ||
inputDate?: Date; | ||
language: 'Nederlands' | 'Engels'; | ||
}; | ||
|
||
const languages = { Nederlands: 'nl-NL', Engels: 'en-GB' }; | ||
|
||
const dayInMiliseconds = 86400000; | ||
const weekInMiliseconds = 604800000; | ||
|
||
export const getTopicalTileDateConfig = ({ config, inputDate = new Date(), language }: TopicalDateConfig): string => { | ||
const { formatDateFromMilliseconds, formatDateSpan } = createFormatting(languages[language], { | ||
date_day_before_yesterday: 'eergisteren', | ||
date_today: 'vandaag', | ||
date_yesterday: 'gisteren', | ||
}); | ||
|
||
/** | ||
* Get the current index of the day of the week for the given input date. | ||
* Starting with Sunday as index 0 and Saturday as index 6 | ||
* Sunday = 0 | ||
* Monday = 1 | ||
* Tuesday = 2 | ||
* Wednesday = 3 | ||
* Thursday = 4 | ||
* Friday = 5 | ||
* Saturday = 6 | ||
*/ | ||
const inputDateWeekDay = inputDate.getDay(); | ||
|
||
// Get the unix timestamp for the given input date. | ||
const inputDateInUnixTime = inputDate.getTime(); | ||
|
||
/** | ||
* Calulate how many milliseconds ago a IsoIndex was. | ||
* (This would not be the data of that week. | ||
* But the milliseconds passed since that week until the given input date.) | ||
* | ||
* Check if the weekday of given input date already passed the start day in the config. | ||
* If so, calculate how many milliseconds ago the | ||
* If not, add 1 week to the offset and calculate the amount of milliseconds for that offset | ||
* This is done because: if the offset is from last wedsnesday too the one before last wednesday. | ||
* We need to check if wedsneday already happened. Otherwise we need to add another week | ||
*/ | ||
const millisecondsPassedSinceWeekOffset = config.startDayOfDate > inputDateWeekDay ? (config.weekOffset + 1) * weekInMiliseconds : config.weekOffset * weekInMiliseconds; | ||
|
||
// Now we set the start of the week by having the weekday of the given input added to the week offset. | ||
// Basicly this always sets the week offset in milliseconds to the sunday of that week in the past. | ||
const startOfTheWeekInMilliSecondsPassed = millisecondsPassedSinceWeekOffset + inputDateWeekDay * dayInMiliseconds; | ||
|
||
// get the milliseconds offset from the given input date weekday to the previous sunday. | ||
const millisecondsPassedSinceSunday = config.startDayOfDate * dayInMiliseconds; | ||
|
||
// Get the length of the timespan in milliseconds | ||
const timespanLengthInMiliseconds = (config.timeSpanInDays - 1) * dayInMiliseconds; | ||
|
||
// convert the weeks past in seconds to the actual date in milliseconds | ||
const dateOfStartWeek = inputDateInUnixTime - startOfTheWeekInMilliSecondsPassed; | ||
|
||
// get form the sunday from the week to start to the day of the week that needs to be dsiplayed. | ||
const startDate = dateOfStartWeek + millisecondsPassedSinceSunday; | ||
|
||
// Get the end date by adding the millisecond of the timespan to the start date. | ||
const endDate = startDate + timespanLengthInMiliseconds; | ||
|
||
// Check if timespan is greater than one day. Or it's just a single day. create the return object. | ||
const dateResult = config.timeSpanInDays === 1 ? formatDateFromMilliseconds(startDate, 'medium') : formatDateSpan(new Date(startDate), new Date(endDate), 'medium').join(' - '); | ||
return dateResult; | ||
}; |
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
63 changes: 63 additions & 0 deletions
63
packages/cms/src/schemas/topical/theme-tile-date-config.ts
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,63 @@ | ||
import { Rule } from '~/sanity'; | ||
import { REQUIRED } from '../../validation'; | ||
|
||
const DAYS_OF_THE_WEEK_LIST = [ | ||
{ | ||
title: 'Zondag', | ||
value: 0, | ||
}, | ||
{ | ||
title: 'Maandag', | ||
value: 1, | ||
}, | ||
{ | ||
title: 'Dinsdag', | ||
value: 2, | ||
}, | ||
{ | ||
title: 'Woensdag', | ||
value: 3, | ||
}, | ||
{ | ||
title: 'Donderdag', | ||
value: 4, | ||
}, | ||
{ | ||
title: 'Vrijdag', | ||
value: 5, | ||
}, | ||
{ | ||
title: 'Zaterdag', | ||
value: 6, | ||
}, | ||
]; | ||
|
||
export const themeTileDateConfig = { | ||
name: 'themeTileDateConfig', | ||
type: 'document', | ||
fields: [ | ||
{ | ||
title: 'Hoeveel weken geleden was de startdatum?', | ||
name: 'weekOffset', | ||
type: 'number', | ||
validation: (rule: Rule) => rule.required().min(0), | ||
}, | ||
{ | ||
title: 'Op welke dag van de week start de datum?', | ||
name: 'startDayOfDate', | ||
options: { | ||
list: DAYS_OF_THE_WEEK_LIST, | ||
layout: 'dropdown', | ||
}, | ||
type: 'number', | ||
validation: REQUIRED, | ||
}, | ||
{ | ||
title: 'Hoeveelheid dagen', | ||
description: '(1 dag of bereik van – tot)', | ||
name: 'timeSpanInDays', | ||
type: 'number', | ||
validation: (rule: Rule) => rule.required().min(1), | ||
}, | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { Rule } from '~/sanity'; | ||
|
||
export const REQUIRED_MIN_MAX = (rule: Rule, min: number, max: number) => rule.required().min(min).max(max); | ||
export const REQUIRED = (rule: Rule) => rule.required(); |
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