Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Capture epoch time in DateInput component #2686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 0 deletions editor.planx.uk/src/@planx/components/Calculate/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default function Component(props: Props) {
// Type guard as mathjs evaluates `m` to a "Unit" object for "meter"
if (typeof result === "number") {
return result;
// Allow component to output booleans
} else if (typeof result === "boolean") {
return result;
} else {
return UNKNOWN;
}
Expand Down
5 changes: 4 additions & 1 deletion editor.planx.uk/src/@planx/components/Calculate/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export function evaluate(input: string, scope = {}, defaults = {}): number {

function serializeKeys(object: any): Object {
return Object.fromEntries(
Object.entries(object).map(([key, value]) => [serialize(key), value]),
Object.entries(object).map(([key, value]) => [
serialize(key),
Array.isArray(value) ? value[0] : value,
]),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test("submits a date", async () => {
expect(handleSubmit).toHaveBeenCalledWith({
data: {
[componentId]: "2010-05-22",
[componentId + ".epoch"]: 1274486400,
},
});
});
Expand All @@ -59,6 +60,7 @@ test("recovers previously submitted date when clicking the back button", async (
expect(handleSubmit).toHaveBeenCalledWith({
data: {
[componentId]: "2010-05-22",
[componentId + ".epoch"]: 1274486400,
},
});
});
Expand Down Expand Up @@ -87,6 +89,7 @@ test("recovers previously submitted date when clicking the back button even if a
expect(handleSubmit).toHaveBeenCalledWith({
data: {
[dataField]: "2010-05-22",
[dataField + ".epoch"]: 1274486400,
},
});
});
Expand Down
13 changes: 12 additions & 1 deletion editor.planx.uk/src/@planx/components/DateInput/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ const DateInputPublic: React.FC<Props> = (props) => {
date: getPreviouslySubmittedData(props) ?? "",
},
onSubmit: (values) => {
props.handleSubmit?.(makeData(props, values.date));
const isoDate = makeData(props, values.date).data;
const passportKey = Object.keys(isoDate)[0];
const epochDate = {
[passportKey + ".epoch"]: new Date(values.date).valueOf() / 1000,
};

props.handleSubmit?.({
data: {
...isoDate,
...epochDate,
},
});
},
validateOnBlur: false,
validateOnChange: false,
Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/@planx/components/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const makeData = <T>(
props: any,
value: T,
overwriteKey?: string,
): {} | { data: Record<string, T> } => {
): Record<string, never> | { data: Record<string, T> } => {
if (isEmpty(value)) return {};
else
return {
Expand Down
Loading