Skip to content

Commit 83704fb

Browse files
committed
feat: Capture epoch time in DateInput
1 parent f791959 commit 83704fb

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

editor.planx.uk/src/@planx/components/Calculate/Editor.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export default function Component(props: Props) {
6060
// Type guard as mathjs evaluates `m` to a "Unit" object for "meter"
6161
if (typeof result === "number") {
6262
return result;
63+
// Allow component to output booleans
64+
} else if (typeof result === "boolean") {
65+
return result;
6366
} else {
6467
return UNKNOWN;
6568
}

editor.planx.uk/src/@planx/components/Calculate/model.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export function evaluate(input: string, scope = {}, defaults = {}): number {
7272

7373
function serializeKeys(object: any): Object {
7474
return Object.fromEntries(
75-
Object.entries(object).map(([key, value]) => [serialize(key), value]),
75+
Object.entries(object).map(([key, value]) => [
76+
serialize(key),
77+
Array.isArray(value) ? value[0] : value,
78+
]),
7679
);
7780
}
7881

editor.planx.uk/src/@planx/components/DateInput/Public.test.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ test("submits a date", async () => {
3333
expect(handleSubmit).toHaveBeenCalledWith({
3434
data: {
3535
[componentId]: "2010-05-22",
36+
[componentId + ".epoch"]: 1274486400,
3637
},
3738
});
3839
});
@@ -59,6 +60,7 @@ test("recovers previously submitted date when clicking the back button", async (
5960
expect(handleSubmit).toHaveBeenCalledWith({
6061
data: {
6162
[componentId]: "2010-05-22",
63+
[componentId + ".epoch"]: 1274486400,
6264
},
6365
});
6466
});
@@ -87,6 +89,7 @@ test("recovers previously submitted date when clicking the back button even if a
8789
expect(handleSubmit).toHaveBeenCalledWith({
8890
data: {
8991
[dataField]: "2010-05-22",
92+
[dataField + ".epoch"]: 1274486400,
9093
},
9194
});
9295
});

editor.planx.uk/src/@planx/components/DateInput/Public.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ const DateInputPublic: React.FC<Props> = (props) => {
2222
date: getPreviouslySubmittedData(props) ?? "",
2323
},
2424
onSubmit: (values) => {
25-
props.handleSubmit?.(makeData(props, values.date));
25+
const isoDate = makeData(props, values.date).data;
26+
const passportKey = Object.keys(isoDate)[0];
27+
const epochDate = {
28+
[passportKey + ".epoch"]: new Date(values.date).valueOf() / 1000,
29+
};
30+
31+
props.handleSubmit?.({
32+
data: {
33+
...isoDate,
34+
...epochDate,
35+
},
36+
});
2637
},
2738
validateOnBlur: false,
2839
validateOnChange: false,

editor.planx.uk/src/@planx/components/shared/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const makeData = <T>(
3737
props: any,
3838
value: T,
3939
overwriteKey?: string,
40-
): {} | { data: Record<string, T> } => {
40+
): Record<string, never> | { data: Record<string, T> } => {
4141
if (isEmpty(value)) return {};
4242
else
4343
return {

0 commit comments

Comments
 (0)