Skip to content

Commit

Permalink
chore: update @types/luxon and fix usage
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Mar 20, 2023
1 parent a368f66 commit 2409789
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 89 deletions.
2 changes: 1 addition & 1 deletion apps/central-scan/backend/package.json
Expand Up @@ -82,7 +82,7 @@
"@types/express": "^4.17.14",
"@types/fs-extra": "^9.0.6",
"@types/jest": "^26.0.6",
"@types/luxon": "^1.26.5",
"@types/luxon": "^3.0.0",
"@types/multer": "^1.4.7",
"@types/node": "16.11.29",
"@types/supertest": "^2.0.10",
Expand Down
2 changes: 1 addition & 1 deletion apps/mark/frontend/package.json
Expand Up @@ -132,7 +132,7 @@
"@types/debug": "^4.1.6",
"@types/history": "^4.7.8",
"@types/kiosk-browser": "workspace:*",
"@types/luxon": "^1.26.2",
"@types/luxon": "^3.0.0",
"@types/react-gamepad": "^1.0.0",
"@types/setimmediate": "^1.0.2",
"@types/testing-library__jest-dom": "^5.14.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/scan/backend/package.json
Expand Up @@ -79,7 +79,7 @@
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.6",
"@types/jest": "^26.0.6",
"@types/luxon": "^1.26.5",
"@types/luxon": "^3.0.0",
"@types/multer": "^1.4.7",
"@types/node": "16.11.29",
"@types/supertest": "^2.0.10",
Expand Down
2 changes: 1 addition & 1 deletion libs/ballot-interpreter-nh/package.json
Expand Up @@ -43,7 +43,7 @@
"@types/he": "^1.1.2",
"@types/jest": "^27.4.0",
"@types/jsdom": "^20.0.0",
"@types/luxon": "^2.0.9",
"@types/luxon": "^3.0.0",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "^5.37.0",
"@typescript-eslint/parser": "^5.37.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/test-utils/package.json
Expand Up @@ -49,7 +49,7 @@
"devDependencies": {
"@types/jest": "^27.0.3",
"@types/kiosk-browser": "workspace:*",
"@types/luxon": "^1.26.5",
"@types/luxon": "^3.0.0",
"@types/react": "17.0.39",
"@types/zip-stream": "workspace:*",
"@typescript-eslint/eslint-plugin": "^5.37.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/test-utils/src/arbitraries.ts
Expand Up @@ -178,7 +178,7 @@ export function arbitraryDateTime({
})
.map((parts) => {
try {
const result = DateTime.fromObject({ ...parts, zone: zoneName });
const result = DateTime.fromObject(parts, { zone: zoneName });
if (
result.year === parts.year &&
result.month === parts.month &&
Expand Down
2 changes: 1 addition & 1 deletion libs/types/package.json
Expand Up @@ -47,7 +47,7 @@
"devDependencies": {
"@types/jest": "^27.0.3",
"@types/kiosk-browser": "workspace:*",
"@types/luxon": "^2.3.2",
"@types/luxon": "^3.0.0",
"@types/node": "16.11.29",
"@types/react": "17.0.39",
"@typescript-eslint/eslint-plugin": "^5.37.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/ui/package.json
Expand Up @@ -80,7 +80,7 @@
"@types/history": "4",
"@types/jest": "^27.0.3",
"@types/kiosk-browser": "workspace:*",
"@types/luxon": "^1.26.5",
"@types/luxon": "^3.0.0",
"@types/node": "16.11.29",
"@types/pluralize": "^0.0.29",
"@types/react": "17.0.39",
Expand Down
40 changes: 22 additions & 18 deletions libs/ui/src/set_clock.test.tsx
Expand Up @@ -23,15 +23,17 @@ function getSelect(testId: string): HTMLSelectElement {
return screen.getByTestId(testId);
}

const aDate = DateTime.fromObject({
year: 2021,
month: 3,
day: 31,
hour: 19,
minute: 34,
second: 56,
zone: 'America/Los_Angeles',
});
const aDate = DateTime.fromObject(
{
year: 2021,
month: 3,
day: 31,
hour: 19,
minute: 34,
second: 56,
},
{ zone: 'America/Los_Angeles' }
);

describe('PickDateTimeModal', () => {
test('shows pickers for the datetime parts of the given time', () => {
Expand Down Expand Up @@ -141,15 +143,17 @@ describe('PickDateTimeModal', () => {
// Expect a changed timezone
expect(onSave).toHaveBeenNthCalledWith(
3,
DateTime.fromObject({
year: aDate.year,
month: aDate.month,
day: changedDay,
hour: aDate.hour,
minute: aDate.minute,
second: 0,
zone: 'America/Chicago',
})
DateTime.fromObject(
{
year: aDate.year,
month: aDate.month,
day: changedDay,
hour: aDate.hour,
minute: aDate.minute,
second: 0,
},
{ zone: 'America/Chicago' }
)
);
});

Expand Down
59 changes: 36 additions & 23 deletions libs/ui/src/set_clock.tsx
@@ -1,4 +1,4 @@
import { DateTime } from 'luxon';
import { DateTime, HourNumbers } from 'luxon';
import React, { useCallback, useState } from 'react';

import {
Expand Down Expand Up @@ -28,6 +28,15 @@ export interface PickDateAndTimeProps {
value: DateTime;
}

function asHour(hour: number): HourNumbers {
/* istanbul ignore next */
if (hour < 0 || hour > 23) {
throw new Error(`Invalid hour: ${hour}`);
}

return hour as HourNumbers;
}

export function PickDateTimeModal({
disabled = false,
onCancel,
Expand All @@ -42,20 +51,20 @@ export function PickDateTimeModal({
const { name, value: stringValue } = event.currentTarget;
// eslint-disable-next-line vx/gts-safe-number-parse
const partValue = parseInt(stringValue, 10);
let { hour } = newValue;
let hour = asHour(newValue.hour);
if (name === 'hour') {
if (systemMeridian === 'AM') {
hour = partValue % 12;
hour = asHour(partValue % 12);
} else {
hour = (partValue % 12) + 12;
hour = asHour((partValue % 12) + 12);
}
}
if (name === 'meridian') {
if (stringValue === 'AM' && newValue.hour >= 12) {
hour = newValue.hour - 12;
hour = asHour(newValue.hour - 12);
}
if (stringValue === 'PM' && newValue.hour < 12) {
hour = newValue.hour + 12;
hour = asHour(newValue.hour + 12);
}
}
const year = name === 'year' ? partValue : newValue.year;
Expand All @@ -64,28 +73,32 @@ export function PickDateTimeModal({
const lastDayOfMonth = daysInMonth[daysInMonth.length - 1].day;
const day = name === 'day' ? partValue : newValue.day;
setNewValue(
DateTime.fromObject({
year,
month,
day: lastDayOfMonth && day > lastDayOfMonth ? lastDayOfMonth : day,
hour,
minute: name === 'minute' ? partValue : newValue.minute,
zone: newValue.zone,
})
DateTime.fromObject(
{
year,
month,
day: lastDayOfMonth && day > lastDayOfMonth ? lastDayOfMonth : day,
hour,
minute: name === 'minute' ? partValue : newValue.minute,
},
{ zone: newValue.zone }
)
);
};
const updateTimeZone: SelectChangeEventFunction = useCallback(
(event) => {
setNewValue(
DateTime.fromObject({
year: newValue.year,
month: newValue.month,
day: newValue.day,
hour: newValue.hour,
minute: newValue.minute,
second: newValue.second,
zone: event.currentTarget.value,
})
DateTime.fromObject(
{
year: newValue.year,
month: newValue.month,
day: newValue.day,
hour: newValue.hour,
minute: newValue.minute,
second: newValue.second,
},
{ zone: event.currentTarget.value }
)
);
},
[newValue, setNewValue]
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/package.json
Expand Up @@ -56,7 +56,7 @@
"@types/fast-text-encoding": "^1.0.1",
"@types/jest": "^27.0.3",
"@types/kiosk-browser": "workspace:*",
"@types/luxon": "^1.26.5",
"@types/luxon": "^3.0.0",
"@types/randombytes": "^2.0.0",
"@types/yargs": "^17.0.12",
"@typescript-eslint/eslint-plugin": "^5.37.0",
Expand Down

0 comments on commit 2409789

Please sign in to comment.