Skip to content

Commit

Permalink
Use zod types for DATE_RANGE_TYPE.
Browse files Browse the repository at this point in the history
Part of #673.
  • Loading branch information
jkomoros committed Nov 11, 2023
1 parent 9ac6255 commit d9ef224
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"reselect-map": "^1.0.6",
"snarkdown": "^2.0.0"
"snarkdown": "^2.0.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@firebase/testing": "^0.19.6",
Expand Down
11 changes: 4 additions & 7 deletions src/components/configure-collection-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import {
CONFIGURABLE_FILTER_URL_PARTS
} from '../filters.js';

import {
DATE_RANGE_TYPES
} from '../type_constants.js';

import { ButtonSharedStyles } from './button-shared-styles.js';

import {
makeFilterModifiedComplexEvent
} from '../events.js';

import {
DateRangeType
dateRangeType
} from '../types.js';

@customElement('configure-collection-date')
Expand Down Expand Up @@ -46,7 +42,7 @@ class ConfigureCollectionDate extends LitElement {
return html`
<div>
<select .value=${typ} @change=${this._handleTypeChanged}>
${Object.keys(DATE_RANGE_TYPES).map(typ => html`<option .value=${typ}>${typ}</option>`)}
${dateRangeType.options.map(typ => html`<option .value=${typ}>${typ}</option>`)}
</select>
<input type='date' .value=${String(dateOne)} data-first=${true} @change=${this._handleDateChanged}>
<input type='date' .value=${String(dateTwo)} ?hidden=${!typeRequiresSecondDate} @change=${this._handleDateChanged}>
Expand All @@ -62,7 +58,8 @@ class ConfigureCollectionDate extends LitElement {
const ele = e.composedPath()[0];
if (!(ele instanceof HTMLSelectElement)) throw new Error('not select element');
const [, dateOne, dateTwo] = parseDateSection(this.value);
this._dispatchNewValue(makeDateSection(ele.value as DateRangeType, dateOne, dateTwo));
const val = dateRangeType.parse(ele.value);
this._dispatchNewValue(makeDateSection(val, dateOne, dateTwo));
}

_handleDateChanged(e : Event) {
Expand Down
18 changes: 9 additions & 9 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import {
CARD_TYPE_CONCEPT,
REFERENCE_TYPE_CONCEPT,
REFERENCE_TYPE_LINK,
BEFORE_FILTER_NAME,
AFTER_FILTER_NAME,
BETWEEN_FILTER_NAME,
URL_PART_DATE_SECTION,
URL_PART_FREE_TEXT,
URL_PART_KEY_CARD,
Expand Down Expand Up @@ -276,7 +273,7 @@ export const parseDateSection = (str : string) : [dateType : DateRangeType, firs
};

export const makeDateSection = (comparsionType : DateRangeType, dateOne : Date, dateTwo : Date) : string => {
const result = [comparsionType];
const result : string[] = [comparsionType];
result.push('' + dateOne.getFullYear() + '-' + (dateOne.getMonth() + 1) + '-' + dateOne.getDate());
if (CONFIGURABLE_FILTER_URL_PARTS[comparsionType] == 2) {
if (!dateTwo) dateTwo = new Date();
Expand All @@ -300,23 +297,23 @@ const makeDateConfigurableFilter = (propName : CardTimestampPropertyName | typeo
let func : ((card : ProcessedCard) => FilterFuncResult) = () => ({matches: false});

switch (comparisonType) {
case BEFORE_FILTER_NAME:
case 'before':
func = function(card) {
const val = card[cardKey] as Timestamp;
if (!val) return {matches: false};
const difference = val.toMillis() - firstDate.getTime();
return {matches: difference < 0};
};
break;
case AFTER_FILTER_NAME:
case 'after':
func = function(card) {
const val = card[cardKey] as Timestamp;
if (!val) return {matches: false};
const difference = val.toMillis() - firstDate.getTime();
return {matches: difference > 0};
};
break;
case BETWEEN_FILTER_NAME:
case 'between':
//Bail if the second date isn't provided
if (secondDate) {
func = function(card) {
Expand All @@ -329,8 +326,7 @@ const makeDateConfigurableFilter = (propName : CardTimestampPropertyName | typeo
}
break;
default:
const _exhaustiveCheck : never = comparisonType;
return _exhaustiveCheck ? [func, false] : [func, false];
assertUnreachable(comparisonType);
}

return [func, false];
Expand Down Expand Up @@ -816,6 +812,10 @@ const makeNoOpConfigurableFilter = () : ConfigurableFilterFuncFactoryResult => {
return [() => ({matches: true}), false];
};

const BEFORE_FILTER_NAME : DateRangeType = 'before';
const AFTER_FILTER_NAME : DateRangeType = 'after';
const BETWEEN_FILTER_NAME : DateRangeType = 'between';

//When these are seen in the URL as parts, how many more pieces to expect, to be
//combined later. For things like `updated`, they want more than 1 piece more
//(e.g. `before/2020-10-03`, but the next pieces will also ask for more) in the
Expand Down
10 changes: 0 additions & 10 deletions src/type_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,6 @@ export const REFERENCE_TYPE_TYPES = {
[REFERENCE_TYPE_CITATION_PERSON]: true
};

export const BEFORE_FILTER_NAME = 'before';
export const AFTER_FILTER_NAME = 'after';
export const BETWEEN_FILTER_NAME = 'between';

export const DATE_RANGE_TYPES = {
[BEFORE_FILTER_NAME]: true,
[AFTER_FILTER_NAME]: true,
[BETWEEN_FILTER_NAME]: true,
};

export const URL_PART_DATE_SECTION = 'date';
export const URL_PART_FREE_TEXT = 'text';
export const URL_PART_KEY_CARD = 'key-card';
Expand Down
13 changes: 11 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
z
} from 'zod';

import {
FieldValue,
Timestamp
Expand All @@ -8,7 +12,6 @@ import {
TEXT_FIELD_TYPES,
REFERENCE_TYPE_TYPES,
TEXT_FIELD_TYPES_EDITABLE,
DATE_RANGE_TYPES,
URL_PART_TYPES,
IMAGE_POSITION_TYPES,
SET_NAME_TYPES,
Expand Down Expand Up @@ -58,7 +61,13 @@ export type CardFieldTypeEditable = keyof(typeof TEXT_FIELD_TYPES_EDITABLE)

export type CardFieldType = keyof(typeof TEXT_FIELD_TYPES);

export type DateRangeType = keyof(typeof DATE_RANGE_TYPES);
export const dateRangeType = z.enum([
'before',
'after',
'between'
]);

export type DateRangeType = z.infer<typeof dateRangeType>;

export type FontSizeBoostMap = {
[name in CardFieldType]+?: number
Expand Down

0 comments on commit d9ef224

Please sign in to comment.