Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Feature/cor 1335 automate sanity topical dates #4659

Merged
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e4adfff
feat: setup date config logic hook
Jorrik-Klijnsma-Work Feb 6, 2023
c5f0ea1
feat: Add date logic hook to topical page
Jorrik-Klijnsma-Work Feb 6, 2023
60141ee
feat: optimise find date range script
Jorrik-Klijnsma-Work Feb 6, 2023
ae90ed8
test: update testing script
Jorrik-Klijnsma-Work Feb 6, 2023
63e4084
test: updated test file
Jorrik-Klijnsma-Work Feb 7, 2023
73908aa
feat: Add explanation to the math done. For reproducible code and und…
Jorrik-Klijnsma-Work Feb 7, 2023
534d662
fix: add explanation to the math done in the function
Jorrik-Klijnsma-Work Feb 7, 2023
7c65ba1
feat: added topical date config
Jorrik-Klijnsma-Work Feb 8, 2023
624af8a
feat: Added fields in sanity
Jorrik-Klijnsma-Work Feb 9, 2023
fb7e762
feat: update sanity structure
Jorrik-Klijnsma-Work Feb 9, 2023
779009e
feat: add toggle
Jorrik-Klijnsma-Work Feb 9, 2023
d7edf28
feat: update typing and added document to sanity.
Jorrik-Klijnsma-Work Feb 9, 2023
7c63672
feat: applying pair programming feedback
Jorrik-Klijnsma-Work Feb 10, 2023
f058df8
feat: sanity tile date field
Jorrik-Klijnsma-Work Feb 12, 2023
df0868b
fix: solved issues on custom sanity component.
Jorrik-Klijnsma-Work Feb 13, 2023
a97375d
fix: also accept 0 as the day value.
Jorrik-Klijnsma-Work Feb 13, 2023
3571656
fix: inconsistent rendering
Jorrik-Klijnsma-Work Feb 13, 2023
249dc44
fix: change iso week to just week
Jorrik-Klijnsma-Work Feb 14, 2023
ad466e5
Fix: remove file
Jorrik-Klijnsma-Work Feb 14, 2023
4ddb306
fix: merge conflict
Jorrik-Klijnsma-Work Feb 14, 2023
12e9d02
fix: apply descriptions based on input by communication
Jorrik-Klijnsma-Work Feb 14, 2023
f30b983
fix: Update formatting
Jorrik-Klijnsma-Work Feb 14, 2023
ef85ac7
fix: Updating dutch
Jorrik-Klijnsma-Work Feb 14, 2023
738eb57
fix: PR feedback
Jorrik-Klijnsma-Work Feb 14, 2023
bc392e4
fix: PR feedback
Jorrik-Klijnsma-Work Feb 14, 2023
c57280c
fix PR feedback, dutch grammar
Jorrik-Klijnsma-Work Feb 14, 2023
3e018f3
fix: PR feedback fix apply typing.
Jorrik-Klijnsma-Work Feb 14, 2023
36194d8
Fix: PR feedback
Jorrik-Klijnsma-Work Feb 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const Home = (props: StaticProps<typeof getStaticProps>) => {
marginBottom={{ _: space[4], sm: space[5] }}
>
{theme.tiles.map((themeTile) => {
const sourceLabel = themeTile.sourceLabel ? replaceVariablesInText(themeTile.sourceLabel, { date: themeTile.tileDate }) : null;
return (
<TopicalTile
hideTrendIcon={themeTile.hideTrendIcon}
Expand All @@ -201,7 +202,7 @@ const Home = (props: StaticProps<typeof getStaticProps>) => {
cta={themeTile.cta}
key={themeTile.title}
kpiValue={themeTile.kpiValue}
sourceLabel={themeTile.sourceLabel}
sourceLabel={sourceLabel}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/queries/get-topical-structure-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function getTopicalStructureQuery(locale: string) {
tileIcon,
'title':title.${locale},
'sourceLabel':sourceLabel.${locale},
'tileDate': tileDate.${locale},
'kpiValue': kpiValue.${locale},
'hideTrendIcon': hideTrendIcon,
'trendIcon': {
Expand Down
7 changes: 4 additions & 3 deletions packages/app/src/queries/query-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ export interface BaseTile {
}

interface TopicalTile extends BaseTile {
title: string;
sourceLabel: string | null;
tileDate: string;
kpiValue: string | null;
cta: Cta;
hideTrendIcon: boolean;
kpiValue: string | null;
sourceLabel: string | null;
title: string;
trendIcon: TrendIcon;
}

Expand Down
39 changes: 39 additions & 0 deletions packages/cms/src/components/topical-tile-date.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { FormEvent, useState } from 'react';
import { withDocument } from 'part:@sanity/form-builder';
import { getTopicalTileDateConfig } from '../hooks/get-topical-tile-date-config';
import PatchEvent, { set } from '@sanity/form-builder/PatchEvent';
import { FormField } from '@sanity/base/components';
import { TextInput } from '@sanity/ui';

const ShowDate = (props: any) => {
APW26 marked this conversation as resolved.
Show resolved Hide resolved
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);
APW26 marked this conversation as resolved.
Show resolved Hide resolved
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);
1 change: 1 addition & 0 deletions packages/cms/src/desk-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const hiddenDocTypes = [
'topicalPage',
'topicalPageConfig',
'trendIcon',
'themeTileDateConfig',
'veelgesteldeVragen',
'veelgesteldeVragenGroups',
'warning',
Expand Down
81 changes: 81 additions & 0 deletions packages/cms/src/hooks/get-topical-tile-date-config.ts
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;
};
1 change: 1 addition & 0 deletions packages/cms/src/schemas/topical/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './theme-collection';
export * from './theme-link';
export * from './theme-link-collection';
export * from './theme-tile';
export * from './theme-tile-date-config';
APW26 marked this conversation as resolved.
Show resolved Hide resolved
export * from './theme-tile-collection';
export * from './measure-theme';
export * from './measure-tile';
Expand Down
63 changes: 63 additions & 0 deletions packages/cms/src/schemas/topical/theme-tile-date-config.ts
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 dat van week start de datum?',
VWSCoronaDashboard26 marked this conversation as resolved.
Show resolved Hide resolved
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),
},
],
};
32 changes: 32 additions & 0 deletions packages/cms/src/schemas/topical/theme-tile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { KpiIconInput } from '../../components/portable-text/kpi-configuration/kpi-icon-input';
import { REQUIRED } from '../../validation';
import { TopicalTileDate } from '../../components/topical-tile-date';
import { supportedLanguages } from '../../language/supported-languages';

export const themeTile = {
type: 'document',
Expand All @@ -10,6 +12,15 @@ export const themeTile = {
title: 'KPI Waarde',
name: 'kpiValue',
},
{
title: 'Thema tegeldatum-configuratie',
name: 'theme-tile-date-config',
description: 'Klik op het label om de velden te tonen.',
options: {
collapsible: true,
collapsed: true,
},
},
],
fields: [
{
Expand Down Expand Up @@ -47,9 +58,30 @@ export const themeTile = {
},
{
title: 'Metadata label',
description: 'Bij {{DATE}} wordt de tekst geplaatst van het tegeldatumveld. Deze kan handmatig overschreven worden.',
name: 'sourceLabel',
type: 'localeString',
},
{
title: 'Tegeldatum',
description:
'Deze velden krijgen hun input van de tegeldatum-configuratie. Maar kunnen handmatig overschreven worden door een eigen tekst in te vullen. Om terug te gaan naar het gebruik van de configuratie kunnen deze velden leeg gemaakt worden.',
name: 'tileDate',
type: 'object',
fields: supportedLanguages.map((lang) => ({
title: lang.title,
name: lang.id,
type: 'string',
inputComponent: TopicalTileDate,
})),
},
{
title: 'Configuratie velden',
description: 'Voor de start- en einddatum van deze tegel op de samenvattingspagina.',
name: 'themeTileDateConfig',
type: 'themeTileDateConfig',
fieldset: 'theme-tile-date-config',
},
{
title: 'Trend icon',
name: 'trendIcon',
Expand Down
1 change: 0 additions & 1 deletion packages/cms/src/validation/index.ts
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);
APW26 marked this conversation as resolved.
Show resolved Hide resolved
export const REQUIRED = (rule: Rule) => rule.required();
22 changes: 4 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10719,24 +10719,10 @@ __metadata:
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.0, caniuse-lite@npm:^1.0.30000792, caniuse-lite@npm:^1.0.30000805, caniuse-lite@npm:^1.0.30000844, caniuse-lite@npm:^1.0.30000981, caniuse-lite@npm:^1.0.30001109, caniuse-lite@npm:^1.0.30001271, caniuse-lite@npm:^1.0.30001272":
version: 1.0.30001272
resolution: "caniuse-lite@npm:1.0.30001272"
checksum: 5a08fc35ae298acef6846111634201bde7b4710fe0ccabb69ad5681115f77971265d9b6b5cd9bb976bc44d8d6ff0119cb28d9032753e063a2c70e1e30b630ad9
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001274":
version: 1.0.30001279
resolution: "caniuse-lite@npm:1.0.30001279"
checksum: c0330577718e4221caae92a1bee8131f546668001840cf66455d5aa6050797f44709865779c5cdcba7cbf32527004a743dc2879497d23f60478f8bbb203cfeeb
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001283":
version: 1.0.30001312
resolution: "caniuse-lite@npm:1.0.30001312"
checksum: 753fb9ea1e02e999430b323a71b5acab5120f3b5fc0161b01669f54a3ef5c5296240b6ae9b79b12a3742e3aed216aa9ee3d5398a23c16d08625ccd376b79545d
"caniuse-lite@npm:^1.0.0, caniuse-lite@npm:^1.0.30000792, caniuse-lite@npm:^1.0.30000805, caniuse-lite@npm:^1.0.30000844, caniuse-lite@npm:^1.0.30000981, caniuse-lite@npm:^1.0.30001109, caniuse-lite@npm:^1.0.30001271, caniuse-lite@npm:^1.0.30001272, caniuse-lite@npm:^1.0.30001274, caniuse-lite@npm:^1.0.30001283":
APW26 marked this conversation as resolved.
Show resolved Hide resolved
version: 1.0.30001451
resolution: "caniuse-lite@npm:1.0.30001451"
checksum: 48a06a7881093bb4d8a08ed5428f24a1cbdaa544b0a6f0c3614287d4f34b6c853e79a0f608a5bd901c27995f5e951825606fba11e7930251cc422bd61de9d849
languageName: node
linkType: hard

Expand Down